Containerizing an Application using ECR,EC2,Fargate &ECS

In a world where scalability, speed, and automation define software success, containerization isn't just an option, but also it's the foundation. This project i am documenting today dives into the heart of modern cloud-native architecture, bringing containers to life on the cloud using AWS power tools: ECR, EC2, Fargate, and ECS.

Special Thanks to my Tutor Bunmi Agbomeji, You are the best.

Step 1. Go to your AWS Portal and sign in your credentials as an IAM user select a region , for the cause of this project i will be selecting Region United States, N. California (us-west-1), then on the search bar , search for EC2, to create an EC2 instance.

Step 2. Click on lunch Instance. Give your Instance a name, for the course of this project i will give my instance "techtest" , Next Select an Application and OS Image, i will Pick Amazon Linux Image or OS, then Select an Instance Type, I will Select t2.micro, Next is Key pair (login) If you don't have a key pair create a new one , download it and save it on your machine, it will be used to login to your instance on the window power shell terminal assuming you are using a Windows laptop or Desktop computer. Then Under Network Setting Click Edit ,VPC leave it on default, move to Firewall(Security Groups) Create a Security Group, give it a name, i will give mine "techtestSG", Under Description i will give it same name "techtestSG" under the Inbound Security Group Rules, Type Change it to HTTP, then Add another Security Group Rule under that Type is SSH, go to the Source Tab add the info 0.0.0.0/0 then leave every other information on default , then click Lunch Instance on the lower right hand corner of the screen.You will get a confirmation with Success then click on your Instance ID to move to connecting your EC2 instance

Step 3.

on the Instance Summary Page Click on Connect, that is where you will get your credntials to connect to your Instance via the Window Power Shell , you can use a GitBash as well to do that or use the Linux Terminal Inside your AWS Profile , anyone is fine , but for this Project i will be using the Window power Shell terminal to connect to our EC2 Instance.

Step 4.

Once you have clicked Connect, Open your Terminal on your Windows's Machine then we will have to Ssh into our EC2 Instance , first what we have to do is go back to the Key Pair we downloaded earlier which is .pem file ricght click on it and click on copy as path , then head to our Terminal we SSH with this command ssh -i C:\Users\fred4\Downloads\Still.pem this is the path we copy for our .pem key pair file. So in essence our command goes thus (ssh -i C:\Users\fred4\Downloads\Still.pem ec2-user@ec2-184-72-16-191.us-west-1.compute.amazonaws.com), this command is to SSH into our EC2 Instance we created named "techtest" once you SSH you get a confirmation shown in the picture Below.

Step 5.

Once you have ssh into your Instance the next step is to enter root with command sudo su -, then run this command "yum update -y" this is command is to ensure all packages are up to date before installing new software on the server, also Helps avoid security vulnerabilities or bugs from outdated packages. after running that , we will run our next command which is "curl -fsSL https://get.docker.com -o get-docker.sh" this command is to Install Docker on the server , then we also run this command "yum install docker -y" to fully install docker on or Server. after Running the "yum install docker -y" we have to check if we have the latest Docker on our Server we run this command to check for that. "docker --version"

Step 6.

Once we confirm we have the latest docker on our Server, we proceed to the next step, which is run the command "systemctl start docker" This command starts the Docker service on a Linux system, so we are using this command to start our docker recently installed on our server. after running this command , the Next command we will run is "systemctl status docker" this command is used to check the status of our docker if it is running successfully, once you run the command you check the Tab for Active: then you see (Running) and the date and time, shows our docker is running successfully.

PS: Please note to come out of this process that shows the docker is running successfully , we Press the letter Q on our Keyboard, takes us back to the main terminal.

Step 7.

Once we have confirmed our Docker is running successfully , we proceed to the next step which is, to create a Directory, we will use this command "mkdir techbuilddir" this command will create a new directory or a folder for us .After we create our directory, we cd into our Directory with the command “cd techbuilddir” then next is to create a Dockerfile inside the directory so we use this command to create our Dockerfile "vim Dockerfile" this command will opened a text editor (Vim) to create or edit a file named Dockerfile inside that folder. for us to be able to add texts into this Dockerfile we will press letter "i' on our keyboard this gives us access to write inside our Dockerfile.

then we inside this Dockerfile we will build a small server that uses AlmaLinux 8, installs Apache, copies in our website (index.html), and starts a web server so people can visit our site through port 80. here is the Text that will be copied into our Dockerfile

