lambda function showing error when trying to connect with rds aws

+1 vote

I've written a lambda func. to query data from amazon rds. All the ports are open and lambda and rds reside in default vpc. The problem is whenever I test the lambda function, I do get the log output with the queried data but this also shows up:

Execution result: failed with "errorMessage": "2017-07-05T15:05:27.425Z 596fdf39-6193-11e7-9176-f58796899f9b Task timed out after 3.00 seconds" }

var mysql = require('mysql');

exports.handler = (event, context) => {
var con = mysql.createConnection({
  host: "testdb.cxyzu.ap-south-1.rds.amazonaws.com",
  user: "root",
  password: "mypassword",
  database: "test",
  port: "3306",
// debug: true
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
// var sql = "INSERT INTO users (id, name) VALUES (4, 'dfdd')";
var sql = "select * from test.users";
  con.query(sql, function (err, result) {
    if (err) throw err;
//    console.log("1 record inserted");
      console.log(result);
  });
});
//callback("sucess");
}

and this is the output:

START RequestId: 596fdf39-6193-11e7-9176-f58796899f9b Version: $LATEST
2017-07-05T15:05:24.680Z	596fdf39-6193-11e7-9176-f58796899f9b	Connected!
2017-07-05T15:05:24.684Z	596fdf39-6193-11e7-9176-f58796899f9b	[ RowDataPacket { id: 1, name: 'sai' },
  RowDataPacket { id: 2, name: 'chandra' },
  RowDataPacket { id: 3, name: 'AA' },
  RowDataPacket { id: 4, name: 'dfdd' } ]
END RequestId: 596fdf39-6193-11e7-9176-f58796899f9b
REPORT RequestId: 596fdf39-6193-11e7-9176-f58796899f9b	Duration: 3003.80 ms	Billed Duration: 3000 ms 	Memory Size: 1536 MB	Max Memory Used: 21 MB	
2017-07-05T15:05:27.425Z 596fdf39-6193-11e7-9176-f58796899f9b Task timed out after 3.00 seconds
Jun 6, 2018 in DevOps on Cloud by Atul
• 10,240 points
5,276 views

2 answers to this question.

0 votes
Best answer

The engine will stay on timeout unless you exit lambda through success or error callback. You can try and call 'context.succeed("done"):' after you code:

var mysql = require('mysql');

exports.handler = (event, context) => {
var con = mysql.createConnection({
  host: "testdb.cxyzu.ap-south-1.rds.amazonaws.com",
  user: "root",
  password: "mypassword",
  database: "test",
  port: "3306",
 // debug: true
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
 // var sql = "INSERT INTO users (id, name) VALUES (4, 'dfdd')";
var sql = "select * from test.users";
  con.query(sql, function (err, result) {
    if (err) throw err;
//    console.log("1 record inserted");
      console.log(result);
      context.succeed("done");
  });
});
//callback("sucess");
}

Read this. It'll help: Lambda Function Handler (Node.js)

answered Jun 6, 2018 by ajs3033
• 7,300 points

selected Aug 22, 2018 by Priyaj
0 votes
  • There are lots of documentation that says to have 2 private subnets for lambda in your VPC and have internet connection using NAT gateway etc..
  • Actually I was able to connect to RDS in default VPC directly from lambda(without placing it in private subnets). Issue was I had imported pymysql file inside of pacakage folder, so I was getting
    that connection Timeout error.
  • I just had to prefix package in from of pymysql (package.mysql)
    except Exception as error: did trick for me
answered Aug 17, 2018 by Priyaj
• 58,020 points

Related Questions In DevOps on Cloud

+1 vote
0 answers

Getting error when I start build in aws code build .

When I start build using code build ...READ MORE

Dec 3, 2019 in DevOps on Cloud by sri
• 250 points
1,429 views
+2 votes
2 answers
+2 votes
3 answers

can't make nginx try_files to work with default_index.html

Here's another convenient use of try_files, as ...READ MORE

answered Oct 18, 2018 in DevOps on Cloud by Hannah
• 18,520 points
5,249 views
+1 vote
3 answers
0 votes
1 answer
0 votes
1 answer
+2 votes
3 answers
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP