Deploying a Dockerized Web Application with AWS ECS and Fargate
Tags: webdev, aws, git, docker
Published: 2023-11-08 19:09:44

**Introduction:**
In today's fast-paced digital world, deploying and scaling web applications efficiently is critical. One popular approach to achieving this is by using Docker containers and Amazon Web Services' (AWS) Elastic Container Service (ECS). In this article, i will walk you through the steps to deploy a website with Docker and AWS ECS, demonstrating how this combination can simplify the deployment process, improve scalability, and enhance overall application management. 

**Definition of Terms:**

1. **Docker:** Docker is a containerization platform that allows developers to package applications and their dependencies into portable, isolated containers. Containers are lightweight, consistent, and can run on any system with Docker installed, making it easier to develop, ship, and deploy applications.

2. **AWS ECS (Elastic Container Service):** AWS ECS is a fully managed container orchestration service that simplifies the deployment, management, and scaling of Docker containers. It allows you to run and scale containers on a cluster of Amazon EC2 instances or AWS Fargate, making it a versatile choice for deploying containerized applications on AWS.

3. **Fargate:** AWS Fargate is a serverless compute engine for containers. When using the Fargate launch type in ECS, you don't need to manage the underlying EC2 instances. Fargate abstracts infrastructure management, allowing you to focus solely on deploying and running containers. It's a convenient choice for users who want a serverless experience and don't want to deal with EC2 instances directly.

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

1. An AWS account: You will need an AWS account to access the ECS service.
2. Docker: You should have Docker installed on your local machine to build container images.
3. An application: Prepare your website code in a Git repository or as a source code package.

**Step 1: Setting up our project environment**
A. **Set up a Git repository for the project.**
• Login to your GitHub account (create one from www.github.com)
• Create a new repository for the project. (Dockerized-webApp-on-AWS-ECS-using-Fargate)
• Upload the Dockerfile and the web Application codes to the repository created above
• The Web Application codes consist of: 
 Basic HTML code in the public folder
 Web application code in Node.js – (server.js) and a (package. json) file for the Node.js project with necessary dependencies.


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


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

**B. Cloning the repository on your local machine using Git Bash**
• Open Git Bash to the location where you intend to store the project files.
• Clone the repository to your local machine. Copy the code from the repository and run on your bash terminal.


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


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

• Change into the project directory and run the _**ls**_ command to see your project files.


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

**C. Authenticating with your Docker Credentials**
• Check if docker is installed on your local machine with 

```
docker –version
```
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmcz88ikb01m3yg4w3y5.png)

• Authenticate with your docker credentials. (Note that you will be required to provide your Docker hub username and password)


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

**Step 2: Build Docker Image and Push image to Docker Hub.**
**A. Build docker image from the Dockerfile**.
• Run command

```
docker build  -t dockerusername/mywebapp .
```
Note: Replace (dockerusername) with your docker hub
 username. (mywebapp) is the image name.(you can have a different name).


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


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

**B. Push image to Docker Hub**
• Run command

```
docker push dockerusername/mywebapp 
```
Note: Replace (dockerusername) with your docker hub username. (mywebapp) is the image name.(you can have a different name).


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


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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2878uraqznzrb0evob0x.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/e2lbl4qbvmi9oxh6ztbr.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/6xt4e1mrvt5ud01svgif.png)


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

**B. Defining a Task Definition**
• A task definition describes how your Docker container should be launched within the ECS cluster.
• 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/61jggwtnhdxlbuxm77fk.png)

**• Task definition configuration:**

• **Task definition family** - specify a unique name for the task definition.
• **Launch type** – Select AWS Fargate
• **Operating system/Architecture**- select Linux/ARM64
• **Task size** - choose the CPU and memory values to reserve for the task.
• **Network mode** - The default is awsvpc mode.
• **Task roles** -
 **Task IAM Role** - A task IAM role allows containers in the task to make API requests to AWS services (you can create if needed)
 **Task execution Role** - A task execution IAM role is used by the container agent to make AWS API requests on your behalf. If you don't already have a task execution IAM role created, it will be created automatically.

• **Container to define in your task definition.**
 **Name** - enter a name for the container.
 **Image URI** - specify the repository using the (repository-url/image:tag)
 **Port mapping** - choose the port mapping to use for the container.

• Leave other configuration as default and click “Create”.


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


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

**C. Creating an ECS service to deploy the Docker container.**
• Go back to the ECS cluster and select the cluster created earlier.


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


• Click on “Create” in the “Services” section.
• **ECS Service Basic configuration**-
 **Existing Cluster** – select our existing cluster.
 **Compute Options** – Select Launch type.
 **Launch type** - select Fargate
 **Platform Version** - Select LATEST.
 **Application type** - Select Service.

**• Task definition -**
 **Family** – Select our Task definition created earlier.
 **Service Name** – Choose a unique name for the service. 
 **Service Type** – Choose Replica
 **Desired Task** – Choose the number of tasks to run (we will start with 1)

**• Networking.**
 **VPC** – Select default VPC or your custom VPC.
 **Subnet** - Select the subnets to deploy into. (For high Availability we will be updating the service to deploy in 3 subnets)
 **Security Group** – Select and existing security group or create a new one. But ensure all required ports are allowed.

• **Load Balancing.**
 **Load balancing type** – Select Application Load Balancer to create a new load balancer.
 **Name** – Give your Load balancer a unique name. 
 **Container** – Select our container to load balance.
 **Listener**- Create a new listener port and protocol (port:80 protocol: HTTP).
 **Target Group** – Create a new target group, specify name and leave other as default.
• Leave other configuration as default and click “Create Service”.


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


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


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


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


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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b666kx4q259jmrdb4xdo.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/0couwg1jx6dm3s2avi6n.png)

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


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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r0fofcohjthviqi0i9pq.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/1tbc6jnfctns7127zctw.png)

Test your website and ensure it functions as expected.

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

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


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

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


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

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


Conclusion
Deploying a website with Docker and AWS ECS streamlines the deployment process, enhances scalability, and simplifies application management. By following the steps outlined in this guide, you can easily set up a reliable and scalable infrastructure for your web applications. Embrace the power of containerization and AWS ECS to take your web development to the next level.

The code for this project can be found in the link below:
https://github.com/7hundredtech/Dockerized-WebApp-on-AWS-ECS-Using-Fargate

Clean up!!!
Update your ECS service by changing the desired task to zero. With this no task is running and you are not charged.


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

Then you can delete the service, the ECS cluster and the Task definition 


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


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



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

Thanks For Reading!!!