Deploying Containers on AWS ECS with Fargate

Introduction
Amazon Elastic Container Service (ECS) with AWS Fargate enables developers to deploy and manage containers without managing the underlying infrastructure. Fargate eliminates the need to provision or scale EC2 instances, providing a serverless approach to containerized applications.
This guide walks through deploying a containerized application on AWS ECS with Fargate using AWS CLI, Terraform, or the AWS Management Console.
1. Understanding AWS ECS and Fargate
✅ What is AWS ECS?
Amazon ECS (Elastic Container Service) is a fully managed container orchestration service that allows running Docker containers on AWS.
✅ What is AWS Fargate?
AWS Fargate is a serverless compute engine for ECS that removes the need to manage EC2 instances, providing:
- Automatic scaling
- Per-second billing
- Enhanced security (isolation at the task level)
- Reduced operational overhead
✅ Why Choose ECS with Fargate?
✔ No need to manage EC2 instances
✔ Pay only for the resources your containers consume
✔ Simplified networking and security
✔ Seamless integration with AWS services (CloudWatch, IAM, ALB)
2. Prerequisites
Before deploying, ensure you have:
- AWS Account with permissions for ECS, Fargate, IAM, and VPC
- AWS CLI installed and configured
- Docker installed to build container images
- An existing ECR (Elastic Container Registry) repository
3. Steps to Deploy Containers on AWS ECS with Fargate
Step 1: Create a Dockerized Application
First, create a simple Dockerfile for a Node.js or Python application.
Example: Node.js Dockerfile
dockerfileFROM node:16-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD ["node", "server.js"]
EXPOSE 3000Build and push the image to AWS ECR:
shaws ecr create-repository --repository-name my-app
docker build -t my-app .
docker tag my-app:latest <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/my-app:latest
aws ecr get-login-password --region <REGION> | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com
docker push <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/my-app:latestStep 2: Create an ECS Cluster
Use the AWS CLI to create a cluster:
shaws ecs create-cluster --cluster-name my-clusterOr use Terraform:
hclresource "aws_ecs_cluster" "my_cluster" {
name = "my-cluster"
}Step 3: Define a Task Definition for Fargate
The task definition specifies how the container runs.
Create a task-definition.js
{
"family": "my-task",
"networkMode": "awsvpc",
"executionRoleArn": "arn:aws:iam::<AWS_ACCOUNT_ID>:role/ecsTaskExecutionRole",
"cpu": "512",
"memory": "1024",
"requiresCompatibilities": ["FARGATE"],
"containerDefinitions": [
{
"name": "my-container",
"image": "<AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/my-app:latest",
"portMappings": [{"containerPort": 3000, "hostPort": 3000}],
"essential": true
}
]
}Register the task definition:
shaws ecs register-task-definition --cli-input-json file://task-definition.jsonStep 4: Create an ECS Service
Use AWS CLI:
shaws ecs create-service --cluster my-cluster --service-name my-service --task-definition my-task --desired-count 1 --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets=[subnet-xyz],securityGroups=[sg-xyz],assignPublicIp=\"ENABLED\"}"Or Terraform:
hclresource "aws_ecs_service" "my_service" {
name = "my-service"
cluster = aws_ecs_cluster.my_cluster.id
task_definition = aws_ecs_task_definition.my_task.arn
desired_count = 1
launch_type = "FARGATE" network_configuration {
subnets = ["subnet-xyz"]
security_groups = ["sg-xyz"]
assign_public_ip = true
}
}Step 5: Configure a Load Balancer (Optional)
If the service needs internet access, configure an Application Load Balancer (ALB).
- Create an ALB in your VPC.
- Add an ECS service to the target group.
- Configure a listener rule for routing traffic.
4. Monitoring & Scaling
🔹 Monitor ECS Service
Use AWS CloudWatch to monitor logs and performance.
shaws logs describe-log-groups🔹 Auto Scaling ECS Tasks
Configure an Auto Scaling Policy:
sh
aws application-autoscaling register-scalable-target \
--service-namespace ecs \
--scalable-dimension ecs:service:DesiredCount \
--resource-id service/my-cluster/my-service \
--min-capacity 1 \
--max-capacity 55. Cleaning Up Resources
After testing, clean up resources to avoid unnecessary charges.
shaws ecs delete-service --cluster my-cluster --service my-service --force
aws ecs delete-cluster --cluster my-cluster
aws ecr delete-repository --repository-name my-app --forceConclusion
AWS ECS with Fargate simplifies container deployment by eliminating the need to manage servers. By following this guide, you can deploy scalable, cost-efficient, and secure applications using serverless containers.
Comments
Post a Comment