Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I am using a Lambda function to query Amazon RDS. Lambda and RDS are in the same VPC, and also the RDS instance is publicly accessible. When the Lambda function is tested, i get the logs with the queried data but this error comes with it:

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: 'name' },

  RowDataPacket { id: 2, name: 'second' },

  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

1 Answer

0 votes
by (44.4k points)

The error you get is because Lambda does not know whether the execution is over or not. To make the Lambda function understand, you should return something like success in the code.

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"); //Lambda understands the execution is a success

  });

});

//callback("sucess");

}

Browse Categories

...