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

Teslim LawalTeslim Lawal
6 min read

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

  1. 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).

  2. Connect to the Instance:

    • Use the EC2 Instance Connect feature to access your instance via the browser. Below is the result.

  1. Update the System and Install Docker:

    • Switch to the root user:

        sudo su
      

  1. Update the package manager:

     yum update -y
    
  2. Install Docker:

     curl -fsSL https://get.docker.com -o get-docker.sh
     sh get-docker.sh
    

  3. Start and verify Docker:

systemctl start docker
systemctl status docker

Step 2: Prepare Your Application for Containerization

  1. Set Up Application Directory:

    • Create and navigate to a new directory:

        mkdir myapp
        cd myapp
      
  2. Create a Dockerfile:

    • Use a text editor to create a Dockerfile:

        vi dockerfile
      
    • Insert the following content:

  3. Create an HTML File:

    • Create an index.html file and add your desired html content.

        vi index.html
      
  4. Build the Docker Image:

Step 3: Push Docker Image to Amazon ECR

  1. 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
      
  2. 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.

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

  1. Navigate to ECS in AWS Console:

    • Go to the Amazon ECS section.​
  2. Create a New Task Definition:

  3. Assign Task Roles:

    • Create and assign necessary IAM roles for task execution.​

Step 5: Set Up an Application Load Balancer (ALB)

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

  2. Configure Security Groups:

    • Assign a security group that allows inbound HTTP traffic on port 80.
  3. Set Up Target Groups:

    • Create a target group with the following settings:

      • Target type: IP

      • Protocol: HTTP

      • Port: 80

      • Health check path: /

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

  1. Create an ECS Cluster:

    • In the ECS console, create a new cluster named myapp-cluster.​
  2. 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.

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

0
Subscribe to my newsletter

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

Written by

Teslim Lawal
Teslim Lawal