Setting Up MongoDB Atlas Cluster and Creating VPC Peering in AWS
Tags: mongodb, aws, networking, devops
Published: 2023-10-02 15:42:35

**MongoDB Atlas** is a fully managed cloud service for MongoDB. It provides a simple and cost-effective way to deploy, manage, and scale MongoDB databases.

**AWS Virtual Private Cloud (VPC) peering** allows you to connect your Atlas cluster to your VPC. This provides a private and secure connection between your Atlas cluster and your other AWS resources.

In this blog post, we will show you how to set up a MongoDB Atlas cluster and create VPC peering in AWS.

## **Prerequisites**

Before getting started, you will need to have the following prerequisites:

- An AWS account
- A MongoDB Atlas account

## **Setting up a MongoDB Atlas cluster**

To set up a MongoDB Atlas cluster, follow these steps:

**Step 1: Create a MongoDB Atlas Cluster**
1. Log in to your MongoDB Atlas account.
2. Click on "Clusters" in the left sidebar.
3. Click the "Build a Cluster" button to create a new cluster.
4. Choose the M10 cluster tier, which is a recommended starting point for most use cases.
5. Configure your cluster settings, including:
  - Cluster name
  - Cloud provider (AWS)
  - Region
  - Cluster tier
  - Additional settings (authentication, backup, etc.)
6. Click the "Create Cluster" button to initiate the cluster creation process.

![setup-atlas-database-01](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wy5bb5cjq0k3yum4fs08.png)

![setup-atlas-database-02](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/viu0eyquxrcre1p18ywv.png)

![setup-atlas-database-03](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5p63o5e8vf4jjp4gr7v.png)

![setup-atlas-database-04](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pp88084xfno02zl1khtx.png)

![setup-atlas-database-05](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txv9c5ewaosbbsfcosmh.png)

![setup-atlas-database-06](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gasstfqudiud8ddhks6j.png)


**Step 2: Configure Network Access**
To secure your MongoDB Atlas cluster, you need to configure network access:
1. In the MongoDB Atlas dashboard, navigate to "Network Access" under the "Security" section.
2. Click the "Add IP Address" button to whitelist the VPC CIDR block for secure access.


![network-access-01](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/th75sc56p44lm6ongs43.png)

**Step 3: Create a Peering Connection in AWS**
This VPC peering allows private network communication between your resources in AWS and MongoDB Atlas cluster.
1. Log in to your AWS Management Console.
2. Navigate to the Amazon VPC dashboard.
3. In the left sidebar, click on "Peering Connections" under the "Peering" section.
4. Click the "Create Peering Connection" button.
5. Configure the peering connection as follows:
  - Peering connection options: Select "Requester" (Your AWS VPC) and "Accepter" (MongoDB Atlas).
  - Requester VPC: Choose your AWS VPC.
  - Accepter VPC: Choose "Another AWS account" and provide the Atlas VPC details provided in the MongoDB Atlas dashboard.
  - Provide a unique name for the peering connection.
6. Click the "Create Peering Connection" button.
7. In the peering connection details, click the "Actions" button and select "Accept Request" to approve the peering request in the MongoDB Atlas account.

![vpc-peering-01](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kpnk4cmv3iojcmyhtno.png)

![vpc-peering-02](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ljwmj3fqmp9em61dkwf.png)

![vpc-peering-03](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10kie8ybi2wkdbchbt2p.png)

![vpc-peering-04](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oouh22c6iqp927y476x6.png)

**Step 4: Configure Route Tables**
To allow traffic to flow between your AWS VPC and MongoDB Atlas VPC, you need to update the route tables:
1. In the AWS VPC dashboard, navigate to "Route Tables."
2. Edit the route table associated with your AWS VPC.
3. Add a new route with the destination CIDR block of the MongoDB Atlas VPC, pointing to the peering connection as the target.
4. Save the changes.


![route-table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wctssww7tli2aj3dzisv.png)

**Step 5: Test Connectivity**
To ensure that the VPC peering connection is working correctly, test the connectivity between resources in your AWS VPC and your MongoDB Atlas cluster. Created a simple Python script on AWS Lambda that uses a VPC and private subnets to test whether the resources within the VPC can reach the MongoDB Atlas production environment through VPC peering.


![lambda-mongodb-atlas-01](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aympebwlm78yjx81t0jq.png)

![lambda-mongodb-atlas-02](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27pq49898y9bbulqwgi3.png)

## **Conclusion**

In this blog post, we have shown you how to set up a MongoDB Atlas cluster and create VPC peering in AWS. This allows you to connect your Atlas cluster to your VPC and provide a private and secure connection between your Atlas cluster and your other AWS resources.