Simplifying Application Containerization with AWS ECS, EC2, ECR, Fargate, and Load Balancing

Joel ThompsonJoel Thompson
4 min read

Introduction

Deploying containerized applications on AWS can be simplified using Amazon ECS, EC2, ECR, Fargate, and Load Balancing. This guide provides a step-by-step approach to setting up a highly available and scalable containerized application.

Here are the essential tools needed for the project:

AWS Services – ECS, EC2, ECR, Fargate, ALB, IAM
Docker – For containerization and image management
AWS CLI – To manage AWS resources via command line
Code Editor – For writing Dockerfiles and configurations
Linux Terminal – To execute commands and set up instances

Step-by-Step Deployment Process

Step 1: Create an EC2 Instance

Why?
An EC2 instance serves as the foundational infrastructure for hosting the containerized application.

How?

  • Launch an EC2 instance using t2.micro.

  • Edit network settings:

    • Modify the security group name to define access controls.

    • Add an inbound rule to allow HTTP traffic.

    • Choose source type as Anywhere (0.0.0.0/0) for unrestricted access.

  • Launch and connect to the instance.

  • Copy the public IP and access the server via SSH.

Step 2: Install Docker

Why?
Docker is required to containerize the application, making it portable across environments.

How?

  • Update the system:

    Type sudo su command to get root user access and yum update -y, to update your Linux.

  • Install Docker:

curl -fsSL https://get.docker.com -o get-docker.sh

yum install docker -y

  • Verify installation:

docker --version

  • Start Docker:

systemctl start docker

systemctl status docker

To leave continue with your commands, type Q and it takes you to the root folder again.

a. A create a directory with the mkdir command and cd into the directory.

b. Type vi Dockerfile and input this command in the dockerfile.

c. Type vi Index.html to create your html file. Paste your code, save and exit.(Esc+:wq)

d. Login to your docker account docker login and input all the necessary details.

✅Navigate to AWS Console - IAM - User. and create a user

  • Then Create access keys .

f. Return to PowerShell and type aws configure

. Step 4: Create Elastic Container Registry (ECR)

Why?
ECR stores container images securely, making them accessible for deployment.

How?

  • Create ECR repository:

aws ecr create-repository --repository-name <repository_name>

  • Click Push Commands for repository instructions

b. click View Push Commands and a dialog box would be opened.

Copy and run each of the prompts

Step 3: Build and Run the Container

Why?
Containers package the application and its dependencies, ensuring consistency across different environments.

How?

  • Build the Docker image:

docker build -t imagename .

  • Create a container to run Your image by running the command below:

docker run -d -p 80:80 --name containername imagename

Step 5: Deploy a Load Balancer

Why?
The Application Load Balancer (ALB) ensures high availability and distributes traffic efficiently.

How?

  • Navigate to EC2Create Load Balancer.

  • Select Internet-facing ALB with three subnets.

Select Availability Zones

  • Attach the EC2 Security Group to control access.

Target Group Setup:

  • Create a Target Group to route requests.

  • Reduce Health Check Threshold to 2.

  • Associate the target group with the load balancer.

Step 6: ECS Configuration - Task Definition & Cluster

Why?
ECS automates container orchestration, ensuring seamless deployment.

How?

  • Create Task Definition using Fargate.

  • Assign IAM Role:

    • Select Elastic Container Service Task.

  • Grant AmazonECSTaskExecutionRolePolicy permissions.

Task Definition Configuration:

  • Leave default settings and create the task definition.

Link ECR image URI

Task definition and cluster successfully created

Step 7: Create ECS Cluster & Service

Why?
A cluster organizes tasks, while the service ensures scalability and load balancing.

How?

  • Create ECS Cluster:

    • Use default VPC.

    • Select three subnets.

Service Setup:

  • Attach Task Definition.

  • Define Service Name.

  • Set Desired Task Count to 2.

Networking & Load Balancing:

  • Assign Security Group and ALB.

  • Select existing listener.

  • Define Target Group.

  • Configure Path Pattern (/*).

  • Set Evaluation Order to 1.

Validation:

  • Ensure tasks are running.

  • Copy DNS link from Networking.

  • Paste in a browser—confirm successful deployment! 🎉


Conclusion

This guide provides a structured, easy-to-follow approach to containerizing applications using AWS ECS, EC2, ECR, Fargate, and Load Balancing. By following these steps, you achieve scalability, high availability, and efficient resource management.

0
Subscribe to my newsletter

Read articles from Joel Thompson directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Joel Thompson
Joel Thompson