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.

  • Set up a Network Peering Connection between your Atlas cluster and your AWS Lambda function.


    If you do not set up a Network Peering connection, you must add to the access list for your Atlas cluster. In addition, you will be charged for communication between Atlas and AWS.


Lambda has made major strides such that even cold starts where the Lambda function is invoked for the first time today typically happen in under 1 second. 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.