简体   繁体   English

Lambda NodeJS MySQL任务超时

[英]Lambda NodeJS MySQL Task Timed out

I am trying to learn how to connect MySQL using lambda functions in AWS. 我正在尝试学习如何使用AWS中的lambda函数连接MySQL。 I have followed a couple of instructions online and basically ended up with this code: 我已经按照网上的一些说明进行操作,基本上得到了以下代码:



    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);
        });
      });
    };

This is working on my local but when I zip this code and uploaded it as a lambda function, this returns 这在我的本地计算机上有效,但是当我压缩此代码并将其作为lambda函数上传时,将返回

Response: { "errorMessage": "2018-11-13T02:16:10.339Z 0562b432-e6ea-11e8-81ef-bd64aa1af0a4 Task timed out after 30.03 seconds" }

It times out no matter how many seconds I set it to. 无论我将其设置为多少秒,它都会超时。

I have pretty much set everything at default since I am new to all of these but I have added AmazonRDSFullAccess to the role of lambda function. 因为我是所有这些的新手,所以我几乎将所有设置都设置为默认,但是我已将AmazonRDSFullAccess添加到lambda函数的角色。

Does anyone have any idea what may be wrong or missing in my setup? 有人知道我的设置中可能有什么错误或遗漏吗?

Thanks. 谢谢。

After doing some trial and errors, I was able to make it work and what I was missing is that I did not allow All TCP in the inbound of my RDS Security group. 经过一些试验和错误之后,我能够使它工作,而我所缺少的是我不允许RDS Security组的入站中使用All TCP After that, I set it as my lambda function to No VPC , and it was able to query properly. 之后,我将其作为lambda函数设置为No VPC ,并且能够正确查询。

This link: https://dzone.com/articles/querying-rds-mysql-db-with-nodejs-lambda-function and the stack overflow link posted in there (which is this: AWS Lambda RDS connection timeout ) helped me a lot in figuring out what was wrong with my code/setup. 该链接: https : //dzone.com/articles/querying-rds-mysql-db-with-nodejs-lambda-function和其中张贴的堆栈溢出链接(这是: AWS Lambda RDS连接超时 )帮助我找出我的代码/设置出了什么问题。

Here is the final code that I ended up using. 这是我最终使用的最终代码。



    const mysql = require('mysql');
    const pool  = mysql.createPool({
        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((err, connection) => {
        if(err) throw err;

        // Use the connection
        connection.query('SELECT id FROM customer limit 10;', (error, results, fields) => {

          // And done with the connection.
          connection.release();

          // Handle error after the release.
          if (error) callback(error);
          else callback(null,results);
        });
      });
    };

Thanks! 谢谢!

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

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