• Documentation
  • Pricing
© 2026 Serverless, Inc. All rights reserved.

Framework

  • Overview
  • Documentation
  • Plugins360
  • Pricing

Learn

  • Blog
  • GuidesUpdated
  • Examples240
  • Courses

Resources

  • Support
  • Security
  • Trust Center
  • Status

Community

  • Slack
  • GitHub47k
  • Forum
  • Meetups

Company

  • About
  • Careers
  • Contact
  • Partners

Legal

  • Terms of Service
  • Privacy Policy
  • Trademark
  • DMCA
Matt Welke

Node.js AWS Lambda connecting to MongoDB Atlas

by

Shows how to connect AWS Lambda to MongoDB Atlas.

  1. Node.js AWS Lambda connecting to MongoDB Atlas

aws-node-mongodb-atlas

An example app, created in this blog post, showing how to connect AWS Lambda to MongoDB Atlas, which must be configured with a user with read/write privileges and an IP whitelist to allow Lambda to connect to it. See blog post for detailed walkthrough setting up MongoDB Atlas.

The MongoDB connection is cached at module scope so warm Lambda invocations reuse it instead of reconnecting on every request.

Setup

  1. Set the MONGODB_URI environment variable in serverless.yml to your MongoDB Atlas connection string.

    provider:
      environment:
        MONGODB_URI: mongodb+srv://<user>:<password>@<cluster>.mongodb.net/<db>?retryWrites=true
    
  2. Install the dependencies and deploy

    npm install
    serverless deploy
    

Usage

curl https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/hello
{
  "insertedPet": { "type": "cat", "name": "Jane Doe" },
  "mongoResult": { "acknowledged": true, "insertedId": "..." }
}

Contents

  • aws-node-mongodb-atlas
  • Setup
  • Usage

Related

GuidesPluginsExamplesSlack CommunitySupport