DEV Community

Cover image for Deploying a containerized web application using AWS ECR , ECS and Fargate
Gbenga Ojo-Samuel
Gbenga Ojo-Samuel

Posted on

Deploying a containerized web application using AWS ECR , ECS and Fargate

Introduction:
In my last article, i deployed a web application using AWS Elastic Container Service (ECS) and Fargate. The docker image used was pulled directly from a docker hub repository. Please see below link for the article.

https://dev.to/gbenga700/deploying-a-dockerized-web-application-with-aws-ecs-and-fargate-29bb

In this follow up article, i will walk you through the steps to deploy the same web application using AWS ECR as image repository while we deploy the application on AWS ECS using Fargate.

Prerequisites:
Before we get started, ensure you have the following Prerequisites:

  • Read the preceding article by clicking the link shared above
  • An AWS account: You will need an AWS account to access the ECS and ECR services.
  • Docker: You should have Docker installed on your local machine to build container images.
  • An application: Prepare your website code in a Git repository or as a source code package.

Step 1: Setting up our project environment.

  • Clone the project repo from GitHub with link below

https://github.com/7hundredtech/Dockerized-WebApp-on-AWS-ECS-Using-Fargate

  • Navigate to your project directory and open your terminal.

Image description

  • Authenticate with your docker credentials

Image description

Step 2: Create a Repository on Amazon Elastic Container Registry (ECR)

A. Creating an Amazon ECR Repository

  • In the AWS Management Console, navigate to the Amazon ECR service and click on “Create repository”.

Image description

Image description

Image description

B. Authenticate your docker client with the registry

  • Select the box on the created repository and click on view push commands. These will enable us connect our ECR repository to our local machine.

Image description

  • Retrieve an authentication token and authenticate your Docker client to your registry.


aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin 969446871231.dkr.ecr.us-east-2.amazonaws.com


Enter fullscreen mode Exit fullscreen mode

Image description

  • Build your Docker image with the commands


docker build -t mywebapp .


Enter fullscreen mode Exit fullscreen mode

Image description

  • check your image with the command


docker image list


Enter fullscreen mode Exit fullscreen mode

Image description

  • Run the following command to push this image to your newly created AWS repository


docker push 969446871231.dkr.ecr.us-east-2.amazonaws.com/mywebapp:latest



Enter fullscreen mode Exit fullscreen mode

Image description

  • Image pushed to the project repository on ECR

Image description

Step 3: Amazon Elastic Container Service (ECS) Configuration.

A. Creating an Amazon ECS Cluster

  • In the AWS Management Console, navigate to the Amazon ECS service and click on “Create Cluster”.

Image description

  • Give your cluster a name (mywebappcluster), Check the box for AWS Fargate since we are deploying a serverless architecture. Then click “Create”.

Image description

B. Define a Task Definition

  • Click on the “Task Definitions” in the ECS service, then click “Create new Task Definition”.

Image description

  • Update the task definition configuration as shown in the snippets below and ensure that image URI is pointing to the ECR repository we created earlier. Then Click on create .

Image description

Image description

Image description

Image description

Image description

C. Create an ECS service.

  • Navigate to the ECS cluster and select the cluster created earlier and click on create in the services section.

Image description

  • for the basic ECS configuration, you can follow the details as shown in the snippets below

Image description

Image description

Image description

Image description

Image description

  • The service will create a task, you can check it by going to “Task”, and click on the Task ID

Image description

  • The service will also create a Load balancer and a target group.

Image description

Image description

Step 4: Test and Scale.

  • At this point, your website is deployed to AWS ECS. You can access it through the ALB's DNS endpoint.

Image description

  • Test your website and ensure it functions as expected.

Image description

To scale your application, adjust the desired count of your ECS service or use auto-scaling based on metrics like CPU or memory utilization.

We will be updating our service to run 3 tasks. this will deploy our web application in 3 availability zones and the Load balancer will load balance traffic across the 3 AZs.

  • Navigate to the service created in your ECS cluster and click on “Update Service”.

Image description

  • Increase the desired tasks to 3 and click on “update”.

Image description

  • 3 Tasks currently running in 3 AZs after the update is completed.

Image description

Image description

Conclusion
By following the steps outlined in this guide, we have successfully deployed a reliable and scalable web applications Embracing the power of containerization with AWS ECR, AWS ECS and Fargate.

NB: Don't forget to CLEAN UP!!!

Thanks for reading!!!

Top comments (0)