Mastering Application Containerization with AWS EC2, ECR, ECS & Fargate Load Balancing Introduction


Application containerization has revolutionized the way we develop, package, and deploy modern software. With containers, you can ensure consistency across development and production environments, streamline deployments, and scale applications seamlessly. In this post, I’ll guide you through how to containerize an application and deploy it using Amazon EC2, Elastic Container Registry (ECR), Elastic Container Service (ECS), and AWS Fargate topped off with load balancing using an Application Load Balancer (ALB).
This hands-on guide is perfect for developers, DevOps engineers, and cloud enthusiasts looking to build scalable, containerized applications on AWS.
Why Containerize with AWS?
Containers package applications with their dependencies, ensuring they run uniformly across environments. AWS enhances this with:
EC2: Virtual servers to run containers
ECR: Private Docker registry for secure image storage
ECS: Managed container orchestration
Fargate: Serverless compute for containers (no EC2 management)
ALB: Distributes traffic across containers
Let’s get started.
1,CREATE AN EC2 INSTANCE
Give your instance a unique name
Leave others settings at default state and create your Keypairs.
While creating your instance, edit the Network settings. Create a new Security group
And add an inbound rule. For Type, select HTTP and select 0.0.0.0/0 for the source.
Go ahead and Launch Instance.
2,CONNECT TO YOUR INSTANCE
To connect your instance first double click on your instance ID
Select the CONNECT option to see various ways you can connect
i connected using the SSH Client, l made use of the Key Pair we created
At this point open up a PowerShell and input the ssh command
Next is to sign in as a Rootuser using the command code sudo su
After signing in as the rootuser run yum update -y
to update the Amazon Linux
3, INSTALL DOCKER
To install docker, you have to run the following commands. curl -fsSL
https://get.docker.com
-o
get-docker.sh
Next run yum install docker -y
After successfully installing docker, we use the systemctl start docker
to start docker
And run systemctl status docker
to know the status of docker
Below you will notice that the docker is active and running, next is to create.
To leave continue with your commands, type Q and it takes you to the root folder again.
At this stage, we need to create a directory with the mkdir
command.
And cd
into the directory.
Type vi Dockerfile to build up your dockerfile
FROM almalinux:8
RUN dnf install -y httpd && dnf clean all
COPY index.html /var/www/html/
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
EXPOSE 80
Type vi Index.html to create your html file. Put in your code, save and exit.
Next is to create an image using command docker build -t imagename .
Login to your docker account by typing docker login
and inputting all the necessary information
Go to the AWS Console - IAM - User - Create access keys - Use Case (choose CLI) - Select confirmation box - Next - Create access keys.
Return to PowerShell and type aws configure
and fill in the requirement
4,CREATE AN ELASTIC CONTAINER REGISTRY
You can do this by running the command aws ecr create-repository —repository-name <repository name>
After creating the repository, you can confirm on AWS Console. Click on the repository that has just been created.
Next, click View Push Commands and a dialog box would be opened
Select windows and copy these commands one after the other and run them on Powershell.
The last code of the push command is shown below.
Next is to create a container to run Your image by running the command docker run -d -p 80:80 –name container name image name
5,CREATE A LOAD BALANCER
Go to the EC2 instance dashboard and select Load Balancing - Load Balancers - Create load balancer.
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 reduce it to 2.
Click Next
Select your instance, next select Create Target group.
Return to Listener and routing so that you can add the load balancer and add the target group you created.
Select Create
CREATE A TASK ROLE
Open IAM - Roles - Create role.
Under Trusted entity type, choose AWS Service.
Next select EC2 as most commonly used services
On the Add Permissions select Amazon ECS Task Execution
Give your Role a unique name
Click next to the service or use case and Select Elastic container service Task
Go ahead and create your Role
CREATE ELASTIC CONTAINER 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.
Note your image URL cause you will be needing it.
Go ahead to Container-1, give the container a name. Go to ECR and copy the URL of your image and paste here as shown below then go ahead and create.
Click Clusters - Create Cluster
Leave every other sections as default and click Create.
Note l changed the name of my cluster while creating
Click the cluster that you have just created, scroll down and click Services - Create.
Leave Environment section as default and head to Create Service. 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 and head to the Load balancing section, check the use load balancing and select the Application load balancer. on the Container section select your container you created
On the Application Load Balancer check the use an existing load balancer, below it choose the existing.
Use an existing Listener on the listener section and use HTTP:80
On the target group section create new target group, give it a name and on the Evaluation order leave it at 2
Click Create.
Double click on the cluster created to see is active
Double click on the created service to see that our status is active and our Task is running and also to see the Deployment was successful.
Move back to the cluster and click on Task to see that is running
Click on infrastructure, to see the capacity provider
On the service created click on Health to see the health condition of the target.
Also check the Configuration and Networking to see is turned on.
Once everything is ok and working fine that shows that the Deployment was successful.
Now I picked the IPv4 address of my EC2 instance and opened it and opened it on a new tap and it was successful.
I also copied the DNS of my load balancer and encountered error.
To correct the error, I went back to my target group and I noticed the instance did not have any target group attached to it as you can see below.
Move to Target and click on Register target
Select the Register target under the target group created to see it running select it and click on include as pending below.
I went back to check my load balancer. Target group for the instance was initially 0 but after the amendment done, it changed to 1. That means the target group is finally associated with the instance.
I also headed to my load balancer and selected the load balancer l created, selected Resource map
At this point i found out that one of my target group is in an availability zone that is not enabled for the load balancer that makes it unhealthy.
NOTE this is different for different person yours mighty successful at this point and next is to copy the DNS name again to a new tap and it will be successful, but in my case it was not so i had to ensure that the target is healthy.
Moved back to my load balancer l created
Selected it and clicked on Edit subnets
Selected the zones and saved it.
l moved back to the load balancer l created clicked on Networking mapping
And boom it was successfully
At this point l copied my DNS name to a new tab and it came up successfully.
Thank you for stopping by I hope I was able to put you through the process in a way detailed manner.
Like, follow and drop a comment and let me know what you think about this project.
Subscribe to my newsletter
Read articles from Nweke Henry directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
