Jim
Jim

Reputation: 1608

Lambda get data from mysql RDS but timeout

I am using Lambda connect to RDS with VPC and work fine. I can get data from mysql successfully but Lambda will timeout.

CloudWatch log:

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

Upvotes: 1

Views: 1258

Answers (1)

Jim
Jim

Reputation: 1608

I finally find out that pool should be end. And Lambda work find.

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

One more thing is that the mysql createConnection function have to run every time when lambda start. Here is Ref.

Upvotes: 3

Related Questions