MongoDB to DocumentDB Migration via AWS DMS — Part 1 VPC and MongoDB Setup

Mahmood Rahimi
4 min readOct 8, 2023

--

Welcome to the first part of our blog series on migrating MongoDB to Amazon DocumentDB (DocumentDB) using the AWS Database Migration Service (DMS). This part will guide you through creating a Virtual Private Cloud (VPC) using Terraform and setting up MongoDB on an Amazon Elastic Compute Cloud (EC2) instance. This is an essential step in the migration process, as it provides the groundwork for both your source (MongoDB) and target (DocumentDB) databases. If you found this blog, you probably already have a VPC, MongoDB, and a DocumentDB instance you want to migrate. Still, if you create a test environment to do everything from start to finish, you can start from the beginning or skip to where you need to.

Prerequisites

Before you begin, make sure you have the following prerequisites in place:

  1. Basic knowledge of AWS services, including Amazon EC2 and Amazon VPC.
  2. An AWS account with appropriate permissions to create VPC resources, EC2 instances, and DMS instances.
  3. Basic knowledge of running terraform code.

Start by going over to GitHub and cloning the Simple-VPC repository. Update the variables.tf and change the ENV on line 32. The terraform code will create a VPC with six Subnets, three public and three private. It will create an IGW with three NATs.

git clone https://github.com/mahmoodr786/simple-vpc.git
cd simple-vpc
terraform init
terraform apply

Your VPC, Subnets and your routes tables should be created.

Now, let's get our MongoDB EC2 instance spun up and set it up. I will be using Ubuntu 22.04 and installing MongoDB Community edition. Let's create our EC2 instance.

Pick the private AZ; no keypair is needed as we will use SSM to connect to the instance.

Give 50GB of space and make sure your instance is encrypted. It is good security practice to always encrypt, even for testing purposes.

Launch instance. Once it is up and running, connect via SSM.

Once connected, elevate your privileges using the sudo su command.

sudo su
apt-get update

The steps below are straight from the MongoDB documentation.

apt-get install gnupg curl
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-7.0.list
apt-get update
apt-get install -y mongodb-org

Before we start the service, we need to update the /etc/mongod.conf and change the bind IP to 0.0.0.0 from 127.0.0.1 to connect to within the VPC.

service mongod start
wget -qO- https://www.mongodb.org/static/pgp/server-7.0.asc | sudo tee /etc/apt/trusted.gpg.d/server-7.0.asc

MongoShell should be automatically installed. Run the command below to get connected.

mongosh

Finally, let's create a DB called DMS, create some collections(Tables), and insert some fake or sample data.

//Create db

use dms

// Create a "users" collection
db.createCollection("users")

// Insert test data into the "users" collection
db.users.insertMany([
{
name: "John Doe",
email: "john@example.com",
age: 30
},
{
name: "Jane Smith",
email: "jane@example.com",
age: 25
},
{
name: "Bob Johnson",
email: "bob@example.com",
age: 35
}
])

Following the above example, I created a few more collections.

In conclusion, In this first part of our tutorial, we accomplished several vital tasks to lay the foundation for our MongoDB to DocumentDB migration via DMS. We created a Virtual Private Cloud (VPC) using Terraform, sourced from my GitHub repository. We launched an Amazon Elastic Compute Cloud (EC2) instance running Ubuntu 22.04, which will host our MongoDB database. Leveraging AWS Systems Manager (SSM), we securely SSH'd into our EC2 instance and installed MongoDB Community Edition. Furthermore, we created a collection and populated it with sample data.

Part 2 of this tutorial will continue our journey by setting up Amazon DocumentDB and the AWS Database Migration Service (DMS). We will initiate the migration process and ensure the successful transfer of our collections from MongoDB to DocumentDB. Additionally, we will explore some of the key differences and limitations of DocumentDB compared to MongoDB to ensure a smooth transition.

--

--

Mahmood Rahimi

I'm a DevSecOps Engineer with 13 years of experience in development, operations, security, and cloud.