Engineering Notes
Home/DevOps & Cloud/AWS/ECS, Fargate and ECR
DevOps & Cloud · AWS

ECS, Fargate and ECR

Run containers without managing servers: ECR stores images, task definitions describe containers, ECS services keep the desired number running behind a load balancer, and Fargate provides the compute. Deploy a new version and watch the circuit breaker save you.

☁️ DevOps & Cloud● Intermediate⏱ 5 min read▶ 2 interactive🛠️ 2 exercises🎤 5 interview Q&As

🎯 What you'll learn

  • Clusters, task definitions, tasks and services
  • Task role vs execution role, secrets and logging
  • Rolling deployments, the circuit breaker and autoscaling
  • ECR practices and ECS vs EKS vs Lambda

1The Pieces

  • ECR (Elastic Container Registry) — private Docker/OCI image registry: repositories, image scanning, lifecycle policies, cross-account/region replication, immutable tags.
  • ECS (Elastic Container Service) — AWS's container orchestrator: runs and keeps containers running, integrates with ALB, IAM, CloudWatch, Secrets Manager, Service Connect.
  • Fargate — serverless compute for containers: you specify vCPU/memory per task; AWS runs it on infrastructure you never see. The alternative is the EC2 launch type (you manage an Auto Scaling group of container instances).
text
ECR repo ──image──► task definition (blueprint) ──► ECS service (keeps N tasks running)
                                                     └─ tasks on Fargate in private subnets ─► ALB target group

2Task Definitions

A task definition is a versioned JSON blueprint (family + revision):

FieldMeaning
cpu / memorytask size (Fargate combinations, e.g. 0.25 vCPU/0.5 GB … 16 vCPU/120 GB)
containerDefinitions[]image, port mappings, environment, secrets (valueFrom Secrets Manager/SSM), log configuration, health check, essential
networkMode: awsvpceach task gets its own ENI and private IP (required for Fargate) — security groups apply per task
executionRoleArnused by the ECS agent to pull the image, push logs, fetch secrets
taskRoleArnassumed by your application code (S3, DynamoDB, SQS access)
runtimePlatformARM64 (Graviton, cheaper) or X86_64

Sidecars (log routers like FireLens/Fluent Bit, OpenTelemetry collectors, Envoy) are additional containers in the same task, sharing its network namespace (localhost).

3Services

An ECS service maintains desiredCount tasks of a task definition, replaces unhealthy ones, registers them with load balancer target groups, and performs deployments:

  • Rolling update (default): minimumHealthyPercent / maximumPercent control how many tasks are replaced at once; the deployment circuit breaker stops and rolls back a deployment whose tasks keep failing.
  • Blue/green with CodeDeploy: a second target group, traffic shifting (canary/linear), automatic rollback on alarms (devops-cloud/cicd/02-deployment_strategies).
  • Service auto scaling (Application Auto Scaling): target tracking on CPU, memory, or ALBRequestCountPerTarget; step scaling; scheduled (11-scalability_and_monitoring/).
  • Service Connect / Cloud Map for service-to-service discovery by name (system-design/system-design/08-service_discovery).
  • Capacity providers: Fargate, Fargate Spot (up to ~70% cheaper, can be interrupted with a 2-minute warning — good for workers), or EC2 Auto Scaling groups.

4ECR in Practice

bash
aws ecr create-repository --repository-name petpal/api \
  --image-tag-mutability IMMUTABLE --image-scanning-configuration scanOnPush=true
aws ecr get-login-password | docker login --username AWS --password-stdin 111111111111.dkr.ecr.eu-west-1.amazonaws.com
docker build -t 111111111111.dkr.ecr.eu-west-1.amazonaws.com/petpal/api:${GIT_SHA} .
docker push 111111111111.dkr.ecr.eu-west-1.amazonaws.com/petpal/api:${GIT_SHA}
  • Immutable tags + tagging by commit SHA: a tag always means the same image; deploy by tag or digest, never latest.
  • Lifecycle policies: keep the last N images per prefix, expire untagged images after a few days.
  • Scanning: basic (on push) or enhanced (Amazon Inspector, continuous) — devops-cloud/docker/07-container_security.
  • Private subnets without NAT need VPC endpoints for ecr.api, ecr.dkr, S3 (layers), CloudWatch Logs and Secrets Manager (10-internet_gateway_and_nat_gateway/).

5ECS vs EKS vs Lambda

ECS on FargateEKS (16-eks/)Lambda (12-…)
unittask (containers)podfunction
control plane costfree~$73/month per clusternone
learning curvelow, AWS-nativehigh (Kubernetes)lowest
portabilityAWS-onlyportable Kubernetes APIsAWS-only
long-running / steady load✅✅❌ (15 min max, per-request pricing)
ecosystemAWS integrationshuge (Helm, operators, service meshes)serverless ecosystem

ECS on Fargate is the pragmatic default for AWS-only teams running containers; EKS when you need Kubernetes portability or its ecosystem.

