简体   繁体   English

在RDS中访问MySQL或MS SQL时的AWS Lambda node.js ETIMEDOUT

[英]AWS Lambda node.js ETIMEDOUT when accessing MySQL or MS SQL in RDS

I am trying to a connect a AWS Lambda function to a database in RDS, I followed a few posts that have suggested they both need to be inside the same VPC, but have been told by Amazon support that this is not the case. 我正在尝试将AWS Lambda函数连接到RDS中的数据库,我关注了几篇文章,建议它们都必须位于同一VPC内,但Amazon支持人员已告知情况并非如此。 Although I have tried a multi subnet VPC with nat gateway and security groups allowing access to each, with no prevail. 尽管我尝试了带有nat网关和安全组的多子网VPC,但每个组都无法访问。

I am able to talk to the Database instances in SQL management studio or MySQL workbench without a problem. 我可以毫无问题地与SQL Management Studio或MySQL工作台中的数据库实例进行对话。

10:13:00 2017-05-16T10:13:00.509Z Callback connection.connect() 10:13:00 2017-05-16T10:13:00.547Z Error ETIMEDOUT 10:13:00 2017-05-16T10:13:00.509Z回调connection.connect()10:13:00 2017-05-16T10:13:00.547Z错误ETIMEDOUT

I have tried a few different methods of connection now, I used an example as seen in the post, the only difference is this databases wasn't in RDS. 我现在尝试了几种不同的连接方法,我使用了一个帖子中所示的示例,唯一的区别是该数据库不在RDS中。

Querying a MySQL database from a NodeJS AWS Lambda Function 从NodeJS AWS Lambda函数查询MySQL数据库

I have tried a MSSQL and MSSQL databases instances, both experiences a timeout on connection. 我尝试了一个MSSQL和MSSQL数据库实例,两者都遇到连接超时。 I have attached all relevant policy's to the lambda function. 我已将所有相关策略附加到lambda函数。

I have included below one of my simple tests, just to try and get a connection. 我在下面的简单测试之一中进行了介绍,只是为了尝试建立连接。

Test Code added below, not this at the bottom of a alexa call. 下面添加了测试代码,而不是在Alexa调用的底部添加了此代码。

function mysqltest2() {
    var mysql      = require('mysql');
    var connection = mysql.createConnection({
      host     : 'endpoint.rds.amazonaws.com',
      user     : 'user',
      password : 'pass',
      database : 'db'
      });
                  var test;
                  console.log(`Intent Request`);
                  console.log('Then run MySQL code:');
                  connection.connect(function(err) {
                      console.log('Inside connection.connect() callback');
                      if (!err) {
                          console.log("Database is connected ... ");
                          connection.query("SELECT 1 + 1 AS solution",
                              function(err, result) {
                                  console.log("Inside connection.query() callback")
                                  if (!err) {
                                      console.log(result);
                                      console.log("Query Successful! Ending Connection.");
                                      test = result;
                                      connection.end();
                            } else {
                                      console.log("Query error!");
                                  }
                              });
                      } else {
                          console.log("Error connecting database ..." +             err.message);
                      }
                  });
                  return test;
  }

There were two problems. 有两个问题。

1 VPC Setup 1 VPC设置

VPC setup, reading @ Michael - sqlbot post about VPCs helped a lot. VPC设置,请阅读@ Michael-有关VPC的sqlbot帖子对您有很大帮助。

When using lambda within a VPC the following needs to be taken into consideration. 在VPC中使用lambda时,需要考虑以下因素。

  • The Lambda execution role needs to be able to create, describe and delete network interfaces. Lambda执行角色需要能够创建,描述和删除网络接口。
  • The Lambda function and the RDS instances need to be in the same VPC. Lambda函数和RDS实例必须位于同一VPC中。
  • The Lambda function and the RDS instances need to be in the same subnets. Lambda函数和RDS实例必须位于同一子网中。
  • The Lambda function and the RDS instances need to share a security group. Lambda函数和RDS实例需要共享一个安全组。
  • That security group needs to have a rule allowing traffic from other entities in the same security group. 该安全组需要有一个规则,允许来自同一安全组中其他实体的流量。
  • The subnet needs to have spare address space to allow lambda to create Elastic Network Interfaces. 子网需要具有备用地址空间,以允许lambda创建弹性网络接口。

    Source Amazon Web Services Support 来源Amazon Web Services支持

2. Node.js Callbacks 2. Node.js回调

The second was basically callback hell in node.js 第二个基本上是node.js中的回调地狱

As the function was being called from Alexa intent it was executing the the content.success before the callback had fired. 由于从Alexa Intent调用了该函数,因此在触发回调之前,它正在执行content.success。 This was then causing a timeout the next time the code executed due to it still being busy executing the previous command. 然后,由于它仍在忙于执行前一个命令,因此下一次执行代码时将导致超时。 I solved this by making all the callbacks nested and generating the SpeechletResponse in the callback. 我通过嵌套所有回调并在回调中生成SpeechletResponse来解决了这个问题。

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

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