0
我想写一个node.js lambda函数,它将连接到AWS中的一个MYSQL RDS实例内的数据库并拉下一些示例记录,但我是得到上述错误。'致命错误:getaddrinfo ENOTFOUND' - node.js mysql lambda
我已经创建了可公开访问的RDS,并且还创建了与RDS相同的VPC下的lambda方法。我使用的代码如下:
var mysql=require('mysql'); //Require whatever connector you need.
//console.log("beware of ", beware);
var beware=1; //This is declared outside the handler: it is possible that its last value will be reused when we enter the function again.
//This function will return a connection. Notice that it's declared outside
//the handler, thus inside the container. It may be reused but I don't think
//that matters because it is self-contained.
function getConnectionCred()
{
var params={
host : '(myconnname).amazonaws.com:3306',
user : 'username',
password : 'password',
database: 'database'};
return mysql.createConnection(params);
}
//This is your handler.
exports.handler=function(event, context)
{
//This is declared inside the handler: it is guaranteed to never be reused!.
var connection=getConnectionCred();
var del = connection._protocol._delegateError;
connection._protocol._delegateError = function(err, sequence){
if (err.fatal) {
console.trace('fatal error: ' + err.message);
}
return del.call(this, err, sequence);
};
//Do things with your connection.
var query_string='SELECT * from table';
connection.query(query_string, [beware], function(res, err){
//Check for errors, disconnect and exit with failure.
if(err){
console.log("Query failed", err);
connection.end(function(err){
context.fail(0);
});
}
//Disconnect and exit with success.
else{
connection.end(function(err){
if(err){
console.log("Warning: disconnection failed" + err);
}
context.succeed(res);
});
}
});
}
如果有人能指出我得到这个错误的方向是正确的决定,将是真棒。
谢谢!
嗨,我已经尝试了以上,并收到以下错误:https://pastebin.com/BZ4VyAeS –
所以这是一个非常不同的错误然后。检查lambda是否有权访问数据库主机。 – hsz
嗨,Lambda函数是在与RDS相同的VPC和安全组下创建的 - 是否需要执行其他任何配置? –