Operating ECS, EKS and ECR
Overview
Containers package an application with its dependencies so it runs the same everywhere. Amazon ECR stores the images, Amazon ECS and Amazon EKS schedule containers onto compute (EC2 instances or AWS Fargate), and the SysOps administrator keeps all of it healthy, observable, secure and cost-efficient. The exam does not expect you to write Dockerfiles, but it does expect you to diagnose why a task will not start, scale a service, and collect logs and metrics.
Think of ECR as a warehouse of sealed shipping containers, ECS or EKS as the port crane operators deciding which ship (host) carries which container, and Fargate as hiring a shipping company so you never own a ship. Your job is the port control tower: watching dashboards, reading incident logs and adding cranes when traffic grows.
Most operational questions fall into four buckets: why did the task stop, why can it not pull the image or reach a secret, how do I scale it, and where are the logs and metrics.
Key Concepts
- ECS has clusters, task definitions (the blueprint), tasks (running instances) and services (keep N tasks running and register them with a load balancer).
- Launch types: Fargate (serverless, you manage no hosts) or EC2 (you manage instances, patching and capacity providers).
- Two IAM roles: the task execution role (ECS agent pulls the image from ECR, writes logs, reads secrets) and the task role (permissions used by your application code).
- ECS Service Auto Scaling uses Application Auto Scaling with target tracking on CPU, memory or ALB request count per target; capacity providers scale the EC2 hosts.
- EKS runs a managed Kubernetes control plane; data plane options are managed node groups, self-managed nodes and Fargate profiles. Scale pods with HPA and nodes with Cluster Autoscaler or Karpenter.
- ECR supports image scanning (basic and enhanced via Inspector), lifecycle policies to expire old images, cross-Region and cross-account replication, and tag immutability.
- Container Insights and the CloudWatch agent / Fluent Bit collect cluster, service and task metrics plus container logs (see the observability chapter).
- Common start failures: CannotPullContainerError, ResourceInitializationError, essential container exited, out-of-memory (exit 137), failing ALB health checks.
Detailed Notes
Building blocks and responsibilities
| Component | Purpose | Operator concern |
|---|---|---|
| ECR repository | Stores and versions images | Scanning, lifecycle, replication, pull permissions |
| ECS task definition | CPU, memory, image, env vars, log driver, roles | Revisions are immutable; update the service to use a new revision |
| ECS service | Maintains desired count, integrates with ALB | Deployment type, health check grace period, circuit breaker |
| EKS cluster | Managed Kubernetes API | Version upgrades, add-ons, node group AMIs |
| Fargate | Serverless compute for tasks and pods | Size by task CPU/memory, no host patching |
Why a task will not start
| Symptom (stopped reason) | Likely cause | Fix |
|---|---|---|
CannotPullContainerError | No route to ECR (private subnet without NAT or endpoints), wrong image tag, missing execution role permission | Add NAT or ecr.api, ecr.dkr and S3 gateway endpoints, fix tag, attach AmazonECSTaskExecutionRolePolicy |
ResourceInitializationError | Cannot fetch secrets or env files, or ENI creation fails | Allow Secrets Manager / SSM endpoint, check subnet IP space |
| Exit code 137 | Container killed for exceeding memory | Raise task or container memory limit |
| Essential container exited | Application crash | Read logs in CloudWatch Logs (awslogs driver) |
| Tasks keep cycling | ALB health check fails or grace period too short | Fix health check path, increase healthCheckGracePeriodSeconds |
aws ecs describe-services --cluster prod --services web --query "services[0].events[:5]"
aws ecs list-tasks --cluster prod --service-name web --desired-status STOPPED
aws ecs describe-tasks --cluster prod --tasks <task-arn> --query "tasks[0].[stoppedReason,containers[].reason]"
Scaling ECS services
aws application-autoscaling register-scalable-target --service-namespace ecs \
--resource-id service/prod/web --scalable-dimension ecs:service:DesiredCount \
--min-capacity 2 --max-capacity 20
aws application-autoscaling put-scaling-policy --service-namespace ecs \
--resource-id service/prod/web --scalable-dimension ecs:service:DesiredCount \
--policy-name cpu-target --policy-type TargetTrackingScaling \
--target-tracking-scaling-policy-configuration '{"TargetValue":60.0,"PredefinedMetricSpecification":{"PredefinedMetricType":"ECSServiceAverageCPUUtilization"}}'
With the EC2 launch type you also need a capacity provider backed by an Auto Scaling group with managed scaling, otherwise tasks stay in PROVISIONING because there are no free hosts. Fargate removes that layer.
Observability for containers
- Container Insights (enabled per cluster) publishes CPU, memory, network and storage metrics at cluster, service and task level into the
ECS/ContainerInsightsorContainerInsightsnamespaces, plus performance log events. - Use the awslogs log driver (or FireLens with Fluent Bit) so container stdout reaches CloudWatch Logs.
- On EKS install the Amazon CloudWatch Observability add-on, which deploys the CloudWatch agent and Fluent Bit as DaemonSets.
- ECS Exec (
aws ecs execute-command) opens a shell in a running container through Systems Manager; it needs the task role to allowssmmessages:*.
ECR operations
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 111122223333.dkr.ecr.us-east-1.amazonaws.com
aws ecr put-image-scanning-configuration --repository-name web --image-scanning-configuration scanOnPush=true
aws ecr put-lifecycle-policy --repository-name web --lifecycle-policy-text file://policy.json
{ "rules": [ { "rulePriority": 1, "description": "Keep last 10 images",
"selection": { "tagStatus": "any", "countType": "imageCountMoreThan", "countNumber": 10 },
"action": { "type": "expire" } } ] }
EKS essentials for operators
| Task | Tool |
|---|---|
| Connect to the cluster | aws eks update-kubeconfig --name prod then kubectl get pods -A |
| Grant IAM users access | EKS access entries (or the legacy aws-auth ConfigMap) |
| Give a pod AWS permissions | EKS Pod Identity or IAM Roles for Service Accounts (IRSA) |
| Upgrade | Control plane first, then add-ons, then node groups; one minor version at a time |
| Pods Pending | Not enough node capacity or IP addresses in the subnet; check events with kubectl describe pod |
Real World Example
Scenario – deployment stuck in a loop. A retailer deploys a new revision of its checkout service on Fargate in private subnets. The service shows tasks repeatedly moving from PROVISIONING to STOPPED with CannotPullContainerError: ... i/o timeout.
The operator realizes the subnets have no NAT gateway and no VPC endpoints. They create interface endpoints for ecr.api and ecr.dkr, a gateway endpoint for S3 (image layers live in S3), and an endpoint for CloudWatch Logs. Tasks start immediately. They also enable the deployment circuit breaker with rollback so future bad revisions roll back automatically, and add a lifecycle policy to keep only the latest 20 images.
Architecture Diagram
- 1Build an image and push it to ECR; scan on push and keep a lifecycle policy.
- 2The ECS task execution role (or EKS node/Pod Identity) pulls the image through NAT or VPC endpoints.
- 3The service registers tasks with the ALB, which runs health checks.
- 4Application Auto Scaling or HPA adjusts task or pod count from CloudWatch metrics.
- 5Container Insights and container logs flow to CloudWatch for alarms and troubleshooting.
Best Practices
- ✓Give tasks private-subnet access to ECR, S3 and CloudWatch Logs through VPC endpoints to avoid NAT cost and pull failures.
- ✓Enable the deployment circuit breaker with rollback and set a realistic health check grace period.
- ✓Separate the task execution role from the task role and keep both least privilege; use Pod Identity or IRSA on EKS.
- ✓Turn on scan on push, tag immutability and lifecycle policies in every ECR repository.
- ✓Enable Container Insights and ship container logs centrally; alarm on service CPU, memory and running task count.
Common Interview Questions
Exam Tips
SOA-C03 focus
- Private-subnet pull failures -> NAT or ECR/S3 endpoints; permission failures -> task execution role.
- App needs S3 access inside a container -> task role (ECS) or IRSA / Pod Identity (EKS), never access keys in the image.
- Exit code 137 means out-of-memory; raise the memory limit.
- Operations-heavy scenarios that want no host management -> Fargate; Container Insights is the built-in monitoring answer.
Finished this lesson?
0 of 46 lessons done · 0% of the course

