DEV Community

Cover image for Setting Up MongoDB Atlas Cluster and Creating VPC Peering in AWS
Jobs
Jobs

Posted on

Setting Up MongoDB Atlas Cluster and Creating VPC Peering in AWS

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

setup-atlas-database-02

setup-atlas-database-03

setup-atlas-database-04

setup-atlas-database-05

setup-atlas-database-06

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

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

vpc-peering-02

vpc-peering-03

vpc-peering-04

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

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

lambda-mongodb-atlas-02

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.

Top comments (0)