Under the HoodHow a Fargate Task Starts▾

When a service needs a new task, the ECS scheduler picks a subnet (spreading across AZs) and asks Fargate for capacity matching the requested CPU/memory and architecture. Fargate places the task on a host it manages and starts a dedicated microVM (Firecracker-based isolation, one task per VM boundary), creates and attaches an ENI in your subnet with your security groups, then the agent — using the execution role — authenticates to ECR, pulls image layers (from the regional cache, through S3), fetches secrets, configures the log driver, and starts containers in dependency order. Once containers pass health checks, ECS registers the task's IP in the target group; the ALB starts sending traffic after the target passes the load balancer's health checks. Large images and cold layer caches dominate start time — keep images small (multi-stage builds) and consider SOCI lazy loading for very large images.

An image pushed to ECR with an immutable SHA tag is referenced by a task definition with execution and task roles; the ECS service keeps tasks running on Fargate in private subnets with their own ENIs, registers them with the ALB, rolls deployments with a circuit breaker and scales with application auto scaling
An image pushed to ECR with an immutable SHA tag is referenced by a task definition with execution and task roles; the ECS service keeps tasks running on Fargate in private subnets with their own ENIs, registers them with the ALB, rolls deployments with a circuit breaker and scales with application auto scaling

⚠️ Common Pitfalls

  1. One role for everything — merge execution and task roles and over-grant.
  2. latest tags / mutable tags — you can't tell what's running or roll back reliably.
  3. Secrets as plain environment variables in the task definition.
  4. Tasks in public subnets with public IPs "to reach ECR" instead of NAT/endpoints.
  5. No circuit breaker — a broken image loops forever.
  6. No ECR lifecycle policy — storage costs grow unbounded.

📋 Quick Reference

text
ECR: immutable tags (git SHA) · scan on push · lifecycle policy · VPC endpoints for private pulls
task def: cpu/memory · awsvpc · execution role (pull/logs/secrets) vs task role (app) · secrets valueFrom · logs
service: desired count · rolling + circuit breaker or CodeDeploy blue/green · autoscaling · Service Connect
capacity: Fargate / Fargate Spot / EC2 · ECS default for AWS-only containers, EKS for Kubernetes

🧠 Key takeaways

  • Task definitions describe containers (image, CPU/memory, ports, roles); services keep N tasks running behind an ALB.
  • Task role = what the app can call; execution role = pulling images, logs, injecting secrets.
  • Use rolling deploys with the deployment circuit breaker (auto rollback) or blue/green; autoscale on CPU or requests per target.
  • Use immutable, SHA-tagged images in ECR with scanning; pick ECS for simplicity, EKS for the Kubernetes ecosystem.

🎤 Interview Q&A

Questions interviewers ask about this topic. Answer out loud first, then open a card to compare.

Q1What are ECS task definitions, tasks and services?core▾

A task definition describes containers (image, CPU/memory, ports, env, IAM role, logging). A task is a running instance of it. A service keeps a desired number of tasks running, integrates with load balancers and handles deployments.

Follow-ups interviewers ask
  • Task role vs execution role? Task role: permissions for your app; execution role: for ECS to pull images and write logs.
Q2Fargate vs EC2 launch type?core▾

Fargate: serverless containers, no instances to manage, per-task billing, less control. EC2: you manage the cluster instances, cheaper at high utilization, supports GPUs and custom AMIs, and Spot capacity.

Follow-ups interviewers ask
  • When is EC2 cheaper? High, steady utilization with bin-packing.
Q3How do ECS deployments work?core▾

Rolling updates replace tasks gradually based on minimum/maximum healthy percentages, with the deployment circuit breaker rolling back failed deployments; blue/green via CodeDeploy shifts traffic between target groups.

Follow-ups interviewers ask
  • What triggers circuit breaker rollback? Tasks repeatedly failing to reach a steady healthy state.
Q4How do you secure container images in ECR?senior▾

Scan images (basic or Inspector enhanced scanning), immutable tags, lifecycle policies to remove old images, least-privilege repository policies, and minimal base images.

Follow-ups interviewers ask
  • Why immutable tags? So v1.2.3 always means the same image.
Q5ECS vs EKS?core▾

ECS is simpler, AWS-native and cheaper to operate; EKS gives Kubernetes compatibility, a vast ecosystem and portability at higher complexity. Choose EKS if you need Kubernetes features or already use it.

Follow-ups interviewers ask
  • Cost difference? EKS charges per cluster plus more operational effort.

🛠️ Practice exercises

Each exercise is a file in this topic folder. Code exercises are self-checking: run them and make the checks pass.

✅ Check yourself

Answer the questions — feedback is instant.

1. Which role lets the application call S3?
The execution role is for ECS itself.
2. New tasks keep crashing during a deploy. What rolls back automatically?
Enable it with rollback.
3. Why tag images with the git SHA rather than 'latest'?
You know exactly what runs.
4. Fargate means…
Serverless containers.