Best Practices Connecting from AWS Lambda

On this page

MongoDB recommends the following best practices to optimize connection times between AWS Lambda and Atlas:

  • Define the MongoDB client connection to the MongoDB server outside the AWS Lambda handler function.

    This makes the database connection available between invocations of the AWS Lambda function for the duration of the lifecycle of the function.

  • Set the callbackWaitsForEmptyEventLoop on the AWS Context object to false.

    By default, the callback waits until the runtime event loop is empty before freezing the process and returning the results to the caller. Setting this property to false requests that AWS Lambda freeze the process soon after the callback is invoked, even if there are events in the event loop. AWS Lambda will freeze the process, any state data, and the events in the event loop. Any remaining events in the event loop are processed when the Lambda function is next invoked, if AWS Lambda chooses to use the frozen process.


There is a small initial startup cost of approximately 5 to 10 seconds when the Lambda function is invoked for the first time and the MongoDB client in your AWS Lambda function connects to MongoDB. Connections to a mongos for a sharded cluster are faster than connecting to a replica set. Subsequent connections will be significantly faster for the duration of the lifecycle of the Lambda function.


The following Node.js example demonstrates these best practices:

"use strict";
const MongoClient = require('mongodb').MongoClient;
const MONGODB_URI = process.env.MONGODB_URI; // or Atlas connection string

let cachedDb = null;

function connectToDatabase (uri) {
  console.log('=> connect to database');

  if (cachedDb) {
    console.log('=> using cached database instance');
    return Promise.resolve(cachedDb);

  return MongoClient.connect(uri)
    .then(db => {
      cachedDb = db;
      return cachedDb;

function queryDatabase (db) {
  console.log('=> query database');

  return db.collection('items').find({}).toArray()
    .then(() => { return { statusCode: 200, body: 'success' }; })
    .catch(err => {
      console.log('=> an error occurred: ', err);
      return { statusCode: 500, body: 'error' };

module.exports.handler = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;

  console.log('event: ', event);

    .then(db => queryDatabase(db))
    .then(result => {
      console.log('=> returning result: ', result);
      callback(null, result);
    .catch(err => {
      console.log('=> an error occurred: ', err);

For more information, see Optimizing AWS Lambda performance with MongoDB Atlas and Node.js.