Accelerate Application Modernization with Amazon ECS, AWS Fargate, and MongoDB Atlas
By Igor Alekseev, Partner Solutions Architect, Data and Analytics – AWS
By Babu Srinivasan, Sr. Partner Solutions Architect – MongoDB
Enterprises are continuously innovating to make their offerings simpler, easier, and more customer-focused. The digital transformation journey drives the modernization of their legacy application and technology stack.
Customer expectations drive business changes at a rapid pace, but it can be hard for legacy applications to cope with ever-changing business needs as application downtime worsens customer experience and decreases competitive edge.
Developers are struggling to manage code changes and introduce new functionality, and as they build monolithic applications the complexity increases, leading to large amounts of technical debt.
Businesses are also faced with increased data and variety, as data structures are no longer tabular and come in all forms and shapes like JSON, logs, videos, and big data.
You can migrate to the two stacks by introducing MongoDB Atlas and Amazon Elastic Container Service (Amazon ECS) with AWS Fargate into your architectures. Both come with built-in security controls and ability to scale, and they include serverless options that allow businesses to pay only for what they use. This improves agility by allowing quick experimentation while still being cost effective.
This post focuses on how you can use MongoDB Atlas and Amazon ECS with AWS Fargate to modernize your applications. MongoDB is an AWS Data and Analytics Competency Partner and developer data platform company empowering innovators to unleash the power of software and data.
MongoDB Atlas + Amazon ECS with AWS Fargate
As a developer data platform, MongoDB Atlas was designed to be fully managed, easy to use, scalable, and versatile. It provides a mission-critical foundation for developers to build upon Atlas and is built on the document data model, which stores records as compressed BSON documents for easy storage, organization, and retrieval.
AWS Fargate is a compute engine that enables you to run containers without having to manage and patch Amazon Elastic Compute Cloud (Amazon EC2) instances. It’s a serverless, pay-as-you-go model that enables you to focus on building applications.
AWS Fargate is a serverless compute engine that works with both Amazon ECS and Amazon Elastic Kubernetes Service (Amazon EKS).
The application runs on AWS Fargate and is behind Application Load Balancer. It connects to MongoDB Atlas over AWS PrivateLink. The application tier authenticates to MongoDB Atlas using AWS Identity and Access Management (IAM) role-based authentication mechanism.
Figure 1 – MongoDB Atlas and AWS Fargate reference architecture.
This post provide a high-level overview of the steps. For more detailed steps and code, we refer to a sample GitHub repository.
Step 1: Set Up Atlas Cluster
The first step is to set up an Atlas cluster; if you have an existing cluster, you can skip Step 1. Note that we are using AWS PrivateLink to ensure the traffic stays secure between Fargate and your Atlas cluster. PrivateLink is supported in MongoDB Atlas for dedicated and serverless clusters.
The application needs to authenticate to our MongoDB Atlas cluster, and the recommended approach is to use a passwordless mechanism. We achieve this by leveraging MongoDB AWS IAM role-based authentication for Fargate. Follow Step 1c to create an IAM role and configure the IAM-based authentication.
Figure 2 – IAM authentication configuration.
The figure above shows how to configure a database user in the MongoDB Atlas user interface (UI) with IAM-based authentication.
Step 2: Clone Sample Code Repository
If you don’t have an application, we have an example in the repository code folder. Clone it by following instructions in Step 2. Note that an .env file needs to be updated to include the connection string for your MongoDB cluster with authMechanism set to MONGODB-AWS. This enables IAM role-based authentication to MongoDB Atlas.
The next step is to containerize your application using Docker Desktop. The reference application contains two separate components: frontend and backend. Each component already contains its own Dockerfile. If you are containerizing your own application, then you need to add Dockerfiles to each of your components.
Step 3: Publish Docker Image to Amazon ECR
Next, we need to build and ship the container images to a container registry. We’re using Amazon Elastic Container Registry (Amazon ECR) to store the image. First, we create two ECR repositories following instructions in Step 3a.
Step 4: Build Application
In this post, we use docker compose with its to deploy resources to the cloud. Docker Compose for Amazon ECS helps developers to set up an AWS context with one Docker command, and simplifies multi-container application development on Amazon ECS.
The docker-compose.yaml file describes the services and dependencies between the different parts of the application. We need to update the file to include references to the container images stored with Amazon ECR, and specify the virtual private cloud (VPC) into which our resources are deployed by following instructions in Step 3b.
Step 5: Deploy Application
Before testing the application, we need to verify that the deployment finished successfully. The figure below shows the AWS CloudFormation UI with stack details and status.
Figure 3 – AWS console CloudFormation UI.
In the AWS Management Console, verify the cluster has started. The figure below shows the Amazon ECS cluster details, indicating successful deployment of the application services.
Figure 4 – AWS console ECS UI.
Next, you need to find the Application Load Balancer URL. Click on “Client Service” to view the details of the service. Below are the details of the Client Service which contains target groups from which you can get the Load Balancer DNS.
Figure 5 – ECS Client Service detail UI.
Next, make sure all of the tasks are running. Figure 6 shows details of individual ECS tasks, indicating successful running of the tasks.
Figure 6 – AWS console ECS tasks UI.
Lastly, you need to use the URL of the load balancer to launch and test the app in a browser.
Figure 7 – Sample application UI.
Business Use Case
While this reference application is simple, it nonetheless covers a wide variety of use cases. Enterprises looking to standardize their application architecture, development, and deployment practices can opt for adopting this architecture. The same is true for companies interested in leveraging rapid prototyping and application development techniques.
Following the steps outlined in this post, the resulting application is scalable and secure out of the box. It is serverless and allows for easy scaling in and out, without having infrastructure management overhead. DevOps teams will appreciate its standardized deployment process, especially when scripted.
By leveraging Amazon ECS, AWS Fargate, and MongoDB Atlas, you can develop applications rapidly to align with business needs and timescales. Thus, developers can fulfill productivity and business requirements simultaneously.
To learn more, refer to the MEANStack_with_Atlas_on_Fargate GitHub repository for step-by-step instructions and sample code.
MongoDB – AWS Partner Spotlight
MongoDB is an AWS Data and Analytics Competency Partner and developer data platform company empowering innovators to unleash the power of software and data.