This is a technical repo to demonstrate the application deployment using MongoDB Atlas and AWS Fargate. This tutorial is intended for those who want to
- Serverless Application Deployment for Production Environment
- Production deployment to auto-scale, HA, and Security
- Agile development of application modernization
- Deployment of containerized application in AWS
- Want to try out the AWS Fargate and MongoDB Atlas
MongoDB Atlas is an all-purpose database having features like Document Model, Geo-spatial, Time Series, hybrid deployment, and multi-cloud services. It evolved as a "Developer Data Platform", intended to reduce the developer workload on the development and management of the database environment. It also provides a free tier to test out the application/database features.
AWS Fargate is a serverless, pay-as-you-go compute engine that lets you focus on building applications without managing servers. AWS Fargate is compatible with both Amazon Elastic Container Service (ECS) and Amazon Elastic Kubernetes Service (EKS).
Code editor: VSCode
Code repository: Git
Command Line execution: AWS CLI
Container: Docker Desktop (with docker compose)
Side Note: Ensure the versions are managed as shown. Will have compatibility issues with the latest versions.
Please follow the link to set up a free cluster in MongoDB Atlas
Configure the database for network security
Follow the link for IAM Role based authentication for AWS Fargate
On successful creation of the role, click "Add permissions" and select "Attach policies" to add "AmazonEC2ContainerRegistryReadOnly" policy to the role.
Copy the ARN created for the role
Select IAM Type as IAM role in MongoDB Atlas for the database user and provide the ARN as shown below
git clone the code from the repository
git clone https://github.com/mongodb-partners/MEANStack_with_Atlas_on_Fargate.git
cd code/MEANSTACK/partner-meanstack-atlas-fargate
Open the code in VSCode
Configure the MongoDB Connection string in ".env" in partner-meanstack-atlas-fargate --> server --> .env file. Update the server details.
Setup the AWS CLI environment
Create the ECR for the client and note down the URI for the repository.
aws ecr create-repository \
--repository-name partner-meanstack-atlas-fargate-client \
--image-scanning-configuration scanOnPush=true \
--region us-east-1
Create the ECR for the backend and note down the URI for the repository.
aws ecr create-repository \
--repository-name partner-meanstack-atlas-fargate-server \
--image-scanning-configuration scanOnPush=true \
--region us-east-1
Configure the Docker image in "docker-compose.yml" in partner-meanstack-atlas-fargate folder.
Update the details for x-aws-vpc , x-aws-role, and images of both server and client, and platform. For the image, paste the URI copied from the earlier step.
Ensure the VPC has having least two public subnets in different AZs. if any of the subnets are in the same AZs, the docker-compose up command will fail.
Ensure the docker desktop is up and running. if not start the docker deamon
update the ECR URI with the account_id in the below command.
ensure the current directory is ...code/partner-meanstack-atlas-fargate
aws ecr get-login-password --region us-east-1| docker login --username AWS --password-stdin <account_id>.dkr.ecr.us-east-1.amazonaws.com
docker context use default
docker compose build
docker compose push
docker context create ecs partner-meanstack-atlas-fargate
docker context use partner-meanstack-atlas-fargate
docker compose up
Note: While creating the docker context, it will give the option for selecting the AWS authentication. Choose the default. Please refer link for further details
? Create a Docker context using: [Use arrows to move, type to filter]
An existing AWS profile
AWS secret and token credentials
AWS environment variables
This will automatically create the AWS CloudFormation stack and deploy the stack.
Verify that the stack is completed successfully
Verify the ECS cluster, task definition, and services are created successfully.
Copy the DNS Name from the Load Balancer
update the private URL in the code with the copied DNS Name. (partner-meanstack-atlas-fargate --> client --> src --> app --> employee.service.ts )
Ensure the code is saved successfully.
complete the rebuild. It will be a rolling update and the changes will be deployed, without impact to the running instances.
docker context use default
docker compose build
docker compose push
docker context use partner-meanstack-atlas-fargate
docker compose up
Ensure that both the client and server tasks are up and running after the update.
Ensure the AWS Cloud map service is registered with both client and server services.
copy the public IP address of the client task
Test the application by invoking the public ipaddress:8080 copied from the above step.
Hope this provides the steps to successfully deploy the containerized application onto AWS Fargate.
Pls share your feedback/queries to [email protected]