Deploy an Application to Amazon ECS With EC2 | Docker | ECR | Fargate & Load balancer

Emmanuel EniadeEmmanuel Eniade
6 min read

You are all welcome to my blog and many. Today I would like to share with you instructions on how to deploy a web application to Amazon ECS with EC2, Fargate and load balancer but before we dive in I would like to provide some basic information regarding some of the Amazon services which we will be using. If you are ready, please grab a cup of coffee, relax and follow me.

Amazon ECS

Amazon Elastic Container Service (Amazon ECS) is a fully managed container orchestration service that helps you to more efficiently deploy, manage, and scale containerized applications. It deeply integrates with the AWS environment to provide an easy-to-use solution for running container workloads in the cloud and on premises with advanced security features using Amazon ECS Anywhere. Amazon ECS delivers the easiest way to build, deploy, and manage containerized applications at any scale on AWS

Amazon ECR

Amazon Elastic Container Registry (Amazon ECR) is a fully managed container registry offering high-performance hosting, so you can reliably deploy application images and artifacts anywhere.

Amazon FARGATE

AWS Fargate is a serverless, pay-as-you-go compute engine that lets you focus on building applications without managing servers. Moving tasks such as server management, resource allocation, and scaling to AWS does not only improve your operational posture, but also accelerates the process of going from idea to production on the cloud, and lowers the total cost of ownership.

Without further ado, lets build this application together. So I would like to do the following in order for us to deploy our application successfully;

  1. Create EC2 instance and login into it

  2. Install docker on EC2 with command

  3. Create ECR with role permission and build container for the application

  4. Create Load balancer

  5. Create ECS service to create task role, task definition and cluster

1. CREATE AN EC2 INSTANCE

Please check How to set up Two EC2 on EFS for step by step guide on how to create EC2 instance. Under Network settings, click edit, create a new Security group and add an inbound rule. For Type, select HTTP and select 0.0.0.0/0 for the source.

2. CONNECTING TO AN EC2 INSTANCE

To connect to your instance, there are many ways to do this but in this case, I have chosen to my instance through SSH and with this you need to use your key pair as demonstrated below;

Next, type the commands as seen in the diagram below . The sudo su command takes you to the root user and yum update -y, updates your Amazon Linux.

3. DOCKER INSTALLATION ON AN EC2 INSTANCE

To install docker, you have to run the following commands. curl -fsSL https://get.docker.com -o get-docker.sh. Next, run install docker -y

After successfully installing docker, we use the systemctl start docker to start docker and systemctl status docker to know the status of docker.

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

To continue with your commands, type Q and it takes you back to your root

4. CREATING DIRECTORY FOR DOCKERFILE AND HTML

i. From here, create directory with the mkdir <name of directory> and cd <name of directory into the directory

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

iii. Type vi Index.html to create your html file. Put in your code, save and exit.

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

f. Return to PowerShell and type aws configure

4. CREATING ELASTIC CONTAINER REGISTRY

i. Thus can be done either going to the console or by typing cli command aws ecr create-repository --repository-name <repository name>

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

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

aws ecr get-login-password --region ap-southeast-2 | docker login --username AWS --password-stdin 421498061926.dkr.ecr.ap-southeast-2.amazonaws.com

iv. having built our image and pushed to the registry, then lets build our container with this command;

docker run -d -p 80:80 —name containername <image name>

5. CREATING LOAD BALANCER

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

ii. Give your load balancer a name. Leave the other parts as default and move to Network mapping.

iii. Under Network mapping, select the VPC. In my case, it was the default VPC and I selected 3 AZs which automatically attached the subnets.

iv. Under Security groups, click on the drop down arrow and select the security group you created when creating the EC2.

v. Under Listeners and routing, select Create Target group.

vi. Give target a name

vii. Under Advanced, scroll down to healthy threshold and finally scroll down click on next to create target.

viii. Select your instance, scroll down and select Create Target group.

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

x. Next scroll down , select create for the load balancer

6. CREATING ELASTIC CONTAINER SERVICE

To create this successfully, the following need to be created under ECS service

i. Task role

ii. Task definition

iii. Cluster

iv. Service

Task role creation

a. Go to IAM, click on role

b. Under Trusted entity type, choose AWS Service.

  1. CREATE ELASTIC CONTAINER SERVICE

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

b. Infrastructure requirements, go to Task Role and Task execution role and select the role you created for ECS.

c. 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 in the diagram.

d. Click create

e. Click Clusters - Create Cluster

f. Leave every other sections as default and click Create.

g. Click the cluster that you have just created, scroll down and click Services - Create.

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

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

j. Move to Load balancing

Click create and service is successfully created.

Everything looks good from here

Now to see our website, from the service overview page click on one of the task as shown

There you have it guys,

I hope you enjoyed it. Incase you have any question,please reach out.

Thanks

Emmanuel

0
Subscribe to my newsletter

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

Written by

Emmanuel Eniade
Emmanuel Eniade