Containers on AWS: Amazon ECR, ECS, Fargate and EKS Explained for Solutions Architects
Containers Recap
A container packages an app with its dependencies so it runs the same everywhere. AWS gives you a registry to store images, orchestrators to run them, and compute to host them.
text
Build image ─► push to ECR (registry) ─► ECS or EKS (orchestrator) ─► runs on Fargate (serverless) or EC2 (your instances)
│
behind an ALB, logs to CloudWatch, secrets from Secrets Manager
Amazon ECR
- Elastic Container Registry: private (and public) Docker/OCI image registry, integrated with IAM
- Image scanning (basic and enhanced with Inspector) finds vulnerabilities
- Lifecycle policies delete old images to save cost
- Replication across Regions and accounts
- Tag immutability prevents overwriting a tag
bash
aws ecr create-repository --repository-name shop-api --image-scanning-configuration scanOnPush=true --image-tag-mutability IMMUTABLE
aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-south-1.amazonaws.com
docker build -t shop-api:1.0.0 .
docker tag shop-api:1.0.0 123456789012.dkr.ecr.ap-south-1.amazonaws.com/shop-api:1.0.0
docker push 123456789012.dkr.ecr.ap-south-1.amazonaws.com/shop-api:1.0.0
Amazon ECS: Elastic Container Service
ECS is AWS's own container orchestrator. It is simple, deeply integrated with AWS and has no extra control plane fee.
| Concept | Meaning |
| Cluster | A logical group of tasks and services |
| Task definition | The blueprint (like a Compose file): images, CPU, memory, ports, environment, roles, logging. Versioned |
| Task | A running copy of a task definition (one or more containers) |
| Service | Keeps N tasks running, replaces failed ones, integrates with a load balancer, supports rolling and blue/green deployments, auto scaling |
| Capacity provider | Where tasks run: Fargate, Fargate Spot, or an EC2 Auto Scaling group |
json
{
"family": "shop-api",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "512", "memory": "1024",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::123456789012:role/shopApiTaskRole",
"runtimePlatform": {"cpuArchitecture": "ARM64", "operatingSystemFamily": "LINUX"},
"containerDefinitions": [{
"name": "api",
"image": "123456789012.dkr.ecr.ap-south-1.amazonaws.com/shop-api:1.0.0",
"portMappings": [{"containerPort": 8080}],
"essential": true,
"environment": [{"name": "STAGE", "value": "prod"}],
"secrets": [{"name": "DB_PASSWORD", "valueFrom": "arn:aws:secretsmanager:ap-south-1:123456789012:secret:prod/shop/db"}],
"logConfiguration": {"logDriver": "awslogs", "options": {"awslogs-group": "/ecs/shop-api", "awslogs-region": "ap-south-1", "awslogs-stream-prefix": "api"}},
"healthCheck": {"command": ["CMD-SHELL", "curl -f http://localhost:8080/health || exit 1"], "interval": 30, "retries": 3}
}]
}
| Role | Used by | Purpose |
| Task execution role | ECS agent / Fargate | Pull the image from ECR, write logs to CloudWatch, read secrets to inject |
| Task role | Your application code inside the container | Call S3, DynamoDB, SQS etc. (least privilege per service) |
bash
aws ecs create-cluster --cluster-name shop
aws ecs register-task-definition --cli-input-json file://taskdef.json
aws ecs create-service --cluster shop --service-name api --task-definition shop-api --desired-count 3 \
--launch-type FARGATE \
--network-configuration 'awsvpcConfiguration={subnets=[subnet-priv-a,subnet-priv-b],securityGroups=[sg-api],assignPublicIp=DISABLED}' \
--load-balancers targetGroupArn=$TG,containerName=api,containerPort=8080 \
--deployment-configuration 'deploymentCircuitBreaker={enable=true,rollback=true},minimumHealthyPercent=100,maximumPercent=200'
awsvpc mode gives each task its own ENI and security group. Required for Fargate
- The deployment circuit breaker rolls back a bad release automatically
- Scale services with Application Auto Scaling (target tracking on CPU, memory or ALB requests per target)
Fargate vs EC2 Launch Type
| Fargate (serverless) | EC2 (you manage nodes) |
| Servers to manage | None | You patch and scale the instances |
| Pricing | Per vCPU and GB per second the task runs | Pay for the instances (cheaper at high steady utilization) |
| Control | Limited (no daemon sets, no GPU on standard Fargate in many cases) | Full: GPU, custom AMI, privileged containers, any instance type |
| Good for | Most web apps, APIs, batch, small teams, least operational overhead | Large steady fleets, special hardware, tight cost optimization |
| Cost saving | Fargate Spot and Savings Plans (Compute SP) | Spot, Reserved, Savings Plans |
Amazon EKS: Elastic Kubernetes Service
EKS runs managed Kubernetes. AWS operates the control plane (multi-AZ API servers and etcd). You run workloads on managed node groups (EC2), Fargate profiles, or let EKS Auto Mode manage nodes.
bash
eksctl create cluster --name shop --region ap-south-1 --nodegroup-name ng1 \
--node-type m7g.large --nodes 3 --nodes-min 2 --nodes-max 6 --managed
aws eks update-kubeconfig --name shop --region ap-south-1
kubectl get nodes
| EKS topic | What to know |
| Control plane cost | A per-cluster hourly fee, plus compute for nodes |
| Versions | Kubernetes versions are supported for a limited time. Plan upgrades |
| Pod permissions | IAM Roles for Service Accounts (IRSA) or EKS Pod Identity give pods temporary AWS permissions without node-wide access |
| Networking | The VPC CNI gives pods VPC IP addresses. Plan subnet sizes (pods use many IPs) |
| Ingress | AWS Load Balancer Controller creates ALBs and NLBs from Ingress and Service objects |
| Scaling | Horizontal Pod Autoscaler, Karpenter or Cluster Autoscaler for nodes |
| Add-ons | CoreDNS, kube-proxy, VPC CNI, EBS CSI driver, managed through EKS |
Other Container Options
| Service | Use |
| AWS App Runner | Simplest: give a container image or source repo, get an auto-scaled HTTPS service. Good for small web apps and APIs |
| AWS Copilot CLI | Command-line helper to deploy on ECS |
| Elastic Beanstalk (Docker) | PaaS style deployment including containers |
| ECS Anywhere / EKS Anywhere | Run on your own hardware with AWS tooling |
| AWS Batch | Run batch jobs on Fargate or EC2 and Spot |
Which One Should I Choose?
| Situation | Choice |
| Team wants the simplest way to run containers with minimal operations | ECS on Fargate (or App Runner for very simple apps) |
| Team already uses Kubernetes, needs portability or the Kubernetes ecosystem | EKS |
| Need GPUs, custom kernels or large steady fleets and cost control | ECS or EKS on EC2 |
| Short event-driven functions | Lambda |
| Cost-sensitive fault-tolerant batch | Fargate Spot or EC2 Spot with AWS Batch |
| Lift-and-shift of a VM-based app | EC2 |
Practice Tasks
- Push a small image to an ECR repository with scan on push
- Write a task definition and run it as a Fargate service with 2 tasks behind an ALB
- Kill one task and watch the service replace it
- Introduce a failing image tag and see the circuit breaker roll back
- Compare the monthly cost of 3 Fargate tasks (0.5 vCPU, 1 GB) with one
t4g.medium instance using the AWS pricing calculator
Conclusion
ECR stores images, ECS runs them simply with tasks and services, EKS runs managed Kubernetes, and Fargate removes server management. Use task roles for app permissions, execution roles for pulling images and logs, awsvpc networking with ALBs, and choose Fargate for least operations, EC2 for special needs, EKS for the Kubernetes ecosystem.
Quick Summary: ECR scan/lifecycle; ECS cluster/task def/service/capacity provider; task vs execution role; Fargate vs EC2; EKS (IRSA, VPC CNI, LB controller, Karpenter); App Runner; choose table.