Containerization with EC2, ECR, ECS, Fargate and Load Balancing


Prerequisites
An active AWS IAM account with necessary permissions.
Docker installed on your local machine or EC2 instance.
AWS CLI configured with your credentials.
Step 1: Launch and Configure an EC2 Instance
Deploy an EC2 Instance:
Use the AWS Console to launch an EC2 instance.
During setup, create a new security group with an inbound rule allowing HTTP traffic (port 80) from all sources (0.0.0.0/0).
Connect to the Instance:
- Use the EC2 Instance Connect feature to access your instance via the browser. Below is the result.
Update the System and Install Docker:
Switch to the root user:
sudo su
Update the package manager:
yum update -y
Install Docker:
curl -fsSL https://get.docker.com -o get-docker.sh sh get-docker.sh
Start and verify Docker:
systemctl start docker
systemctl status docker
Step 2: Prepare Your Application for Containerization
Set Up Application Directory:
Create and navigate to a new directory:
mkdir myapp cd myapp
Create a Dockerfile:
Use a text editor to create a Dockerfile:
vi dockerfile
Insert the following content:
Create an HTML File:
Create an
index.html
file and add your desired html content.vi index.html
-
Build and tag your Docker image:
docker build -t myapp .
Step 3: Push Docker Image to Amazon ECR
Authenticate Docker to ECR: Go to the AWS Console - IAM - User - Create access keys - Use Case (choose CLI)—Select confirmation box - Next - Create access keys. Go back to your instance and run aws configure
Retrieve and execute the login command for ECR:
aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <account-id>.dkr.ecr.<region>.amazonaws.com
Create an ECR Repository:
Create a new repository:
aws ecr create-repository --repository-name myapp
Once the repository has been created, you may verify it via the AWS Console. Click on the repository that has just been established. After that, select View Push Commands to open a dialog box.
Tag and Push the Image:
Tag your image for ECR:
docker tag myapp:latest <account-id>.dkr.ecr.<region>.amazonaws.com/myapp:latest
Push the image to ECR:
docker push <account-id>.dkr.ecr.<region>.amazonaws.com/myapp:latest
Create a container to run your image by running the command in the diagram below.
Step 4: Create an ECS Task Definition
Navigate to ECS in AWS Console:
- Go to the Amazon ECS section.
Create a New Task Definition:
Choose "Fargate" as the launch type.
Specify task and container definitions:
Container name:
myapp
Image:
<accou
nt-id>.dkr.ecr.<region>.
amazonaws.com/myapp:latest
Port mappings: 80
Assign Task Roles:
- Create and assign necessary IAM roles for task execution.
Step 5: Set Up an Application Load Balancer (ALB)
Create a New ALB:
Navigate to EC2 > Load Balancers > Create Load Balancer.
Select "Application Load Balancer".
Configure the following:
Name:
myapp-alb
Scheme: Internet-facing
Listeners: HTTP on port 80
Availability Zones and subnets: Select as per your VPC configuration
Configure Security Groups:
- Assign a security group that allows inbound HTTP traffic on port 80.
Set Up Target Groups:
Register Targets:
Give your load balancer a name. Leave the other parts as default and move to network mapping.
Under Network mapping, select the VPC. In my case, it was the default VPC, and I selected 3 AZs, which automatically attached the subnets.
Under Security groups, click on the drop-down arrow and select the security group you created when creating the EC2.
Under Listeners and Routing, select Create Target Group.
Give the target group a name.
Move to Advanced Health Check Settings and scroll to Healthy Threshold.
g. Click Next
Select your instance, and next select Create Target Group.
Return to Listener and routing so that you can add the load balancer.
Select Create
Step 6: Deploy ECS Service with Fargate
Create an ECS Cluster:
- In the ECS console, create a new cluster named
myapp-cluster
.
- In the ECS console, create a new cluster named
Create a Service:
Within your cluster, create a new service with the following configurations:
Launch type: Fargate
Task definition: Select the one created earlier
Service name:
myapp-service
Number of tasks: 2 (for high availability)
Cluster VPC and subnets: Select appropriate options
a. Open IAM - Roles - Create role.
b. Under Trusted entity type, choose AWS Service.
- Search ECS - Task Definition - Create new task definition.
Under Infrastructure Requirements, go to Task Role and Task Execution Role and select the role you created for ECS.
Go ahead to Container-1 and give the container a name. Go to ECR and copy the URL of your image and paste it here as shown in the diagram.
Click Create.
Click Clusters - Create Cluster
Leave every other section as default and click Create.
Click the cluster that you have just created, scroll down, and click Services - Create.
Leave the Environment section as default and head to Deployment configuration. Choose the task definition you created under task definition family. Next, give a service name.
Under desired tasks, choose 2.
Move to Networking - Security group - use existing security group - select the security group you created.
Head to the Load balancing section
Click Create.
From the images above, every deployment was successful.
Now pick the IPv4 address of the EC2 instance and open it, and the result,
If you go to your target group, you will notice that your instance doesn’t have a target group attached to it, as seen below.
Go to your load balancer, and if you see that the target group for the instance is 1, then it is successful.
Now let’s go back and check with the DNS names. And there we have it.
Thanks for reading to the end. I’ll see you next time on where I simplify all things cloud computing.
Subscribe to my newsletter
Read articles from Teslim Lawal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
