[英]“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) 我在nodejs中编写了一个简单的lambda函数,它查询来自amazon rds的数据。(注意:我的lambda和rds是默认的vpc,所有端口都打开,并尝试增加lambda中的时间)
My issue is when I test my lambda function I get log output with the queried data but I am also getting 我的问题是当我测试我的lambda函数时,我得到了带有查询数据的日志输出 ,但我也得到了
Execution result: failed with "errorMessage": "2017-07-05T15:05:27.425Z 596fdf39-6193-11e7-9176-f58796899f9b Task timed out after 3.00 seconds" }
执行结果:“errorMessage”失败:“2017-07-05T15:05:27.425Z 596fdf39-6193-11e7-9176-f58796899f9b任务在3.00秒后超时”}
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
You need to exit the lambda through a success or error callback. 您需要通过成功或错误回调退出lambda。 Otherwise, the engine stays on until a timeout occurs.
否则,引擎会一直运行,直到发生超时。
The easiest way to exit your lambda would be to call 'context.succeed("done");' 退出lambda的最简单方法是调用'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: 这里有一些基本的主题介绍:
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.
我连接到RDS实例,如果我发送“context.succeed(”done“)”,则不调用回调。
I´m connecting to an Amazon RDS instance inside a lambda running nodejs 8.1. 我正在运行nodejs 8.1的lambda中连接到Amazon RDS实例。
SOLUTION: 解:
In order to exit of "Nodejs event Loop" you must add the next code to invoke the callback: 要退出“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,
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.