AWS Lambda function never calls a callback

I created a lambda node function that performs a simple call to the Aurora database. When I test the function in the console, the request returns, I can see the results in the log, but the callback never calls the call, and so my lambda function expires. I can’t understand what the problem is. Hope 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 
+6
source share
1 answer

Thanks to this question ...

Lambda time after callback call

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

Thus, the Node event loop never empties and therefore never returns a callback. In the above code, I did ...

 con.end(); 

before calling the callback, and it worked.

+18
source

All Articles