Moving to MongoDB Atlas
Background
In the previous section, we deployed a multi container app in the cloud using the AWS ECS service. We created two containers, one for the back end, and the other for the the database. There is pros and cons of having our own container for the database via a ECS container. This can be a solution of choice for those mastering the cloud and the database management. However, it might be more complex when it's time to ensure scaling and availability. More performance problems can also occurs during traffic spike. Managing the security and backups can also be more troublesome.
An alternative to using a Mongo Docker container is to use MongoDB Atlas which is a database already deployed in the cloud. In this section, the instructor shows how to integrate MongoDB Atlas to our current application.
MongoDB Atlas
Create a MongoDB Atlas account.
Create a database for free in the Cloud by choosing Shared Clusters.
Better practice to use the MongoDB Atlas solution for both development and production to avoid compatibility issues regarding the Mongo version.
We want to make our code flexible enough to use the same cloud database for both production and development. Therefore, in our application, we have to change the MongoDB connection string and environment variables in the app.js
file.
The environment MONGODB_NAME
is to distinct which database will be used for dev vs production.
We can modify the value of our variable environment by matching the MongoDB_URL
with the domain name specified in the connection string provided by MongoDB Atlas.
We can also get rid (commented out) of the mongodb service and the data volume in the docker-compose.yml
file as they will no longer be needed. We also have to delete the depends_on
instruction for the backend.
Security Configurations
MongoDB Atlas offers some security features. In the Network Access tab, we can whitelist IP addresses to restrict who will be able to connect to the cluster. In the image below, connection to the cluster can be done from any IP.
We also have to create at least one user with Write and Read access.
In the backend.env
file, we can modify the environment variables as so:
To verify that our application can correctly connect to the Mongo Atlas database, we can start the services using docker-compose up
.
For AWS to use that version of the web application, we need to rebuild the image and re-push it to Docker Hub.
MongoDB Atlas in production
Since a container is not needed anymore for the database, we can visit the AWS console and delete every resources related to the MongDB container, EFS and EFS security group. We can Create a new task revision and update the environment variable for the Web API to connect to the MongoDB Atlas database.
We can now update the Service with using the latest Task Definition. Our service should be up and running.
To confirm that everything worked properly, I made some API calls using Postman to the same Load Balancer URL.
Last updated