How to Connect Mongo DB with AWS using ATLAS?
Last Updated :
19 Oct, 2023
The MongoDB Atlas provides the Application deployment solution by MongoDB hosting on AWS. MongoDB Atlas handles operational tasks such as backups, updates, and security configurations.
There are many ways to connect MongoDB with AWS depending on specific use cases and requirements:
- MongoDB Atlas (Fully managed by MongoDB and runs on AWS infrastructure): High availability, scalability, and security are all provided, and it makes database maintenance jobs simpler.
- EC2 instance hosted MongoDB (Manually set up MongoDB on AWS EC2 instance): MongoDB was manually installed on AWS EC2 instances. Set security groups up to let incoming traffic to SSH (port 22) and MongoDB (default port 27017) for secure access. SSH can be used to access your EC2 instance and communicate with MongoDB. gives you full control over your MongoDB deployment.
As we have discussed only two ways but there are many ways, now we are going to see how to connect MongoDB with AWS using ATLAS in detail. However, the fully managed MongoDB Atlas database service for modern applications ensures availability, scalability, and security compliance. Developers and DevOps professionals choose the best deployment solution based on application architecture, scalability needs, security requirements, and operational preferences. With the MongoDB cloud platform, you can expand your data with features like full-text search, mobile sync, and automated data tiering.
For contemporary applications, however, the fully managed MongoDB Atlas database service ensures availability, scalability, and security compliance. Depending on the application architecture, scalability needs, security requirements, and operational preferences, developers and DevOps experts select the best deployment solution. You can increase the amount of data in your database by utilizing MongoDB's cloud platform's full-text search, mobile sync, and automated data tiering features.
Types Of Deployment In MongoDB Atlas
- Deploy MongoDB Atlas with VPC peering into a new VPC (end-to-end deployment): This option creates a project, cluster, and other components of a full MongoDB Atlas environment within AWS.
- Deploy MongoDB Atlas without VPC peering this option connects your current VPC and MongoDB Atlas.
- Deploy MongoDB Atlas with VPC peering into an existing VPC: This option peers MongoDB Atlas with a new VPC.
- Deploy MongoDB Atlas with Private Endpoint: This option connects MongoDB Atlas AWS VPC using Private Endpoint
Each option creates a corresponding MongoDB database user and AWS IAM role for increased security. We're going to deploy MongoDB Atlas end-to-end using VPC peering into a new VPC. This option creates a project, cluster, and other components of a full MongoDB Atlas environment within AWS. Set up MongoDB Atlas to connect MongoDB with AWS services by performing the following steps:
Section 1: MongoDB Atlas Setup
Step 1. Sign Up/Login for MongoDB Atlas. if you don't have an account of MongoDB then just Sign Up: (https://www.mongodb.com/cloud/atlas/signup). After creating an account, log in to your MongoDB Atlas account.
.jpg)
Step 2: Create a new project. For the new project go to project->New Project-> Enter the name of project -> add members and set permissions-> finally create a project. the steps are as follows:
.jpg)
Step 3: Create new Cluster on MongoDB ATLAS Dashboard: create new cluster->select preferred machine, cloud provider (AWS) and select preferred machine and cloud provider (AWS).
.jpg)
.jpg)
.jpg)
Step 4: Whitelist Your IP Address.MongoDB Atlasa restricts access to your cluster by default. You need to add your IP address to the cluster's IP whitelist to allow your applications to connect. Add IP addresses access list using add list. In this case, I am making it public IP(0.0.0.0/0) address so anyone can access it
.jpg)
If this option not showing to you while creating a cluster then you need to go MongoDB Atlas dashboard under the Security section -> Network Access ->Add IP Address.
.jpg)
Step 5: Create a Database User. You'll need a database user to connect to your MongoDB Atlas cluster. In the MongoDB Atlas dashboard: ->Database Access ->Add New Database User -> Create username and password -> Specify databases this user can access(mostly Atlas Admin)-> Add User.
.jpg)
.jpg)
.jpg)
.jpg)
Section 2: Connecting to MongoDB Atlas
Step 6: Connect to Your MongoDB Atlas Cluster.In the database section click on cluster-> connect-> you will get 5 options for connect with cluster-> 1. Drivers 2.Compass 3. Shell 4.MongoDb for VS code 5.Atlas SQL. For now, we are going with MongoDB for VS code to connect with the MongoDB cluster with VS Code
.jpg)
.jpg)
.jpg)
There are 4 more options for connecting with cluster using :
- Drivers
- Compass
- Shell
- MongoDb for VS code
- Atlas SQL
you can also use other options according to your need for your application.
Section 3: Testing the Connection
Step 7: MongoDB for VS code Extension.MongoDB for VS code Extension allows us to connect with the MongoDB ATLAS cluster.
.jpg)
In VS Code, open the Command Palette: Click on View->Command Palette->Paste your connection string into the Command Palette which is
"mongodb+srv://<username>:<password>@cluster0.tqxhzq5.mongodb.net/"
Replace <password> with the password for the <username> user, in my case
"mongodb+srv://yogesh:[email protected]/"->
then press Enter or you can use the connect button or "ctrl + shift+ p" to paste the connection string in the Command Palette.click on Create New Playground
.jpg)
Default insert data Queries will given to run for testing purposes
Step 7: Test the connection.Run Queries of MongoDB to insert data in the collection of MongoDB ATLAS
.jpg)
The data is gets inserted using the insertMany() function of MongoDB Query.After running the Queries, the data is inserted into the collection of Database in the MongoDB ATLAS. You can see the data is inserted in the sales collection
.jpg)
All done! Now that you've used MongoDB Atlas, you should be able to connect your application to MongoDB which is hosted on AWS. Consider using environment variables or a secrets manager to store sensitive information, such as database credentials, and be sure to keep your connection strings and credentials secure.
Similar Reads
How to Connect to MongoDB Atlas Using Shell?
MongoDB is a highly scalable NoSQL database, renowned for its ability to manage vast amounts of complex and unstructured data. Unlike traditional databases that use a tabular format, MongoDB employs a document-oriented approach, storing data in a flexible, JSON-like format. This makes MongoDB highly
5 min read
How to Connect MongoDB with Spring Boot?
In recent times MongoDB has been the most used database in the software industry. It's easy to use and learn This database stands on top of document databases it provides the scalability and flexibility that you want with the querying and indexing that you need. In this, we will explain how we conne
5 min read
How to Connect to a MongoDB Database Using Node.js
MongoDB is a NoSQL database used to store large amounts of data without any traditional relational database table. To connect to a MongoDB database using NodeJS we use the MongoDB library "mongoose". Steps to Connect to a MongoDB Database Using NodeJSStep 1: Create a NodeJS App: First create a NodeJ
4 min read
How to Connect Node.js To MongoDB Atlas Using Mongoose?
MongoDB Atlas is a cloud-based database service that offers robust features and scalability for managing our data. Here we will use Express.js for the server framework and Mongoose for interacting with MongoDB. And also we use the Ejs for our front end to render the simple HTML form. In this tutoria
6 min read
How to Connect Mongodb Compass to Flask
An effective GUI tool for managing and visualizing MongoDB data is MongoDB Compass. On the other hand, a well-liked Python web framework for creating web apps is called Flask. Integrating your MongoDB data into your Flask web application may benefit from connecting MongoDB Compass to Flask. Through
2 min read
Troubleshooting MongoDB Atlas Connection Errors
MongoDB Atlas is MongoDB's fully managed cloud service which offers high availability, automatic scaling, and robust security for your databases. However, when connecting to MongoDB Atlas, we may encounter connection errors that can be difficult to detect and resolve. These errors can arise from net
8 min read
How to Connect Node to a MongoDB Database ?
Connecting Node.js to MongoDB is a common task for backend developers working with NoSQL databases. MongoDB is a powerful, flexible, and scalable database that stores data in a JSON-like format. In this step-by-step guide, we'll walk through the entire process from setting up your development enviro
6 min read
How To Connect MongoDB with ReactJS?
MongoDB is a popular NoSQL database known for its scalability, flexibility, and high performance. When building modern web applications with ReactJS, itâs common to connect your frontend with a backend server that interacts with a database like MongoDB. PrerequisiteReact JSNode JSMongo DBApproach to
5 min read
Connect MongoDB Atlas Cluster to MongoDB Compass
MongoDB Compass is a free GUI for MongoDB. We might want to connect MongoDB Atlas Cluster to MongoDB Compass to take benefit of the GUI model for database administration. By connecting MongoDB Atlas, the fully managed cloud database service, to MongoDB Compass, developers can easily interact with th
5 min read
How to use MongoDB Connection String
MongoDB connection strings are essential for establishing connections between applications and MongoDB databases. These strings contain crucial information such as server addresses, authentication credentials and optional parameters, enabling seamless communication. Understanding the structure and c
6 min read