Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why AWS Lambda keeps timing out when using Knex.js

I have a AWS Lambda function that connects to a RDS instance using the Knex.js NPM module. After I get the data from the DB, and call the callback() function, the AWS Lambda won’t exit, won’t send back a response and will time out.

If I console.log() the result from the DB I see the data, meaning the retrieval of the information from the DB was successful.

like image 733
David Gatti Avatar asked Sep 15 '25 21:09

David Gatti


1 Answers

Turns out that by default AWS Lambda will wait for the event loop to be empty before terminating the execution of the function. This means that even if you call the callback() function, AWS Lambda won't quit. To change this behavior you need to set callbackWaitsForEmptyEventLoop to false. This tells AWS Lambda to quit even when the event loop is not empty, which in this case it is our DB connection.

context.callbackWaitsForEmptyEventLoop = false;
like image 114
David Gatti Avatar answered Sep 17 '25 15:09

David Gatti