Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda function never calls the callback

I've created a node lambda function that does a simple call to an Aurora database. When I test the function in the console, the query returns, I can see the results in the log, but the callback never seems to get called and so my lambda function times out. I can't figure out what the problem is. Hopefully someone here can point me to the problem.

var mysql = require("mysql");

module.exports.handler = function(event, context, cb) {
  console.log('start\n');
  var con = mysql.createConnection({
    ...
  });
  console.log('call data\n');

  con.query('SELECT * FROM Tags', function(err, rows) {
    console.log('Data received from Db:\n');
    console.log(rows);

    console.log('calling callback');

    cb(null, 'Success');

    console.log('callback called');
  });
  console.log('data called\n');
};

The resulting Cloudwatch log is as follows...

2016-07-25T14:20:05.343Z    daf5cd6b-5272-11e6-9036-e73ad17006df    start  
2016-07-25T14:20:05.398Z    daf5cd6b-5272-11e6-9036-e73ad17006df    call data  
2016-07-25T14:20:05.405Z    daf5cd6b-5272-11e6-9036-e73ad17006df    data called  
2016-07-25T14:20:05.440Z    daf5cd6b-5272-11e6-9036-e73ad17006df    Data received from Db:  
2016-07-25T14:20:05.440Z    daf5cd6b-5272-11e6-9036-e73ad17006df    [ 
    RowDataPacket {
        id: 1,
        externalId:
        'a87ead34de7e',
        orgId: 1,
        name: 'lacinia sapien',
        createdDate: 1448598369,
        modifiedDate: 0
    },
    ...,
    RowDataPacket {
        id: 50,
        externalId: '9ebaaab372e3',
        orgId: 1,
        name: 'et commodo',
        createdDate: 1451551837,
        modifiedDate: 0
    }
]
2016-07-25T14:20:05.483Z    daf5cd6b-5272-11e6-9036-e73ad17006df    calling callback 
2016-07-25T14:20:05.483Z    daf5cd6b-5272-11e6-9036-e73ad17006df    callback called 
END RequestId: daf5cd6b-5272-11e6-9036-e73ad17006df 
REPORT RequestId: daf5cd6b-5272-11e6-9036-e73ad17006df  Duration: 300000.12 ms  Billed Duration: 300000 ms Memory Size: 1024 MB Max Memory Used: 52 MB   
2016-07-25T14:25:05.341Z daf5cd6b-5272-11e6-9036-e73ad17006df Task timed out after 300.00 seconds 
like image 930
mp2526 Avatar asked Jul 25 '16 14:07

mp2526


People also ask

Is a Lambda function a callback function?

By that definition, the lambda functions passed into an Array. prototype method are callback functions.

How do you stop Lambda invocation?

You cannot kill a running lambda function. Though there is another way by which you can set concurrency limit to 0. This will stop it from starting any more executions.

What is the most likely issue with the Lambda function timeout?

Finding the root cause of the timeout. There are many reasons why a function might time out, but the most likely is that it was waiting on an IO operation to complete. Maybe it was waiting on another service (such as DynamoDB or Stripe) to respond.

What are the limitations of Lambda functions in AWS?

Technical LimitationsThe maximum time a function can run is 15 minutes, and the default timeout is 3 seconds. Obviously, this makes Lambda unsuitable for long-running workloads. The payload for each invocation of a Lambda function is limited to 6MB, and memory is limited to just under 3GB.


1 Answers

Thanks to this question...

Lambda Timing out after calling callback

I found the problem. the Node mysql module keeps the connection open until the server closes it unless it is explicitly closed by the handler logic.

So the node event loop never empties and so never returns the callback. In the above code, I did a ...

con.end();

before calling the callback and it worked.

like image 84
mp2526 Avatar answered Oct 23 '22 13:10

mp2526