codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

59 / 65 topics
58Cloud Deployments59AWS MongoDB60Azure Cosmos DB61Google Cloud Platform62MongoDB Atlas63On-Premises Deployments64High Availability Strategies65Disaster Recovery Plans
Tutorials/MongoDB/AWS MongoDB
🍃MongoDB

AWS MongoDB

Updated 2026-04-20
3 min read

Introduction

Amazon Web Services (AWS) offers a robust and scalable solution for hosting MongoDB databases through Amazon DocumentDB, a fully managed service that provides compatibility with the popular NoSQL database. This tutorial will walk you through the process of deploying and managing an AWS MongoDB-compatible database using Amazon DocumentDB.

Prerequisites

Before proceeding with this guide, ensure you have the following:

  • An active AWS account.
  • Basic understanding of MongoDB concepts.
  • AWS CLI installed and configured on your local machine.
  • IAM user with permissions to create and manage DocumentDB resources.

Setting Up Your Environment

Step 1: Create a VPC and Subnet

Amazon DocumentDB requires a Virtual Private Cloud (VPC) and at least one subnet within that VPC. You can use the AWS Management Console or AWS CLI to set up these resources.

Using AWS Management Console

  1. Navigate to the VPC Dashboard: Go to the Amazon VPC console.
  2. Create a VPC: Click on "Your VPCs" and then "Create VPC". Provide a name tag, IPv4 CIDR block (e.g., 10.0.0.0/16), and select an availability zone.
  3. Create Subnets: After creating the VPC, create subnets within it. Ensure they are in different availability zones for high availability.

Using AWS CLI

# Create a VPC
aws ec2 create-vpc --cidr-block 10.0.0.0/16 --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=my-docdb-vpc}]'

# Create Subnets
aws ec2 create-subnet --vpc-id <VPC_ID> --cidr-block 10.0.1.0/24 --availability-zone us-west-2a --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=my-docdb-subnet-a}]'
aws ec2 create-subnet --vpc-id <VPC_ID> --cidr-block 10.0.2.0/24 --availability-zone us-west-2b --tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=my-docdb-subnet-b}]'

Step 2: Configure Security Groups

Security groups control inbound and outbound traffic to your DocumentDB instances.

  1. Create a Security Group: In the VPC dashboard, create a new security group.
  2. Add Rules: Add rules to allow inbound traffic on port 27017 (MongoDB default) from your IP address or specific CIDR blocks.

Using AWS CLI

# Create Security Group
aws ec2 create-security-group --group-name my-docdb-sg --description "Security group for DocumentDB" --vpc-id <VPC_ID>

# Add Inbound Rule
aws ec2 authorize-security-group-ingress --group-id <SECURITY_GROUP_ID> --protocol tcp --port 27017 --cidr <YOUR_IP>/32

Deploying Amazon DocumentDB

Step 1: Create a Cluster

You can create a DocumentDB cluster using the AWS Management Console or AWS CLI.

Using AWS Management Console

  1. Navigate to DocumentDB Dashboard: Go to the Amazon DocumentDB console.
  2. Create Cluster: Click on "Clusters" and then "Create Cluster".
  3. Configure Settings: Choose the instance class, number of instances, VPC, subnets, and security group. Enable encryption if required.

Using AWS CLI

# Create a Cluster
aws docdb create-db-cluster --db-cluster-identifier my-docdb-cluster --engine documentdb --master-username admin --master-user-password <PASSWORD> --vpc-security-group-ids <SECURITY_GROUP_ID> --db-subnet-group-name my-docdb-subnet-group

Step 2: Create a Subnet Group

A subnet group is required to associate your subnets with the DocumentDB cluster.

Using AWS CLI

# Create Subnet Group
aws docdb create-db-subnet-group --db-subnet-group-name my-docdb-subnet-group --subnet-ids <SUBNET_ID_A> <SUBNET_ID_B> --db-subnet-group-description "Subnet group for DocumentDB"

Step 3: Add Instances to the Cluster

After creating the cluster, add instances to it.

Using AWS CLI

# Add Instance to Cluster
aws docdb create-db-instance --db-cluster-identifier my-docdb-cluster --db-instance-identifier my-docdb-instance --db-instance-class db.t3.medium --engine documentdb

Connecting to Your DocumentDB Cluster

Step 1: Retrieve Connection String

You can retrieve the connection string from the AWS Management Console or using the AWS CLI.

Using AWS CLI

# Describe Cluster
aws docdb describe-db-clusters --db-cluster-identifier my-docdb-cluster --query 'DBClusters[*].Endpoint'

Step 2: Connect Using MongoDB Shell

Use the mongo shell to connect to your DocumentDB cluster.

mongo "mongodb://admin:<PASSWORD>@<ENDPOINT>:27017/?ssl=true&replicaSet=rs0"

Best Practices

  • Security: Always use SSL/TLS for connections to ensure data is encrypted in transit.
  • Backup and Restore: Enable automated backups and test the restore process regularly.
  • Monitoring: Use Amazon CloudWatch to monitor your DocumentDB cluster's performance and health.
  • Scaling: Scale your cluster by adding more instances or changing instance types based on workload requirements.

Conclusion

This tutorial has covered the essential steps for deploying and managing an AWS MongoDB-compatible database using Amazon DocumentDB. By following these guidelines, you can set up a secure, scalable, and high-performance MongoDB environment in the cloud.

For further reading, refer to the Amazon DocumentDB documentation and explore additional features like sharding and global tables for enhanced scalability and availability.


PreviousCloud DeploymentsNext Azure Cosmos DB

Recommended Gear

Cloud DeploymentsAzure Cosmos DB