简体   繁体   English

从Lambda函数(节点)连接到MySQL数据库

[英]Connect to MySQL database from Lambda function (Node)

I have been unable to connect to MySQL database using Node from Lambda function. 我无法使用Lambda函数中的Node连接到MySQL数据库。 The error I receive is Task timed out after 4.00 seconds . 我收到的错误是Task timed out after 4.00 seconds

Does anyone have any solutions? 有没有人有任何解决方案?

Here is an overview of my state: 以下是我的州概况:

  1. The AWS RDS database is a MySQL database. AWS RDS数据库是MySQL数据库。 It is not confined to the VPC (I am able to connect using host/user/password from MySQLWorkbench). 它不仅限于VPC(我可以使用MySQLWorkbench的主机/用户/密码进行连接)。
  2. The execution role of my Lambda function is set to have Lambda as a trusted entity and given AdministratorAccess. 我的Lambda函数的执行角色设置为将Lambda作为可信实体并给予AdministratorAccess。
  3. On my local machine, I installed the mysql module, zipped my index.js and node_modules folder, and uploaded to my Lambda function. 在我的本地机器上,我安装了mysql模块,压缩了我的index.js和node_modules文件夹,并上传到我的Lambda函数。
  4. I have tried putting the createConnection and connect function inside the handler. 我已经尝试将createConnection和connect函数放在处理程序中。 I have tried putting my query inside the callback function of the connection function. 我已经尝试将我的查询放在连接函数的回调函数中。 I have tried increasing the timeout time to 10 seconds. 我已经尝试将超时时间增加到10秒。
  5. My code: 我的代码:

     var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'amazon-string.rds.amazonaws.com', user : 'myusername', password : 'mypassword' }); connection.connect(); exports.handler = (event, context, callback) => { connection.query("SELECT * FROM table", function(err, rows, fields) { console.log("rows: " + rows); callback(null); }); }; 

Increase the timeout to one minute. 将超时时间增加到一分钟。 It could be due to the coldstart of the lambda function. 这可能是由于lambda函数的冷启动。

Only your first call should take time, consecutive calls should be very fast, since you are reusing the same connection. 只有您的第一个呼叫需要花费时间,连续呼叫应该非常快,因为您正在重复使用相同的连接。

Also, By having higher timeout, does not mean you will be charged for that timeout, you will be charged only for the time the Lambda runs. 此外,通过更高的超时,并不意味着您将被收取超时费用,您将只收取Lambda运行时的费用。

Also to speed up the coldstart time you can webpack your scripts, 另外,为了加快冷启动时间,您可以将脚本包装好

http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/webpack.html http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/webpack.html

There is one more issue noticed, 还有一个问题被注意到,

var mysql = require('mysql');

var connection = mysql.createConnection({
    host     : 'amazon-string.rds.amazonaws.com',
    user     : 'myusername',
    password : 'mypassword'
});

connection.connect();

exports.handler = (event, context) => {

    connection.query("SELECT * FROM table", function(err, rows, fields) {
        console.log("rows: " + rows);
        context.succeed('Success');
    });

};

Hope it helps. 希望能帮助到你。

Since you're using RDS, go check out it's security group configuration. 由于您正在使用RDS,请查看它的安全组配置。 By default RDS's security group will allow inbound connections from your own IP and your default security group on your default VPC. 默认情况下,RDS的安全组将允许来自您自己的IP和默认VPC上的默认安全组的入站连接。 However Lambda, by default, runs under no VPC, and thus is not able to establish a connection to RDS. 但是,默认情况下,Lambda在没有VPC的情况下运行,因此无法与RDS建立连接。

Either change your RDS instance to allow all IP addresses, or execute your Lambda function under a VPC that your RDS instance can access, and allow access to the security group. 更改RDS实例以允许所有IP地址,或在RDS实例可以访问的VPC下执行Lambda功能,并允许访问安全组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM