Introduction
This project focuses on deploying a containerized application using Amazon ECS Fargate. ECS (Elastic Container Service) is AWS’s container orchestration platform, and Fargate is a serverless compute engine that allows us to run containers without managing the underlying infrastructure.
Why is it useful?
Prerequisites
Before starting, ensure the following tools, accounts, and permissions are ready:
AWS Account (Free Tier)
Sign up at aws.amazon.com/free.
AWS may request a payment method (credit card), but if you remain within the free tier limits, you will not be charged.
AWS CLI (Command-Line Interface)
Installed and configured on your local machine.
Installation guide.
Run aws configure to set up your AWS credentials (Access Key, Secret Key, default region).
IAM Permissions
An IAM user with AdministratorAccess or a similar policy that grants permissions to create and manage ECS, Fargate, ECR, and related services.
Enabled Services
ECS (Elastic Container Service)
ECR (Elastic Container Registry)
CloudWatch (for logging, optional but recommended)
Important: Throughout this guide, we assume a sample Docker container image (e.g., a simple “Hello World” web app) is ready to be pushed to ECR. If not, we will include instructions on how to quickly build one.
Step-by-Step Implementation
We will combine both Console (GUI) and CLI methods in each step.
Create an ECR Repository
Console Steps
Go to the AWS Management Console and navigate to Elastic Container Registry (ECR).
Click Create repository.
Choose Public or Private repository (choose Private if you only want your images accessible within your account).
Name the repository, for example: my-fargate-app.
Click Create repository.
CLI Steps and Explanation
What it does: This command creates a new private ECR repository named “my-fargate-app” under your AWS account.
Build and Push Your Docker Image to ECR
Assume you have a simple Dockerfile that prints “Hello from Fargate!” when accessed.
Sample Dockerfile (place this in a directory, e.g., my-app/)
Create a simple index.html with your message:
Console Steps
(Optional) Verify your ECR repository exists by going to ECR in the AWS Console.
Copy the Repository URI from the repository’s detail page (e.g., ********.dkr.ecr.us-east-1.amazonaws.com/my-fargate-app).
CLI Steps and Explanation
Authenticate Docker to ECR:
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin ********.dkr.ecr.us-east-1.amazonaws.com
What it does: Retrieves a temporary authentication token from ECR and logs your local Docker client into your ECR registry.
Create an ECS Cluster (Using Fargate)
Console Steps
Go to ECS in the AWS Console.
Click Clusters → Create Cluster.
Select Networking Only (Fargate) if prompted.
Choose a Cluster name, for example: fargate-cluster.
Leave default settings (or configure VPC/Subnets if you like).
Click Create.
CLI Steps and Explanation
What it does: Creates an ECS cluster named “fargate-cluster” that will use AWS Fargate as the launch type.
Create a Task Definition for Fargate
Console Steps
In ECS Console, go to Task Definitions → Create new Task Definition.
Select Fargate as the launch type.
Enter a Task Definition Name, e.g., fargate-task.
Under Task Role and Execution Role, choose or create the appropriate IAM roles (the console can auto-create if you allow it).
Configure container details:
Container name: my-fargate-app-container
Image: ********.dkr.ecr.us-east-1.amazonaws.com/my-fargate-app:latest
Port mappings: 80 (TCP)
CPU / Memory: Choose minimal (e.g., 0.5 GB or 1 GB) to stay within free tier.
Click Create.
CLI Steps and Explanation :
aws ecs register-task-definition \
--family fargate-task \
--requires-compatibilities FARGATE \
--network-mode awsvpc \
--cpu "256" \
--memory "512" \
--execution-role-arn <EXECUTION_ROLE_ARN> \
--task-role-arn <TASK_ROLE_ARN> \
--container-definitions '[
{
"name": "my-fargate-app-container",
"image": "********.dkr.ecr.us-east-1.amazonaws.com/my-fargate-app:latest",
"essential": true,
"portMappings": [
{
"containerPort": 80,
"protocol": "tcp"
}
]
}
]'
(You will need valid ARNs for the executionRole and taskRole. The console can create them automatically, or you can create them in IAM.)
Run the Task (Create a Service)
Console Steps
In the ECS Console, go to your fargate-cluster → Services → Create.
Launch type: Fargate.
Task Definition: fargate-task (the one you just created).
Service name: fargate-service.
Number of tasks: 1 (to stay within free tier usage).
Deployment type: Rolling update (default).
VPC and Subnets: Choose existing default VPC and subnets.
Assign public IP: ENABLED (so we can access the container from the internet).
Click Next and Create Service.
CLI Steps and Explanation:
aws ecs create-service \
--cluster fargate-cluster \
--service-name fargate-service \
--task-definition fargate-task \
--desired-count 1 \
--launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[<YOUR_SUBNET_ID>],securityGroups=[<YOUR_SECURITY_GROUP_ID>],assignPublicIp=ENABLED}"
(You need to replace <YOUR_SUBNET_ID> and <YOUR_SECURITY_GROUP_ID> with actual IDs. You can find them in the AWS Console under VPC.)
Verifying and Testing the Project
Common Issues and Troubleshooting
Image Pull Failure:
Make sure you tagged and pushed your Docker image correctly.
Check that the task definition references the correct image URI.
Permissions / IAM Role Errors:
Ensure the Execution Role and Task Role are set properly in the ECS console or in your CLI command.
The AWS console can auto-generate roles if needed.
No Public IP / Not Reachable:
Check that you enabled Auto-assign Public IP in your network configuration.
Verify that your Security Group allows inbound traffic on port 80.
Staying Within Free Tier:
Confirm you selected minimal CPU/Memory configurations.
Stop or delete your service once you have tested to avoid incurring charges if you exceed free tier limits.
AWS CLI Version Mismatch:
If you see errors like “Unknown parameter,” ensure your AWS CLI is up to date.
Conclusion
We have successfully created and deployed a containerized application on AWS ECS Fargate. We built our own Docker image, pushed it to ECR, configured a Fargate task definition, and finally ran the service in a secure, scalable way—all while staying within the free tier. We have learned how to:
By following these steps, we have gained valuable hands-on experience in container orchestration on AWS and are better prepared to deploy more advanced and scalable applications in the future.
Popular Projects
What is Cloud Computing ?
Cloud computing delivers computing resources (servers, storage, databases, networking, and software) over the internet, allowing businesses to scale and pay only for what they use, eliminating the need for physical infrastructure.