ECS
Amazon ECS (Elastic Container Service) runs your Docker containers. With Fargate, you do not manage any servers.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 29, 2026
Related tutorials
Learning objectives#
- Write a task definition
- Run a service on Fargate
- Attach a load balancer
- Use task roles and secrets
Explanation#
A task definition describes containers: image, CPU, memory, ports, environment variables, secrets and logging.
A service keeps the desired number of tasks running, replaces failed ones and registers them with a load balancer target group.
Fargate runs tasks without EC2 instances to manage; the EC2 launch type uses your own instances for more control and cost tuning.
The task role grants the app AWS permissions; the task execution role lets ECS pull images from ECR and read secrets.
Code example: Fargate task definition for a Node.js API#
{
"family": "shop-api",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "512", "memory": "1024",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::123456789012:role/shop-api-task",
"containerDefinitions": [{
"name": "api",
"image": "123456789012.dkr.ecr.ap-south-1.amazonaws.com/shop-api:1.0.0",
"portMappings": [{ "containerPort": 3000 }],
"environment": [{ "name": "NODE_ENV", "value": "production" }],
"secrets": [{ "name": "DB_PASSWORD", "valueFrom": "arn:aws:secretsmanager:ap-south-1:123456789012:secret:prod/shop/db-AbCdEf:password::" }],
"logConfiguration": { "logDriver": "awslogs", "options": {
"awslogs-group": "/ecs/shop-api", "awslogs-region": "ap-south-1", "awslogs-stream-prefix": "api" } }
}]
}
Registered as shop-api:1. A service can now run N copies of this task behind an ALB, each logging to CloudWatch.
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Fargate needs awsvpc networking and task-level CPU/memory.
- The execution role pulls the image and injects the secret.
- The task role is what the app’s own AWS SDK calls use.
- Logs go to CloudWatch via the awslogs driver.
Real-world use case#
A containerised Node.js or ASP.NET Core API running 2–10 Fargate tasks behind an ALB with auto scaling on CPU.
Common mistakes#
Execution role = ECS’s permissions (pull, logs, secrets). Task role = your app’s permissions.
Deploy immutable version tags so rollbacks are reliable.
Best practices#
- Use Fargate unless you need EC2 control
- Inject secrets via the secrets field
- Run at least two tasks across AZs
- Enable deployment circuit breaker with rollback
Remember this#
Task definition = recipe. Service = keep N running.
Execution role = ECS. Task role = your app.
Fargate = containers without servers.
Practice exercise#
Your task: Create a service running 2 tasks of shop-api on Fargate in two private subnets.
Show solution
aws ecs create-service --cluster shop --service-name shop-api --task-definition shop-api:1 \
--desired-count 2 --launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[subnet-a,subnet-b],securityGroups=[sg-app],assignPublicIp=DISABLED}"
Mini quiz#
Which role does your application code use when calling S3 from an ECS task?
Want more? Practise all AWS MCQs · AWS interview questions
My notes#
Saved in this browser.