Deploying an AWS DocumentDB instance and accessing it through Mongo Shell using EC2

AWS DocumentDB is a fully managed NoSQL document database service designed for JSON-based workloads. It is compatible with MongoDB and provides high availability, scalability, and security for modern applications.  

However, direct access to AWS DocumentDB from a local machine is not possible due to its VPC-based architecture. To interact with DocumentDB, we use an Amazon EC2 instance as a bridge, allowing us to securely connect via Mongo Shell.  

This guide walks you through the step-by-step process of deploying an AWS DocumentDB instance and accessing it using Mongo Shell from an EC2 instance. By following these steps, you will learn how to: 

Step 1: Creating an EC2 Instance 

To start, you’ll need to create an EC2 instance in AWS, which will serve as a connection point to your AWS DocumentDB instance. The EC2 instance will act as the gateway for your Mongo Shell. 

Instructions: 

  1. Go to the AWS Management Console. 
  2. Navigate to EC2 and click on Launch Instance. 
  3. Select an Amazon Machine Image (AMI), such as Amazon Linux 2 or Ubuntu, depending on your preference. 
  4. Choose an instance type (e.g., t2.micro for free tier users). 
  5. Configure the instance settings, such as VPC, subnet, and assign a public IP if needed. 
  6. Set up security groups that allow SSH access (port 22) from your local machine’s IP address. 

Step 2: Creating an AWS DocumentDB Instance in the Same VPC 

Once your EC2 instance is ready, you can create a DocumentDB instance in the same VPC to ensure that it can be accessed securely. 

Instructions: 

  1. Go to the DocumentDB section in the AWS Management Console. 
  2. Click Create database. 
  3. Choose the instance size and other configurations (e.g., replication, storage). 
  4. Make sure the AWS DocumentDB instance is placed in the same VPC as the EC2 instance. 
  5. Set up security groups for the DocumentDB instance to allow connections from the EC2 instance’s security group (you’ll need the security group ID of the EC2 instance here). 
  6. Take note of the endpoint of your DocumentDB instance, which will be used later in the connection process. 

Step 3: Connect to EC2 Using Your Windows Machine (via Terminal) 

To SSH into your EC2 instance from your Windows machine, you can use Windows Subsystem for Linux (WSL) or Git Bash. Both options will give you the ability to run the SSH command directly from your terminal. 

Using Git Bash: 

Install Git Bash: 

Prepare your PEM key: 

chmod 400 /c/Users/YourUsername/Documents/keys/my-key.pem 

SSH into EC2: 

ssh -i /c/Users/YourUsername/Documents/keys/my-key.pem ec2-user@<ec2-public-ip> 

Accept the connection: 

Once you’ve successfully logged in, you will have terminal access to your EC2 instance. 

Using Windows Subsystem for Linux (WSL): 

If you’re using WSL, the steps are similar: 

Install WSL (if you don’t already have it): 

Use SSH: 

ssh -i /mnt/c/Users/YourUsername/Documents/keys/my-key.pem ec2-user@<ec2-public-ip> 

Now you’re connected to your EC2 instance directly from your Windows machine via the terminal, bypassing the need for PuTTY.

Step 4: Access DocumentDB and Query Using Mongo Shell 

Now that you’re connected to your EC2 instance, the next step is to access the AWS DocumentDB instance through Mongo Shell. 

Instructions: 

sudo yum install -y mongo-tools 

mongo –ssl –host docdb-endpoint –port 27017 –username <your-username> –password <your-password> –authenticationDatabase admin 

Conclusion 

By following these steps, you will have successfully deployed a AWS DocumentDB instance in AWS and accessed it using Mongo Shell via an EC2 instance. This setup is common in scenarios where direct internet access to your database is not allowed, and EC2 serves as a secure proxy. 

If you need further help, you can contact us at info@xavor.com. We will schedule a free consultation session to explore how Xavor can assist you.

[og_img]
2025-03-05 00:55:27

Exit mobile version