Deploying a containerized web application using AWS ECR , ECS and Fargate
Tags: webdev, devops, aws, docker
Published: 2023-11-20 20:08:12

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xl5u7jnjrvt3f26expwv.png)

- Authenticate with your docker credentials
 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wv3q9f47ish2b2do5eug.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5er7b5dhc7izryeib5my.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o7xxe5fpokr5wuh9ii2c.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9wu3qlinldpo10iz9re.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/809yx4m6v2ongsh9bhc1.png)

- 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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w4nbmd7v066zvu2a3jrk.png)

- Build your Docker image with the commands
 

```
docker build -t mywebapp .
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gcu5vt9yrc9yegv0o3hh.png)

- check your image with the command 

```
docker image list
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/afqukd8y2mno57v0l64a.png)

- 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 description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7jo2j2klwt0gzbpfzemm.png)

- Image pushed to the project repository on ECR


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/akqmd7nzxu6qvs44m6nq.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x499zkc3khgbhnntt8w1.png)


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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/flclx8jjieho0cnwkqtx.png)

B. Define a Task Definition

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihot7x62uhk27b73c3sj.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x8cv7gjhb99x9b1tdhj6.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxjj5oq1v4qoluwd88dn.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oq1zezonzfy6r13rdcg8.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tier9korkd5dw43oj7q2.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1nufmkqil55bd4thj9nl.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cvapw8q9u1wyegh7tv0d.png)


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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9uim0iu9of2o1ovcsc7p.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4ra4szwffej5p3ssp51.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqld4j8qxvxqls2so66v.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5b6np6qdtp5r78g2iq5.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xgmzxvxr2a4uw4t1dahm.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/harbo8qrnpfc378fkgvv.png)
 

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sf3m2kjmrp5uwngjqc7i.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5d8zfigjc3ytt3a38r9y.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9m35v2djz3qsebo9d85s.png)

- Test your website and ensure it functions as expected.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qfmvhgd0n1p3qh080crd.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvdnmng2sotr60rnyxmn.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8c2oqkymt9kl15kl5ng.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qg1ze3pn79hdgrqie9as.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6uc0b1rgl2wy14jf0kh.png)

**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!!!