Lambda 从 mysql RDS 获取数据但超时

Lambda get data from mysql RDS but timeout

我正在使用 Lambda 通过 VPC 连接到 RDS 并且工作正常。 我可以从 mysql 成功获取数据,但 Lambda 会超时。

CloudWatch 日志:

2017-02-15T18:56:18.609Z [ RowDataPacket { userInfo: 'xxx'} ]
END RequestId: xxx REPORT RequestId: xxx Duration: 300001.69 ms Billed Duration: 300000 ms Memory Size: 512 MB Max Memory Used: 22 MB
2017-02-15T19:01:18.306Z xxx Task timed out after 300.00 seconds

Handle.js

db.getPersonInfo("xxx", function (err, result) {
    console.log(result);
    const response = {
          statusCode: 200,
          body: JSON.stringify({
            message: 'test',
            input: event,
          }),
        };
    callback(null, response);
});

DB.js

var getPersonInfo = function(userId, callback){
    pool.getConnection(function(err, connection){
      var sql = 'SELECT userInfo FROM user where userId = ?';
      connection.query( sql , userFbId , function(err, results) {
        if(err){
          console.log(err);
        }
        callback(err, results);
        connection.release();
      });
    });
  };

我终于发现pool应该结束了。和 Lambda 工作找到。

var getPersonInfo = function(userId, callback){
    pool.getConnection(function(err, connection){
      var sql = 'SELECT userInfo FROM user where userId = ?';
      connection.query( sql , userId , function(err, results) {
        if(err){
          console.log(err);
        }
        callback(err, results);
        connection.release();
        pool.end(function (err) {
          // all connections in the pool have ended
        });
      });
    });
  };

另一件事是 mysql createConnection 函数必须在每次 lambda 启动时 运行。这里是 Ref.