1

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Once you invoke end(..) function on pool instance, you need to re-initialise it. Otherwise, it will fail.
I understand the callback is basically stopping execution, so I had callback after pool.end otherwise we were hitting the number of connections limit all the time, then I had callback after pool.end, and from time to time we were getting PROTOCOL_SEQUENCE_TIMEOUT, as pool.end is basically a promise. So I moved callback inside pool.end, now seems to be ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.