Frequent timeout with app using Serverless Framework (AWS Lambda/Gateway), Express, Mongoose/MongoDB Atlas Frequent timeout with app using Serverless Framework (AWS Lambda/Gateway), Express, Mongoose/MongoDB Atlas express express

Frequent timeout with app using Serverless Framework (AWS Lambda/Gateway), Express, Mongoose/MongoDB Atlas


Under your provider add timeout, maximum value of timeout in lambda is 900 seconds, place it according to your execution time like 30 seconds and see what happens

provider:  timeout: 30

The error is clearly saying that it's execution exceeded timeout, since you have not configured timeout so it was using default timeout of 3 seconds, hopefully it will solve the issue


The issue is likely due to your open database connection. While this connection is established any calls to callback won't be returned to the client and your function will timeout.

You need to set context.callbackWaitsForEmptyEventLoop to false.

Here is the explanation from the docs:

callbackWaitsForEmptyEventLoop – Set to false to send the response right away when the callback executes, instead of waiting for the Node.js event loop to be empty. If this is false, any outstanding events continue to run during the next invocation.

With serverless-http you can set this option quite easily within your server.js file:

const sls = require('serverless-http')const app = require('./app')module.exports.run = sls(app, { callbackWaitsForEmptyEventLoop: false })