DEV Community

Cover image for Migrating from MongoDB to Amazon DocumentDB
Ismail G.
Ismail G.

Posted on

Migrating from MongoDB to Amazon DocumentDB

Modern applications today often use document databases. For years, MongoDB has been the preferred choice for developers to build applications using JSON-like document data structures. However, a move to a fully managed service like Amazon DocumentDB is attractive when workloads increase.

Built from the ground up, Amazon DocumentDB (with MongoDB compatibility) is highly available, robust, and scalable. It supports common MongoDB drivers and tools, making it easy to move teams without changing application code. This article will guide you step-by-step through migrating data from MongoDB to Amazon DocumentDB using the AWS Database Migration Service (DMS).

Prerequisites

Before you begin the migration, make sure you have the following:

An Amazon DocumentDB cluster already created and available in your AWS account.

We will use AWS Database Migration Service (DMS) to migrate data from a MongoDB database to Amazon DocumentDB. This will work with minimal downtime and will not require the export and import of collections manually.

Create an AWS DMS Replication Instance for MongoDB Migration

The replication instance is responsible for the actual migration process. It establishes a connection to the source and target endpoints, extracts the data, transforms it (if necessary), and then inserts it into the destination.

  • Access the AWS DMS Console and navigate to the Replication instances section.

Image description

  • Select "Create replication instance."

Image description

  • Pick an instance identifier and select an instance class, such as dms.t3.medium.
  • Select the appropriate virtual private cloud (VPC). This must be the same VPC that your DocumentDB cluster refers to as its home.
  • Multi-AZ should be activated when exceptional availability is required.
  • Select the "Create" button and await the "Available" status of the instance.

Create Source and Target Endpoints for MongoDB Migration

Once the replication instance is ready, create source and target endpoints to define where the data will be moved from and to.

Source Endpoint (MongoDB):

Image description

  • Endpoint type: Source
  • Engine: MongoDB
  • Server name: MongoDB hostname or IP address
  • Port: 27017
  • Database name: your MongoDB database (e.g., zips-db)
  • Authentication mode: default
  • Username and password: credentials for your MongoDB instance

Target Endpoint (Amazon DocumentDB):

Image description

  • Endpoint type: Target
  • Engine: MongoDB
  • Server name: your DocumentDB cluster endpoint (e.g., mydocdbcluster.cluster-xxxxxx.docdb.amazonaws.com)
  • Port: 27017
  • Database name: target database name
  • Authentication: enter your DocumentDB admin username and password
  • TLS mode: verify-full
  • TLS CA file: upload global-bundle.pem

To add TLS CA file,first download the global CA certificate. You can find the TLS certificate download command directly in the Amazon DocumentDB console under your cluster's Connectivity & security tab.

Image description

After both endpoints are created, test the connections to verify that DMS can reach each database.

Image description

Create and Run a MongoDB Migration Task

Now, you can define and launch the migration task.

In the DMS Console, go to Database migration tasks and click Create task.

Image description

Choose a task identifier, and select your previously created replication instance.

Image description
For migration type, choose one of the following:

Migrate existing data only

Image description

Important: Turn off data validation. This feature is not supported for MongoDB endpoints.

Under Table mappings, click Add new selection rule (you must select at least one) :

Image description
Schema: your MongoDB database name (e.g., zips-db)

Source table name: %

Action: Include

Choose to start the task automatically on create.

Once created, the task will begin migrating your data to Amazon DocumentDB. You can monitor progress via the DMS console and view logs for detailed insights.

Image description

Wrapping Up

AWS DMS makes migrating from MongoDB to Amazon DocumentDB a simple task. Following the procedures outlined above will help you to reduce downtime and transfer your document-based workloads to a completely controlled environment that grows with your requirements. DocumentDB gives you the AWS security, scalability, and dependability advantages without compromising MongoDB compatibility.

This move can be the next step in developing your architecture if you are thinking about integration with other AWS services, operational simplicity, and long-term maintenance.

Top comments (0)