Volume 4 · Chapter 11 — Compute and Containers
ECS and ECR in Depth
11.1 Introduction
The orchestration chapter compared ECS, EKS, and Fargate at a high level. This chapter goes into ECS’s building blocks - task definitions, tasks, services, launch types, the two IAM roles, capacity providers, and service auto scaling - plus ECR, because ECS specifics (especially task role vs execution role) are common exam questions.
11.2 Task Definitions, Tasks, and Services
Atask definitionis the blueprint for running containers - it specifies the container image(s), CPU/memory, ports, environment variables, volumes, logging, and the IAM roles. Ataskis a running instantiation of a task definition (one or more containers running together). Aservicemaintains a desired number of tasks, replaces failed ones, integrates with a load balancer (registering tasks in a target group), and supports rolling deployments - the ECS equivalent of keeping a service healthy and scaled. So: task definition (blueprint) → task (running instance) → service (maintains desired count + LB + deployments). This mirrors the Kubernetes Deployment/Pod model in ECS terms.
flowchart TB
TD[Task Definition - image, CPU/mem, roles, ports] --> Task[Task - running containers]
Svc[ECS Service - desired count, LB, rolling deploy] --> Task
Svc --> ALB[Load balancer target group]
Launch{Launch type} -->|Fargate| FG[No servers to manage]
Launch -->|EC2| EC2[You manage the cluster instances] 11.3 Launch Types and Capacity Providers
ECS runs tasks two ways. TheFargate launch typeis serverless - AWS provisions the compute per task from CPU/memory you specify, with no cluster to manage (the default for most workloads). TheEC2 launch typeruns tasks on a cluster of EC2 instances you manage (for host control, specific instance types, GPU, or cost at scale).Capacity providersabstract this - they let a service draw capacity from Fargate, Fargate Spot (cheaper, interruptible), or an EC2 Auto Scaling group, and can mix them for cost optimization (e.g. base On-Demand + Fargate Spot).ECS Anywhereruns ECS tasks on your own on-premises hardware. The exam pattern: Fargate to avoid managing servers, EC2 launch type for host-level control, Fargate Spot / capacity providers for cost optimization.
11.4 Task Role vs Execution Role (Key Exam Point)
ECS uses two distinct IAM roles that are frequently confused. Thetask rolegrants permissions toyour application coderunning in the container - what the app can do in AWS (read a secret, write to S3, call DynamoDB); this is the per-service least-privilege identity. Thetask execution rolegrants permissions to theECS agent/Fargatetorunthe task - pulling the image from ECR, fetching secrets for injection, and writing logs to CloudWatch. The exam trap: if the container app can’t access an AWS service, fix thetask role; if the task can’tstart(can’t pull the image or write logs), fix theexecution role. Getting this distinction right is a reliable ECS exam point.
11.5 Service Auto Scaling and ECR
ECSservice auto scalingadjusts the desired task count using Application Auto Scaling with target-tracking, step, or scheduled policies (e.g. keep average CPU or request-count-per-target at a target), scaling containers like an ASG scales instances.**Elastic Container Registry (ECR)**is the managed private Docker registry - it stores images, integrates with IAM for access control, scans images for vulnerabilities (basic or enhanced via Inspector), supports lifecycle policies to expire old images, and cross-Region/cross-account replication. Deployments pull the specific versioned image from ECR. The exam expects you to know ECS service auto scaling scales tasks, and ECR is the secure, scanned, private image store feeding ECS/EKS.
11.6 Banking Production Scenario
A bank runs its microservices on ECS with Fargate to avoid managing servers, using capacity providers to blend Fargate and Fargate Spot for cost on non-critical services. Each service has a task definition specifying a least-privilegetask role(the payments service can read only its own secret and queue) and a sharedexecution roleto pull images from ECR and write logs. Services register with an ALB target group and use service auto scaling on request-count-per-target to scale tasks with load. Images are built in CI, scanned by ECR (enhanced scanning via Inspector), tagged by version, and expired by lifecycle policy; ECR replicates them to the DR Region. When a container can’t reach a secret the team checks the task role; when a task fails to start the team checks the execution role. This precise ECS/ECR design is professional-level container competence.
11.7 Chapter Revision Notes
- Task definition (blueprint: image, CPU/mem, roles) → task (running containers) → service (desired count, LB integration, rolling deploys).
- Launch types: Fargate (serverless, default) vs EC2 (managed hosts); capacity providers draw from Fargate/Fargate Spot/EC2 ASG for cost; ECS Anywhere runs on-prem.
- Task role = permissions for your app code; execution role = permissions for ECS/Fargate to pull the image, fetch secrets, and write logs. App can’t access AWS → task role; task won’t start → execution role.
- ECS service auto scaling scales the task count (target/step/scheduled).
- ECR = managed private registry: IAM access, vulnerability scanning (basic/Inspector), lifecycle policies, cross-Region/account replication.