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.
Before proceeding with this guide, ensure you have the following:
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.
10.0.0.0/16), and select an availability zone.# 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}]'
Security groups control inbound and outbound traffic to your DocumentDB instances.
# 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
You can create a DocumentDB cluster using the AWS Management Console or 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
A subnet group is required to associate your subnets with the DocumentDB cluster.
# 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"
After creating the cluster, add instances to it.
# 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
You can retrieve the connection string from the AWS Management Console or using the AWS CLI.
# Describe Cluster
aws docdb describe-db-clusters --db-cluster-identifier my-docdb-cluster --query 'DBClusters[*].Endpoint'
Use the mongo shell to connect to your DocumentDB cluster.
mongo "mongodb://admin:<PASSWORD>@<ENDPOINT>:27017/?ssl=true&replicaSet=rs0"
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.