-
Retrieve All Notes
A GET request to/notes: -
Retrieve a Specific Note
A GET request to/notes/:id: -
Create a New Note
A POST request to/notes: -
Update an Existing Note
A PATCH request to/notes/:id: -
Delete a Note
A DELETE request to/notes/:id:
Before deploying the containerized app on ECS, several AWS components must be configured.
Creating a Security Group
Begin by creating a security group for your ECS application. In the EC2 console, navigate to “Security Groups” and create a new group named “ECS SG” with a description like “ECS security group.” For testing purposes, add a rule to allow all traffic from any IP (note that this is not recommended for production). Ensure that the security group is associated with the correct VPC.
Creating an ECS Task Definition
In the ECS console under “Task Definitions,” create a new Fargate task definition (for example, “ECS-project-one”). Configure the following settings:- Task Role: Use the ECS task execution role.
- Memory: Choose minimal memory options for testing.
- Containers: Add both containers to the task definition.
Configuring the MongoDB Container
For the MongoDB container, use the following configuration:- Name: Mongo
- Image: Use the default Mongo image from Docker Hub.
- Port Mapping: Map port 27017.
- Environment Variables: Set up the MongoDB root username and password (e.g., mongo/password).
- Volume: Mount a persistent volume.


Configuring the Express API Container
For the API container:- Name: Web API (or similar)
- Image: Use your pre-built image from Docker Hub (e.g., kodekloud/ecs-project2).
- Port Mapping: Map container port 3000.
- Environment Variables: Supply the four variables required for MongoDB connectivity.

Defining Volumes
Next, add a volume (e.g., “Mongo-DB”) using AWS Elastic File System (EFS) to persist MongoDB data. In the ECS task definition, navigate to the “Volumes” section and create a new volume. You must first create an EFS from the AWS console.
- Create a new file system in the EFS console. Provide a name (e.g., MongoDB) and ensure it is within the same VPC as your ECS cluster.
- Customize mount targets by choosing appropriate subnets and update the default security group to one that allows NFS (typically port 2049). For enhanced security, create a dedicated security group for EFS that permits inbound NFS traffic only from the ECS security group.

- Under “Mount Points,” set the source to the created volume (e.g., MongoDB) and mount it to
/data/dbas required by MongoDB.


Creating the ECS Service and Load Balancer
After finalizing your task definition, create an ECS service with the following steps:- Navigate to your ECS Cluster (e.g., Cluster One) and create a new Fargate service.
- Select the newly created task definition (e.g., ECS-project-two) and specify a service name (e.g., “notes app service”). Set the desired number of tasks (typically one for testing).
- Ensure you select the proper VPC and subnets, and attach the previously created “ECS SG” security group.

Configuring the Application Load Balancer
To distribute traffic and provide a static endpoint for the application:- Choose an Application Load Balancer and open its configuration in a new tab.
- Provide a name (e.g., “notes lb”), set it as internet-facing, and select the IPv4 address type. Ensure that it is associated with the same VPC.
- Create a dedicated security group for the load balancer (e.g., “lb-SG”). Although opening port 3000 might be an initial thought, it is preferable to have the load balancer listen on the default HTTP port (80) and forward traffic to the container’s port (3000). Configure the rule to allow HTTP traffic from any source.

- Next, create a target group (e.g., “notes-targetgroup1”). For ECS tasks, select the target type as IP. Configure the health check settings—by default, the health check is set to
/, but since application endpoints reside under/notes, update the health check path to/notes(or set up a dedicated health check endpoint).

- In the ECS service configuration, link the load balancer by selecting the Application Load Balancer and mapping it to the API container (listening on port 3000). The load balancer will listen on port 80 and forward traffic to the target group.

Verifying the Deployment
Once the tasks are running, test the setup by either accessing the container’s public IP or, preferably, using the load balancer’s DNS name. For example, sending a GET request to: http://<load-balancer-dns>/notes should return the list of notes. Tools like Postman can be used to verify the RESTful API endpoints. A sample POST request body to create a new note:
After confirming that your application is functioning as expected, consider tightening your security group rules and reviewing best practices for production deployments.
This article demonstrated how to deploy a multi-container application on ECS using Docker Compose as a reference. We covered the configuration of ECS task definitions, setting up persistent storage with EFS, and configuring an Application Load Balancer to securely distribute traffic among containers. For additional resources and detailed AWS documentation, please refer to: Happy deploying!