ECS & Fargate — Containers
Run Docker containers on AWS without managing servers — task definitions, services, and load-balanced deployments
Skills taught: aws.ecs, aws.fargate, aws.task-definition, aws.ecs-service, aws.ecr, aws.alb
ECS and Fargate — containers without Kubernetes
ECS (Elastic Container Service) runs your Docker containers on AWS. Fargate is the launch type where AWS manages the underlying EC2 instances for you — you define what container to run, how much CPU and memory it needs, and Fargate figures out where to place and run it. No cluster to manage, no server to patch. Think of it as 'serverless containers': the container runs as long as needed, you're billed only for CPU and memory consumed.
Core ECS concepts
CLUSTER
└── SERVICE (keeps N tasks running, handles rolling deploys)
└── TASK (one running instance of a task definition)
└── CONTAINER (your Docker container)
TASK DEFINITION: like a docker-compose.yml for one task
- Which Docker image to run
- CPU and memory limits
- Environment variables and secrets
- Port mappings
- Log configuration
- IAM task role (permissions for the container)
SERVICE: keeps tasks healthy
- 'Run 3 tasks at all times'
- Replaces failed tasks automatically
- Integrates with ALB (Application Load Balancer) for traffic routing
- Performs rolling deployments: start new tasks, stop old ones
FARGATE vs EC2 LAUNCH TYPE:
Fargate: AWS manages the server — you pay per CPU/memory used
EC2: You manage a fleet of EC2 instances, ECS schedules containers on them
→ Use Fargate for most applications
→ EC2 for very high throughput or GPU workloads
Deploying a Node.js app to Fargate
# 1. Push image to ECR (Elastic Container Registry)
aws ecr create-repository --repository-name myapp
ECR_URI=$(aws ecr describe-repositories --repository-names myapp \
--query 'repositories[0].repositoryUri' --output text)
aws ecr get-login-password | docker login --username AWS --password-stdin $ECR_URI
docker build -t myapp .
docker tag myapp:latest $ECR_URI:latest
docker push $ECR_URI:latest
# 2. Create task definition (JSON or via console)
cat > task-def.json <<EOF
{
"family": "myapp",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "256",
"memory": "512",
"executionRoleArn": "arn:aws:iam::123456789:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::123456789:role/myapp-task-role",
"containerDefinitions": [{
"name": "myapp",
"image": "${ECR_URI}:latest",
"portMappings": [{"containerPort": 3000, "protocol": "tcp"}],
"environment": [
{"name": "NODE_ENV", "value": "production"}
],
"secrets": [
{"name": "DATABASE_URL", "valueFrom": "arn:aws:secretsmanager:...:myapp/db-url"}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/myapp",
"awslogs-region": "ap-south-1",
"awslogs-stream-prefix": "ecs"
}
}
}]
}
EOF
aws ecs register-task-definition --cli-input-json file://task-def.json
# 3. Create cluster
aws ecs create-cluster --cluster-name myapp-cluster
# 4. Create service
aws ecs create-service \
--cluster myapp-cluster \
--service-name myapp-service \
--task-definition myapp \
--desired-count 2 \
--launch-type FARGATE \
--network-configuration 'awsvpcConfiguration={subnets=[subnet-xxx],securityGroups=[sg-xxx]}'
Deploying updates — rolling deployments
# In your GitHub Actions workflow, after building and pushing the new image:
- name: Deploy to ECS
run: |
# Force new deployment — ECS pulls the latest image and replaces tasks
aws ecs update-service \
--cluster myapp-cluster \
--service myapp-service \
--force-new-deployment
# Wait for deployment to complete (all new tasks healthy)
aws ecs wait services-stable \
--cluster myapp-cluster \
--services myapp-service
# ECS rolling deploy behavior (default):
# With 2 desired tasks:
# 1. Start a new task with the new image
# 2. Wait for it to pass health checks
# 3. Stop one old task
# 4. Start another new task
# 5. Wait for health check
# 6. Stop the last old task
# → Zero downtime if health checks are configured
Tip: Use AWS Secrets Manager instead of environment variables for secrets. In the task definition, reference secrets with the secrets field (not environment). The secret value is injected as an env var at container start but never stored in the task definition JSON. Rotate the secret in Secrets Manager, redeploy — the container picks up the new value without changing any task definition.
Frequently Asked Questions
ECS vs Kubernetes (EKS) — when should I use which?
ECS is simpler, tightly integrated with AWS services, and has lower operational overhead. Use ECS Fargate for most containerized workloads — it's the right choice for teams who don't want to manage Kubernetes. EKS (managed Kubernetes) is for teams already using Kubernetes, complex multi-cloud deployments, or workloads that need specific Kubernetes features (custom operators, Helm charts). ECS covers 80% of use cases with much less complexity.