In this tutorial, I'll walk you through the process of deploying a simple Dockerized web app on AWS using Elastic Container Service (ECS) and Fargate.
Step 1: Create a Dockerized Web App
-
Write a Simple HTML Web App
Create an
index.html
file
<html>
<head>
<t1>This is simple website</t1>
</head>
<body>
<h1>Hello Users</h1>
<p>This is a simple website hosted using Docker on AWS ECS</p>
</body>
</html>
- Create a Dockerfile This file tells Docker how to package your web app into a container.
FROM nginx:alpine
COPY ./index.html /usr/share/nginx/html/index.html
EXPOSE 80
- Build the Docker Image Run the following command in your terminal:
docker build -t my-webapp .
Step 2: Push Your Docker Image to AWS ECR (Elastic Container Registry)
Requirements:
-Make sure you have AWS CLI installed.
-Configure the AWS CLI with your credentials by running:
aws configure
This command will prompt you to enter your AWS Access Key ID, Secret Access Key, default region, and output format.
-
Create a Repository in Amazon ECR
Go to Amazon ECR in the AWS Console and create a new repository called
my-simple-website
.You can choose to make this repository private or public based on your needs. - Authenticate Docker to AWS ECR Use the following command to log in:
aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <ECR_URI>
- Tag the Docker Image Tag your image for ECR:
docker tag my-webapp:latest <ECR_URI>/my-webapp:latest
- Push the Docker Image to ECR Finally, push your image:
docker push <ECR_URI>/my-simple-website:latest
Step 3: Set Up ECS and Fargate
- Create an ECS Cluster Go to the Amazon ECS Console and create a new Fargate cluster where your Docker containers will run.
-
Create a Task Definition
In ECS, create a Task Definition for Fargate using the image you just pushed to ECR (
<ECR_URI>/my-simple-website:latest
). Set memory and CPU configurations as needed.
- Create an ECS Service After defining the task, create an ECS Service to manage the running tasks (containers).
You can configure the ALB with a target group for your ECS service.
Make sure that the security group associated with your ALB has a rule that allows inbound traffic on the desired ports (e.g., HTTP port 80 or HTTPS port 443).
Step 4: Access Your Web App
Get the Public URL or dns of the Load Balancer
This URL is how you will access your web app.Open the URL in Your Browser
When you navigate to the URL, you should see your web app running with the message "Hello World!" displayed.
Conclusion
Deploying a Dockerized web app on AWS using Elastic Container Service (ECS) and Fargate is an efficient way to manage your applications. This tutorial walked you through creating a simple HTML web app, building a Docker image, pushing it to Amazon Elastic Container Registry (ECR), and setting up an Application Load Balancer (ALB) for internet access. With ECS and Fargate, you can easily scale and manage your services, allowing you to focus on development without the hassle of server management.
Top comments (0)