Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"errorMessage": Task timed out after 3.00 seconds aws lambda nodejs lambda function trying to connect with RDS

I have written a simple lambda function in nodejs which queries data from amazon rds.(note : my lambda and rds are in default vpc with all ports open and also tried increasing time out in lambda)

My issue is when I test my lambda function I get log output with the queried data but I am also getting

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");
}

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
like image 234
chandra Avatar asked Jul 05 '17 15:07

chandra


People also ask

Why is my Lambda function timing out?

There are three reasons why retry and timeout issues occur when invoking a Lambda function with an AWS SDK: A remote API is unreachable or takes too long to respond to an API call. The API call doesn't get a response within the socket timeout.

What is the most likely issue with the lambda function's timeout?

There are many reasons why a function might time out, but the most likely is that it was waiting on an IO operation to complete. Maybe it was waiting on another service (such as DynamoDB or Stripe) to respond. Within a Lambda invocation, the function might perform multiple IO operations.


2 Answers

The accepted solution doesn´t work for me. I´m connecting to a RDS instance and if I send "context.succeed("done")", the callback is not called.

I´m connecting to an Amazon RDS instance inside a lambda running nodejs 8.1.

SOLUTION:

In order to exit of "Nodejs event Loop" you must add the next code to invoke the callback:

connection.end( function(err) {
    if (err) {console.log("Error ending the connection:",err);}

    //  reconnect in order to prevent the"Cannot enqueue Handshake after invoking quit"

    connection = mysql.createConnection({
        host     : 'rds.host',
        port     :  3306,
        user     : 'user',
        password : 'password',
        database : 'target database'

    });
    callback(null, {
        statusCode: 200,
        body: response,

    });
});
like image 162
Jorge Valvert Avatar answered Oct 17 '22 18:10

Jorge Valvert


You need to exit the lambda through a success or error callback. Otherwise, the engine stays on until a timeout occurs.

The easiest way to exit your lambda would be to call 'context.succeed("done");' after you code is done.

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");
}

Here some basic introduction to the topic:

Lambda Function Handler (Node.js)

like image 28
jens walter Avatar answered Oct 17 '22 18:10

jens walter