Deploying a containerized web application using AWS ECR , ECS and Fargate
Gbenga Ojo-Samuel
Posted on November 20, 2023
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.
- Authenticate with your docker credentials
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”.
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.
- 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
- Build your Docker image with the commands
docker build -t mywebapp .
- check your image with the command
docker image list
- 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
- Image pushed to the project repository on ECR
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”.
- Give your cluster a name (mywebappcluster), Check the box for AWS Fargate since we are deploying a serverless architecture. Then click “Create”.
B. Define a Task Definition
- Click on the “Task Definitions” in the ECS service, then click “Create new Task Definition”.
- 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 .
C. Create an ECS service.
- Navigate to the ECS cluster and select the cluster created earlier and click on create in the services section.
- for the basic ECS configuration, you can follow the details as shown in the snippets below
- The service will create a task, you can check it by going to “Task”, and click on the Task ID
- The service will also create a Load balancer and a target group.
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.
- Test your website and ensure it functions as expected.
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”.
- Increase the desired tasks to 3 and click on “update”.
- 3 Tasks currently running in 3 AZs after the update is completed.
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!!!
Posted on November 20, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.