Deploy MERN Stack in AWS with Amazon DocumentDB 2 min read | 13th Mar 25
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:
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
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:
Step 4: Deploying Your MERN App to AWS
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