Blogarrow-back Back

Deploying a MERN Stack Application in AWS with Amazon DocumentDB

blog writter

Admin

Deploy MERN Stack in AWS with Amazon DocumentDB 2 min read | 13th Mar 25

Issues fixing Support

The MERN stack (MongoDB, Express, React, and Node.js) is a popular technology stack for developing full-stack web applications. Traditionally, MongoDB is used as the database component, but when deploying on AWS, you might need to use Amazon DocumentDB instead of MongoDB. Since Amazon DocumentDB is not a direct replacement for MongoDB but is API-compatible with it, a common question arises:

"Can I use my existing MongoDB-based MERN application with Amazon DocumentDB without rewriting the code?"

If you are facing this challenge, this guide will help you deploy your MERN application in AWS with Amazon DocumentDB while ensuring minimal changes to your codebase.

 

Understanding Amazon DocumentDB Compatibility with MongoDB

Amazon DocumentDB is designed to be MongoDB API-compatible, which means most of your existing MongoDB commands and queries should work without modifications. However, there are some limitations and differences that you need to be aware of before deploying.

Key Differences Between MongoDB and Amazon DocumentDB

While Amazon DocumentDB supports many MongoDB methods and commands, there are notable differences:

  • Admin Databases and Collections: Limited support for certain administrative operations.
  • Field Name Restrictions: Some field names are reserved and cannot be used.
  • Index Builds: Index creation and handling differ slightly.
  • Result Ordering: Query result order might not always be the same.
  • MongoDB APIs and Data Types: Some features like cursormaxTimeMS, explain(), $lookup, and $distinct might behave differently.
  • Backup & Restore: The mongodump and mongorestore utilities have limited support.
  • Retryable Writes: Certain retryable writes may not work as expected.

For a complete list of differences, refer to AWS's official What is Amazon DocumentDB (with MongoDB compatibility) - Amazon DocumentDB

 

Step-by-Step Guide to Deploying MERN with Amazon DocumentDB in AWS

Step 1: Setting Up an Amazon DocumentDB Cluster

  1. Sign in to your AWS Console and navigate to Amazon DocumentDB.
  2. Click Create Cluster and configure the required settings: 
    • Choose a suitable instance size based on your workload.
    • Enable VPC and security settings to allow access from your application.
    • Enable TLS encryption for secure communication.
  3. Once created, copy the connection string provided by AWS.

Step 2: Connecting Mongoose to Amazon DocumentDB

Mongoose is a popular ODM (Object Data Modeling) library for MongoDB in Node.js applications. Fortunately, it works with DocumentDB with a few modifications.

Install Required Dependencies

npm install mongoose

Modify Your Database Connection Code

Use the following connection string format in your server.js or database configuration file:

const mongoose = require('mongoose');

const dbURI = 'mongodb://your-cluster-url:27017/myDatabase?ssl=true&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false';

mongoose.connect(dbURI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    tlsCAFile: "/path/to/rds-combined-ca-bundle.pem" // Required for TLS encryption
})
.then(() => console.log("Connected to Amazon DocumentDB"))
.catch(err => console.error("Error connecting to DocumentDB", err));

Make sure to download the RDS CA certificate from AWS and reference its location in the tlsCAFile parameter.

Step 3: Handling Unsupported Features in DocumentDB

Since DocumentDB lacks full MongoDB feature support, you may need to modify certain queries. Below are some common adjustments:

  • Replace retryWrites=true with retryWrites=false in your connection string.
  • Modify $lookup queries: DocumentDB has limited support for $lookup in aggregation.
  • Adjust Indexing Strategies: Sparse and text indexes might require adjustments.

Step 4: Deploying Your MERN App to AWS

  1. Deploy Backend (Express + Node.js)
    • Use EC2, ECS (Docker Containers), or AWS Lambda (for serverless applications).
    • Set up environment variables for DocumentDB connection.
  2. Deploy Frontend (React App)
    • Use Amazon S3 + CloudFront for static hosting.
    • Configure API Gateway or Load Balancer to route traffic to your backend.
  3. Set Up Security & Permissions
    • Modify AWS Security Groups to allow connections from your application.
    • Use AWS Secrets Manager to store sensitive credentials securely.

 

Conclusion

Migrating a MERN stack application to AWS with Amazon DocumentDB is feasible with minimal changes. While DocumentDB provides MongoDB API compatibility, certain features may require adjustments. By following the steps outlined above, you can successfully deploy your MERN application in AWS while leveraging Amazon DocumentDB as your database.

For further information, always refer to the latest AWS documentation and test your application thoroughly before deploying to production.

 

Have Questions?

If you encounter any issues during migration, feel free to raise a ticket and seek solution from Piccosupport – www.piccosupport.com

Recent Blogss