"errorMessage":任务在 3.00 秒后超时 aws lambda nodejs lambda 函数尝试连接 RDS

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

我在 nodejs 中编写了一个简单的 lambda 函数,它从 amazon rds 查询数据。(注意:我的 lambda 和 rds 在默认 vpc 中,所有端口都打开,并且还尝试增加 lambda 中的超时时间)

我的问题是,当我测试我的 lambda 函数时,我得到 日志输出 和查询数据,但我也得到

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

您需要通过成功或错误回调退出 lambda。否则,引擎将保持开启状态,直到发生超时。

退出 lambda 的最简单方法是在代码完成后调用 'context.succeed("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");
}

这里是对主题的一些基本介绍:

Lambda Function Handler (Node.js)

接受的解决方案对我不起作用。我连接到 RDS 实例,如果我发送 "context.succeed("done")",则不会调用回调。

我正在连接到 lambda 运行 nodejs 8.1 中的 Amazon RDS 实例。

解决方案:

为了退出 "Nodejs event Loop",您必须添加下一个代码来调用回调:

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,

    });
});