AWS · Containers & CI/CD

ECS

Amazon ECS (Elastic Container Service) runs your Docker containers. With Fargate, you do not manage any servers.

Intermediate 5 min Lesson 18 of 30
Before you start:DevOps: DockerECR
0/30 in AWS
On this page · 5 min · Intermediate

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#

JSONtaskdef.json Illustrative
{
  "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" } }
  }]
}
Expected output
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#

  1. Fargate needs awsvpc networking and task-level CPU/memory.
  2. The execution role pulls the image and injects the secret.
  3. The task role is what the app’s own AWS SDK calls use.
  4. 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#

Mixing up task role and execution role

Execution role = ECS’s permissions (pull, logs, secrets). Task role = your app’s permissions.

Using the latest tag

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
Shell Illustrative
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#

ECS quizMultiple choice Question 1 of 1

Which role does your application code use when calling S3 from an ECS task?

Score so far: 0/0

Want more? Practise all AWS MCQs · AWS interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.