If you can connect from your local system into RDS then it should be working from the lambda as well I think. We do support any database or pretty much any service that AWS provides and also node_modules. Basically if it works in NodeJS it should work in Lambda with Serverless.
so serverless already support it, so, if i not wanna use vpc… i can still use RDS right ?
is it something i must include on .yml file to getting node_module ?
@frengkys There are a few of places this could be going wrong. I’d start by making sure you have a working Lambda function that at least attempts to create a connection. console.log() and the CloudWatch logs will help you.
I guess you should close the connection using con.destroy(); before calling context.succeed but the log in CloudWatch should tell you more. Locate the function in aws console, go to Monitoring tab and follow link “View logs in CloudWatch”
it should be
’-------------------- Error connect to DB -------------------- or -------------------- DB is connected ! ---------------------- right?
but none of them shown…
You database connection code is outside of the Lambda function handler. I wouldn’t assume that it’s being run every time the Lambda is called.
Try moving the const con = mysql.createConnection(...) and con.connect(...) inside the Lambda handler function so that you know it’s actually being run.
module.exports.send = (event, context, callback) => {
const mysql =require(‘mysql’);
const con = mysql.createConnection({
host : “xxxxxxxxxxxxxxx”,
user : “xxxxxxxxx”,
password : “xxxxxxxxxxx”,
database : “xxxxxxx”,
});
con.connect(function(err){
if (err){
console.log(‘-------------------- Error connect to DB --------------------’);
return;
}
console.log(‘-------------------- DB is connected ! ----------------------’);
});
var data = event.body.json_data;
console.log(“pre”);
con.query(‘SELECT 1 + 1 AS solution’, function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
console.log(“post”);
var temp = {name : “frengky”};
var query = con.query(‘INSERT INTO temp SET ?’, temp,function(err,res){
if(err) throw err ;
});
console.log(query.sql);
the logs still isnt say anything except ‘pre’ and ‘post’ log… oh and console.log(query.sql)
but the logs that say Mysql is connected or not isn’t shown… @buggy@JakubMatejka