Connecting to a MongoDB Cluster on AWS from an EC2 Instance in the Same VPC Using SSH Tunneling with MongoDB compass
In the realm of current software improvement, MongoDB has grow to be a popular desire for handling and storing statistics. If you’re utilizing a MongoDB cluster hosted on AWS and want to hook up with it from an EC2 instance within the equal Virtual Private Cloud (VPC), this manual will walk you through the procedure the usage of SSH tunneling with MongoDB Compass.
Prerequisites:
Before diving into the steps, ensure you have the following prerequisites in place:
- An AWS account with a MongoDB cluster deployed.
- An EC2 instance within the same VPC as the MongoDB cluster.
- MongoDB Compass installed on your local machine.
Step 1: Launch an EC2 instance
Step 2: Configure Security Groups:
Start by configuring the security groups for both your MongoDB cluster and EC2 instance. Ensure that the necessary ports are open for communication between the two entities. Allow inbound traffic on the MongoDB port (default is 27017) and SSH port (default is 22).
Step 3: Set Up SSH Key Pair:
Connect to your EC2 instance using an SSH key pair. If you don’t have one, create and associate a key pair during the EC2 instance creation process.
# For Linux and macOS users
ssh -i /path/to/your/key.pem ec2-user@your-ec2-instance-ip
Step 4: Install MongoDB Tools:
On your EC2 instance, install the MongoDB tools necessary for connecting to your MongoDB cluster. Run the following commands:
Amazon Linux
# Configure the package management system (yum)
sudo vi /etc/yum.repos.d/mongodb-org-7.0.repo
# For Amazon Linux 2023 paste the below contents
[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2023/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc
# For Amazon Linux 2 copy and Paste below into
[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc
# save and close the file
# install mongodb
sudo yum install -y mongodb-org
Ubuntu/Debian
sudo apt-get update
sudo 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
# Create the /etc/apt/sources.list.d/mongodb-org-7.0.list file for Ubuntu 22.04 (Jammy):
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" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
# start mongod
sudo systemctl start mongod
# verify if mongod is running
sudo systemctl status mongod
Step 5: Connect to MongoDB:
Open MongoDB Compass on your local machine. Connect to the MongoDB server using the address “mongodb host” and port “27017”. Use the connection credentials configured for your MongoDB cluster on AWS.
# Download the Amazon DocumentDB Certificate Authority (CA) certificate required to authenticate to your cluster
wget https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem
You will have to configure:
- connection string -> copy and paste your mongodb into Host text input
2. Configure Authentication(input your username and password on the Auth tab leave everything else empty)
3. Upload the CA pem file we downloaded earlier under TLS/SSL
4. Configure your ssh-tunneling which is essentially your EC2 instance ssh details.
All done click on connect your mongoDB should be connected.
By following these steps, you’ve got correctly established a stable connection on your MongoDB cluster hosted on AWS from an EC2 instance in the identical VPC. This approach guarantees statistics integrity and privacy whilst utilising the ability and scalability of MongoDB Atlas in the cloud. Happy days!🥳
Did I make your day somehow?
You can buy me a coffee using this link https://buymeacoffee.com/sakhilesibuyi thank you 🙏