【发布时间】:2019-04-15 19:37:09
【问题描述】:
我正在尝试学习如何在 AWS 中使用 lambda 函数连接 MySQL。我在网上遵循了一些说明,基本上得到了这段代码:
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 1000,
connectTimeout : 60 * 60 * 1000,
acquireTimeout : 60 * 60 * 1000,
timeout : 60 * 60 * 1000,
host: "foo-bar-123.us-east-2.rds.amazonaws.com",
user: "root",
password: "pass123",
database: "sample_db",
});
exports.handler = (event, context, callback) => {
// prevent timeout from waiting event loop
context.callbackWaitsForEmptyEventLoop = false;
pool.getConnection(function(err, connection) {
if (err) throw err;
// Use the connection
connection.query('SELECT id FROM customer limit 10;', function (error, results, fields) {
// And done with the connection.
connection.release();
// Handle error after the release.
if (error) callback(error);
else callback(null,results);
});
});
};
这在我的本地工作,但是当我压缩此代码并将其作为 lambda 函数上传时,这将返回
Response:
{
"errorMessage": "2018-11-13T02:16:10.339Z 0562b432-e6ea-11e8-81ef-bd64aa1af0a4 Task timed out after 30.03 seconds"
}
无论我设置多少秒都会超时。
我几乎将所有内容都设置为默认值,因为我对所有这些都是新手,但我已将 AmazonRDSFullAccess 添加到 lambda 函数的角色中。
有人知道我的设置可能有什么问题或遗漏吗?
谢谢。
【问题讨论】:
-
你有在 VPC 后面运行的 Lamda 吗?
-
只需通过在上下文对象上设置 callbackWaitsForEmptyEventLoop = false 来更改行为,以便在调用回调函数时立即结束执行。 bcz 默认为真......你想设置
-
一开始设置为无VPC,然后尝试添加我在RDS实例中看到的子网和安全组,但连接仍然超时。
标签: mysql node.js amazon-web-services aws-lambda amazon-rds