Use the official CentOS 7 base image

FROM almalinux:8

Install the Apache HTTP server package from the CentOS repository

RUN dnf install -y httpd && dnf clean all

Copy the index.html file from the Docker build context to the default Apache document root directory in the container

COPY index.html /var/www/html/

Specify the command to run when the container starts, which starts the Apache HTTP server in the foreground

CMD ["/usr/sbin/httpd","-D","FOREGROUND"]

Expose port 80 to allow incoming HTTP traffic to the container

EXPOSE 80

Then we Save our Docker file and exit. After Saving and Exiting the Text Editor, We will now create our Index.html File, using the command "vi index.html" then add an index.html code in our file, we can get anyone one from ChatGPT

Step 8.

After Creating our index.html file, next thing is to Build our Base Image, command to build our image goes thus "docker build -t imagename . " Where by we change the image name to the specific name we want to use in creating our image, mine will be "techtestimg" so command goes thus "docker build -t techtestimg . " below image shows a successful image build. " then we run "docker images" to check if our image was built successfully.

Step 9.

After building our image, next step is to create and run our container, but before we create our container we will have to login our dockerhub account on the terminal first, the command to login our dockerhub on the terminal is "docker login" after we login, then the command to create our container is "docker run -d -p 80:80 --name containername image name" so in essence we run the command thus; docker run -d -p 80:80 --name techbrocon techtestimg After creating our container, to check if it was successful, we run the command "docker ps"

Before We Proceed to the Next Stage , we want to know if our application we deployed earlier is actually running, we head to our EC2 instance and copy our Public address and paste it on our web browser, once it opens our web app, then it means we are up and running.

Step 10.

After creating our Container successfully, The next thing to do is to CREATE ECR WITH A ROLE/PERMISSION, we head back to our AWS account then search for ECR in the search bar, Then we click on create a repository please note we will create our repository in a private repository not a public repository , give our repository a name mine will be "techbrorepo" leave every other thing on default and click create. then we click on our ECR repository named "techbrorepo" and WE CLICK ON VIEW PUSH COMMANDS. After that we navigate to the IAM page to create a role, we click on Role by the Left part of the Screen And then click on Create Role, Under Trusted entity type, we click on AWS Service , scroll down, under Used Case, we Drop down to EC2, then we click on Next we search for the role we wanna give to this EC2, We Search for Administrator and we check on the AdministratorAccess Role and we click Next, we give our Role Name i will use "EC2role" Leave every other thing in Default, then Press Create.

Step 11.

Next is to Create An access Key, we will Navigate to Users, Still on IAM, then select the IAM User, then under the Access Key 1 , Click Create Access Key. then under Access key best practices & alternatives check on Command Line Interface (CLI) , scroll down and check the confirmation Box , then Next, Skip next page and create Access Key.

Step 12.

After creating our Access Key , then we head back to our Terminal and we wil have to configure, command to configure is "aws configure" which will request for our Access Key ID we created earlier and Secret Access Key , Press enter , then Region , copy the region in which you started creating your resources mine is N. Calfornia which is us-west-1. Press enter, Default Output : NOne Anbd press Enter, then we Head back to the ECR repository we created to view our Push Commands. We copy the first Push command Under MacOS/Linux which is aws ecr get-login-password --region us-west-1 | docker login --username AWS --password-stdin 491085400228.dkr.ecr.us-west-1.amazonaws.com, paste in the terminal abnd enter, you will get a login Succeeded notice.

Step 13.

Then We head back to our Push command then copy the 3rd Command which is tag our image so we can push the image to this repository please note that since we have already built an Image we will skip step 2 on the push commands. so we copy command "docker tag techbrorepo:latest 491085400228.dkr.ecr.us-west-1.amazonaws.com/techbrorepo:latest"

PS: Because we have already built an image earlier with the name "techtestimg" we have to change the image name in the 3rd command to the one we built earlier so we do not encounter an error. so command will be changed to "docker tag techtestimg:latest 491085400228.dkr.ecr.us-west-1.amazonaws.com/techbrorepo:latest" instead of docker tag techbrorepo:latest 491085400228.dkr.ecr.us-west-1.amazonaws.com/techbrorepo:latest"

Then we push with the command "docker push 491085400228.dkr.ecr.us-west-1.amazonaws.com/techbrorepo:latest".

Step 14.

