Why does my AWS Lambda function keep timing out?

The short answer: Just set event.callbackWaitsForEmptyEventLoop = false.

exports.handler = async (event, context) => {
  context.callbackWaitsForEmptyEventLoop = false;
  ...
};

Why does this matter?

The default behavior of a NodeJS runtime in AWS Lambda is to wait for the javascript event loop to be empty before ending the execution of the lambda and returning a value. You can read more about how AWS Lambda and the node.js event loop works here.

So what is happening with your lambda function currently is that the connection to Redis is keeping the event loop open, thus preventing your function from ever ending the execution successfully.


Or you can close the Redis connection.