Deploying Docker Containers on AWS ECS Fargate with ALB: A Step-by-Step Guide

👋 Welcome to my Hashnode profile! I'm a passionate technologist with expertise in AWS, DevOps, Kubernetes, Terraform, Datree, and various cloud technologies. Here's a glimpse into what I bring to the table: 🌟 Cloud Aficionado: I thrive in the world of cloud technologies, particularly AWS. From architecting scalable infrastructure to optimizing cost efficiency, I love diving deep into the AWS ecosystem and crafting robust solutions. 🚀 DevOps Champion: As a DevOps enthusiast, I embrace the culture of collaboration and continuous improvement. I specialize in streamlining development workflows, implementing CI/CD pipelines, and automating infrastructure deployment using modern tools like Kubernetes. ⛵ Kubernetes Navigator: Navigating the seas of containerization is my forte. With a solid grasp on Kubernetes, I orchestrate containerized applications, manage deployments, and ensure seamless scalability while maximizing resource utilization. 🏗️ Terraform Magician: Building infrastructure as code is where I excel. With Terraform, I conjure up infrastructure blueprints, define infrastructure-as-code, and provision resources across multiple cloud platforms, ensuring consistent and reproducible deployments. 🌳 Datree Guardian: In my quest for secure and compliant code, I leverage Datree to enforce best practices and prevent misconfigurations. I'm passionate about maintaining code quality, security, and reliability in every project I undertake. 🌐 Cloud Explorer: The ever-evolving cloud landscape fascinates me, and I'm constantly exploring new technologies and trends. From serverless architectures to big data analytics, I'm eager to stay ahead of the curve and help you harness the full potential of the cloud. Whether you need assistance in designing scalable architectures, optimizing your infrastructure, or enhancing your DevOps practices, I'm here to collaborate and share my knowledge. Let's embark on a journey together, where we leverage cutting-edge technologies to build robust and efficient solutions in the cloud! 🚀💻
Introduction:
In the era of cloud computing, deploying applications in a scalable and reliable manner is essential for businesses of all sizes. Amazon Web Services (AWS) offers a powerful suite of services for container management and orchestration. Among them, Amazon Elastic Container Service (ECS) is a robust platform for deploying and managing Docker containers at scale.
In this guide, we will walk through the process of deploying Docker containers on AWS ECS, augmented with the use of an Application Load Balancer (ALB) to distribute incoming traffic among container instances. By following these steps, you'll be able to create a highly available and scalable architecture for your applications, ensuring they remain responsive even under varying loads.
Whether you're a seasoned AWS user or new to the platform, this guide will provide you with a comprehensive overview of the steps involved in setting up your ECS cluster, defining task definitions, configuring an ALB, and deploying your Docker containers. Let's dive in and explore the world of containerized deployments on AWS ECS.
Step 1: Create a Docker Container
First, you'll need a Docker container to deploy on ECS. You can use an existing Docker image or create your own.
Refer to My blog (How to Push Docker Images to Amazon ECR (Elastic Container Registry) - Private Repository)
For this example, let's create a simple Node.js web server.
- Create an
Dockerfilein your project directory:
FROM --platform=linux/amd64 node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install
CMD ["node", "index.js"]
EXPOSE 3000
- Create a
index.jsfile in the same directory:
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Welcome to my app!!!!!!");
});
app.get("/info", (req, res) => {
res.send("hey this is /info api");
});
app.listen(3000, () => {
console.log("listening on 3000");
});
- Build the Docker image:
docker build -t your-image-name .
Step 2: Push Docker Image to ECR (Elastic Container Registry)
Before deploying to ECS, push your Docker image to the Amazon ECR.
- Tag your Docker image:
docker tag your-image-name:latest your-ecr-repository-uri:latest
- Push the image to ECR:
docker push your-ecr-repository-uri:latest
Step 3: Create an IAM role for ECS
To create an IAM role, navigate to the IAM console, select "Roles," and then "Create role."
Choose the type of trusted entity, "ecs-tasks"

Attach relevant permissions policies, review the role details, provide a name and optional description, and finally, create the role.
This role can be used to grant permissions to AWS services, users, or resources as needed within your AWS environment.
Step 4: Create ECS Cluster and Task Definition
Go to the ECS service in the AWS Management Console.
Create a new ECS cluster (fargate).


Define a Task Definition:
Specify container details, including image URI (from ECR).
Define CPU, memory, networking, etc.


Step 5: Create an Application Load Balancer (ALB)
Go to the EC2 service in the AWS Management Console.
Under Load Balancers, create a new Application Load Balancer (internet facing).
Configure listeners, security settings, and routing.
Security Group rules for ALB are below.

Create a target group (IP addresses) and select VPC, health check path (/).
- Don't select any target instances.

Step 6: Configure ECS Service
Create a new ECS service within your cluster.


- Select your task definition and Configure the desired number of tasks.

- Select VPC, subnets, and security group for the cluster.

- Select the load balancer and target group created before.



Step 7: Test
Once everything is set up, access your ALB's DNS name in a web browser. You should see your Node.js application running.
Conclusion
That's a basic overview of creating an ECS container with an ALB setup in AWS. Make sure to adjust the configurations according to your specific requirements and security considerations. Let me know if you need further assistance!




