DEV Community

Cover image for Deploying a Dockerized Web Application with AWS ECS and Fargate
Gbenga Ojo-Samuel
Gbenga Ojo-Samuel

Posted on

Deploying a Dockerized Web Application with AWS ECS and Fargate

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

Image description

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

Image description

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

Image description

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

docker –version
Enter fullscreen mode Exit fullscreen mode

Image description

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

Image description

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 .
Enter fullscreen mode Exit fullscreen mode

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

Image description

Image description

B. Push image to Docker Hub
• Run command

docker push dockerusername/mywebapp 
Enter fullscreen mode Exit fullscreen mode

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

Image description

Image description

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

Image description

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

• 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

Image description

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

Image description

• 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

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

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

Image description

Image description

Image description

Thanks For Reading!!!

Top comments (0)