Skip to content

Deploy a serverless MEAN Stack application with MongoDB Atlas and Fargate

License

Notifications You must be signed in to change notification settings

mongodb-partners/MEANStack_with_Atlas_on_Fargate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

App Modernization with AWS Fargate(ECS) and MongoDB Atlas

Introduction:

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

  1. Serverless Application Deployment for Production Environment
  2. Production deployment to auto-scale, HA, and Security
  3. Agile development of application modernization
  4. Deployment of containerized application in AWS
  5. 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).

Architecture Diagram:

image

Pre-requisite:

AWS Console with cloud shell access

Node version v18.8.2 and above. (this code repository is not compatible for node v16.x.x)

MongoDB Atlas cluster

Step-by-Step Fargate Deployment:

Step 1: Set up the MongoDB Atlas cluster**

Please follow the link to set up a free cluster in MongoDB Atlas

Step 1a: Configure the Database access and Network Security

Configure the database for database access and network security

Step 2: Create the secret in the Secret Manager

a. In AWS Console navigate to Secret Manager and click on Store a New secret

image

b. Choose Other type of secret then Plaintext tab and provide the full URL of MongoDB Atlas Cluster

image

c. Proceed to the next page where you supply the secret name as workshop/atlas_secret

image

d. Continue to other pages without any changes to store the secret

image

e. Confirm the secret is created by observing it listed on the page. Note: you might need to hit the refresh button to reload the secrets.

image

Step 3: Login to the AWS Console cloud shell and set up the ECS Copilot Application and environment

a. Login to AWS Console and launch the cloud shell image

b. Install ECS Copilot CLI by running the following commands

sudo curl -Lo /usr/local/bin/copilot https://github.com/aws/copilot-cli/releases/latest/download/copilot-linux    && sudo chmod +x /usr/local/bin/copilot    && copilot --help

c. Clone the GitHub repository for the code. Ensure node version is 18.x.x or above

git clone https://github.com/mongodb-partners/MEANStack_with_Atlas_on_Fargate.git
cd MEANStack_with_Atlas_on_Fargate/code/MEANSTACK/partner-meanstack-atlas-fargate
node --version
image

d. Install the Application and Environment

copilot init
image

Type your application a name atlasmean and hit Enter Key

Next, select Load Balanced Web Service and hit Enter Key

image

We are now creating a service. There's a predefined service called server. Type server as the service name.

image

Select the server/Dockerfile from the menu and hit Enter key

image

Wait till initial infrastructure provisioning finishes and you are prompted for a deployment environment

Press y to deploy

image

Supply the environment name dev and hit Enter Key.

image

Ensure the server is deployed successfully.

image

Step 5: Update the Server URI to the frontend code

For the frontend application to communicate to the server service, it needs to know the server service URI. We have a placeholder in the code where you need to make the change.

a. First, we need to find the server service DNS. In AWS Console, navigate to EC2 and then in Left Side Navigation to Load Balancing|Load Balancers. Note the DNS name in the bottom right part of the page.

image image

b. Next update employee.service.ts to point to the DNS. Leave the port as is.

image

c. Run the following command

nano client/src/app/employee.service.ts

d. Use arrow keys to navigate to the placeholder, and paste the DNS keeping the port number intact.

image

e. Ctrl-X to save

image

Step 6: Deploy the front-end code

a. Use the same command we used before copilot init and select Load Balanced Web Service

image

b. Supply frontend as the service name

image

c. Press y to deploy, and chose the dev environment we created earlier.

d. If you get the message below. Then type copilot deploy.

image

e. Make sure you select frontend (using the spacebar)

image

f. Wait for the deployment process to complete in AWS Console CloudFormation UI or from the CloudShell. You should see at least one running task.

image

g. Now, you need to find the DNS for the frontend. Like before, navigate to EC2 --> Load Balancing --> Load Balancers. Note there are now two load balancers, one for the frontend and the other for the server. Select the frontend one. It has a listener configured on port 8080.

image

image

h. Now switch to the details tab and copy the DNS

image

Step7: Testing the Application

Test the application by invoking the <"DNS for frontend loadbalancer">:8080 copied from the above step.

image

Useful links

Resolving EACCES permissions errors when installing packages globally

AWS CloudShell compute environment: specifications and software

Summary:

Congratulations!! you have successully deployed the containerized application onto AWS ECS Fargte.

Cleanup

Use the command copilot svc ls to list all the servrices

Use the command copilot svc delete --name frontend & copilot svc delete --name server to delete the services

image

Pls share your feedback/queries in the comments

About

Deploy a serverless MEAN Stack application with MongoDB Atlas and Fargate

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published