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

Daniel ErebiDaniel Erebi
4 min read

PRE-REQUISITE:

  1. Learn: L26a on my previous Article: How to deploy a Linux EC2 instance on AWS using the link below

https://hashnode.com/post/cm9ncn0zl000e0ai988ojh2wj

  1. Connect to your Instance either through Instance connect of ssh client

In this Project I connected using the SSH client by opening PowerShell and input the ssh command as shown below.

  1. INSTALL DCOKER.

a. Create a folder called ( dockerproject ) to be able to house all the html and docker commands that will be used to push an image by running the following command:

—> mkdir dockerproject

—> cd dockerproject

—> ls

—> cd dockerproject

—> ls

—> nano index.html ( Input the index.html scripts programed for your Service into the folder by the command

—> cat index.html ( Check that the command is properly save by the command )

—> cat dockerfile

d. Input the docker command for the service into the folder by copy and pasting the following command and cat dockerfile to be sure the commands are stored

e. After successfully installing docker, we use the

—> sudo systemctl start docker to start docker and

—> sudo systemctl enable docker to enable docker.

—> sudo systemctl status docker to know the status of docker.

To comeout press on your keyboare ( ctlr z )

f. From the diagram below, you would realize that docker is started, active and running.

g. Go to the AWS Console - IAM - User - Create access keys - Use Case (choose CLI) - Select confirmation box - Next - Create access keys.

h. Return to PowerShell and type aws configure

CREATE AN ELASTIC CONTAINER REGISTRY

a. You can do this by typing aws ecr create-repository —repository-name <repository name>

b. 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.

c. Copy these commands one after the other and run them on Powershell.

NOTE: The aws modified link will include the repocitory code and region seen below

aws ecr get-login-password --region eu-north-1 | docker login --username AWS --password-stdin 605134468004.dkr.ecr.eu-north-1.amazon.com

Image successfully pushed.

  1. CREATE A LOAD BALANCER

    a. Go to the EC2 instance dashboard and select Load Balancing - Load Balancers - Create load balancer.

    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.

    Move to Advanced health check settings and scroll to Healthy threshold.

    Select your instance, next select Create Target group.

    Return to Listener and routing so that you can add the load balancer.

    1. CREATE A TASK ROLE

a. Open IAM - Roles - Create role.

b. Under Trusted entity type, choose AWS Service.

  1. CREATE ELASTIC CONTAINER SERVICE

a. Search ECS - Task Definition - Create new task definition.

Click Clusters - Create Cluster

Leave Environment section as default and head to Deployment configuration. Choose the Task definition you created under Task definition family. Next give a Service name.

Move to Networking - Security group - use existing security group - select the security group you created.

Head to the Load balancing section

I had Error during my Deployment due to 3 failed tasks and unhealthy target status as shown below. So I repeated STEP 7 again and I was able to Deploy successfully.

From the images above, every deployment was successful.

Now I picked the IPv4 address of my EC2 instance and opened it and the result is the project.

But there is an error with the Public IPV4 address: 52.228.7.165

Also there is an error with the Private IPV4 address: 172.31.20.0

I went a head to get the DNS of my load balancer to launch the link and there is also an 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 in the diagram 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 2. That means the target group is finally associated with the instance.

Now let’s go back and check with the DNS names. And there we have it.

But I still dont have the website displayed …Why???

0
Subscribe to my newsletter

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

Written by

Daniel Erebi
Daniel Erebi

Daniel Erebi is a dynamic and forward-thinking professional currently making strides as an Intern in Cloud Computing Engineering, driven by a passion for innovation and a commitment to transitioning into the tech industry. With a robust foundation as an Automation Engineer in the oil and gas sector, Daniel brings a unique perspective to cloud computing, combining hands-on technical expertise with a strategic mindset honed in high-stakes industrial environments. A registered Electrical/Electronic Engineer holding a Master’s Degree, Daniel has consistently demonstrated excellence in problem-solving, systems optimization, and cross-disciplinary collaboration. My Experience in automation engineering involved designing and implementing advanced control systems, fostering skills in programming, data analysis, and workflow automation—competencies now being leveraged to bridge the gap between industrial engineering and cutting-edge cloud solutions. Currently focused on mastering cloud infrastructure, Daniel excels at evaluating and recommending cloud service providers, tools, and architectures to optimize performance, security, and cost-efficiency. My proficiency in programming languages and DevOps practices positions me to design scalable, resilient cloud systems, while my analytical approach ensures alignment with organizational goals. Committed to becoming a top-notch Cloud Engineer, Daniel Erebi is actively expanding his knowledge in platforms like AWS, Azure, and Google Cloud, as well as technologies such as Kubernetes, Terraform, and CI/CD pipelines. My engineering background, paired with a relentless curiosity for emerging tech, equips me to tackle complex challenges in cloud migration, hybrid environments, and IoT integration. Daniel Erebi is driven by the belief that the fusion of technical skill, industry experience, and lifelong learning is key to shaping the future of cloud computing. With a proven track record of adaptability and a vision for innovation, Daniel Erebi is poised to make meaningful contributions to the tech landscape. Thank you for your interest in Daniel Erebi's journey.