Next Step we will create Our Load balancer, Lets head to EC2 on AWS then scroll down to Load balancing and Select Load Balancer., click on create load balancer, then Select a Load Balancer , we will pick Application Load Balancer, under basic configuration give your load balancer a name i will use "techbro-alb", Under scheme should click "internet-facing" , Load balancer IP address type "IPv4" under Network mapping select the Default VPC , move to Availability Zones and subnets, check the availability zones available. Under Security Group Select the SG we created earlier to Deploy our resources, mine is "techtestSG" under Listeners and routing click create target group , under Basic configuration Select "Instances" Scroll down to Target group name and give it a name i will use "techbrogrp" Leave every other things in default and click next , then click Create Target Group .After creating we will scroll down and click Register Target. Select the Target group you created , Click Include as pending Below. and click register pending targets Then Back to our Load Balancer creation , Scroll to Listeners and routing and select the Target group we just created , Leave every other thing on default and click Create Load Balancer

Now that you’ve set everything up, it’s time to test whether the Load Balancer is working properly, we will copy the DNS address of our load balancer mine is techbro-alb-1464941157.us-west-1.elb.amazonaws.com paste it on a browser page to verify if it’s routing traffic properly, if everything is set up correctly, the Load Balancer will forward traffic to our EC2 instance and we should see our web app.

Step 15.

After creating our Load Balancing and verifying its working properly , We will Proceed to ECS , Elastic Container Service., Search for ECS at the Search bar on AWS portal, then click on Task Definitions , Create a Task Definition . Give our Task Definition a name , mine is "Task1" THEN WE NAVIGATE BACK TO IAM AND CREATE A ROLE FOR THIS TASK definition. Click on Roles , then Create Role, Trusted entity type AWS Service ,under Use case search FOR ELASTIC CONTAINER SERVICE , Under ELASTIC CONTAINER SERVICE, Select Elastic Container Service Task and Select Next. then Under Permission policies,Search for "AmazonECSTaskExecutionRolePolicy" tHEN CLICK ON NEXT. Give your Role a Name , Mine is "ECSExec, scroll down and click on Create Role. then Head back to our Task Definition in ECS and select the role we created. Scroll down to Container - 1 tab Input your Iae URI copied earlier mine is " 491085400228.dkr.ecr.us-west-1.amazonaws.com/techbrorepo then name : techtestimgs, Leave every other info on default and click Create.

Step 16.

We have successfully created a Task Definition , Now we head to Clusters to Create a New Cluster, Click on Create Cluster to Begin, Give your Cluster a Name Mine is "DevCluster2" Leave every other things on default and Click Create.

Step 17.

After creating Our Cluster, We will Click on the Cluster Created i.e "DevCluster2" , then scroll down to Service tab , then click to create a New Service. under Environment We should Have our Existing Cluster Which is "DevCluster2: Compute options Should be Capacity provider strategy, Capacity provider Should be FARGATE, Platform version Should Be Latest , Deployment configuration, Drop Down and Pick our Task Definition "Task1"" , Task definition revision Should be 1 , Service Name , i will give it "Techservice" Service type Should be Replica, Desired tasks Should be 2 Leave . Every other thing on Default, Drop down to Networking , Select the Default VPC and the Subnets , under Security Group, Create a New Security Group. Inbound rules for security groups TYPE: HTTP , PROTOCO:TCP PORT RANGE : 80 SOURCE: ANYWHERE. Leave every other thing in default and create. Then we head to the Tasks Tab to see if our Tasks are running , if they are shows we are good to go. To Locte Our Public IP to check our Web App if its Live, We go to our Cluster, , then Tasks Tab We click on one of the Tasks we Created Scroll down to Configuration , and get the Public IP: 13.56.164.56

Please Note that In this Project, Even though we Set up Load Balancing, we did not Integrate it , with our Cluster Services.

Wrapping Up
After Deploying a Dockerized application on ECS using EC2, Fargate, ECR, and an Application Load Balancer was quite the ride—from setting up the infrastructure to watching everything come together in the cloud. Each Clicks and Deployment was a learning experience, and hitting that final deploy button was really worth it.

If you made it this far reading my blogs, thanks for reading, I hope this steps uploaded Above can helps you on your own cloud journey. Feel free to drop your thoughts or questions in the comments—let’s connect and grow together. Until next time, happy Containerization. Ciao.

0
Subscribe to my newsletter

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

Written by

Stillfreddie Techman
Stillfreddie Techman