Containers on AWS
Running containers on AWS — ECR for images, ECS concepts (clusters, task definitions, tasks, services), EC2 vs Fargate capacity, EKS for Kubernetes, App Runner, service discovery and Service Connect, networking with awsvpc, task roles, deployments, autoscaling, and choosing between ECS, EKS, Fargate and Lambda.
Containers package an application with its dependencies into an image that runs the same everywhere (Docker). AWS offers several ways to run them, differing mainly in who operates the orchestrator and who operates the servers.
The options
| ECS | EKS | |
|---|---|---|
| Orchestrator | AWS-native, simple API | Upstream Kubernetes (Kubernetes) |
| Learning curve | Low | High |
| Ecosystem | AWS integrations | Helm, operators, CNCF tooling, multi-cloud portability |
| Control-plane cost | Free | Per-cluster hourly fee |
| Choose when | You're all-in on AWS and want simplicity | You already know Kubernetes, need its ecosystem or portability |
ECR: the image registry
Elastic Container Registry stores images privately per account and region:
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 --platform linux/arm64 -t orders:1.4.2 .
docker tag orders:1.4.2 123456789012.dkr.ecr.ap-south-1.amazonaws.com/orders:1.4.2
docker push 123456789012.dkr.ecr.ap-south-1.amazonaws.com/orders:1.4.2Enable scan on push for vulnerabilities, immutable tags so 1.4.2 can't be overwritten, and lifecycle policies to expire old images. Build arm64 images to run on cheaper Graviton capacity.
ECS concepts
- Task definition: a versioned blueprint — container images, CPU and memory, ports, environment variables and secrets, log configuration, and the IAM roles. Like a Kubernetes pod spec.
- Task: a running instance of a task definition (one or more containers sharing networking).
- Service: keeps N tasks running, replaces failed ones, registers them with a load balancer target group, and performs rolling or blue/green deployments.
- Cluster: a logical grouping of services and the capacity they run on.
{
"family": "orders",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "512", "memory": "1024",
"runtimePlatform": { "cpuArchitecture": "ARM64", "operatingSystemFamily": "LINUX" },
"taskRoleArn": "arn:aws:iam::123456789012:role/orders-task",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecs-task-execution",
"containerDefinitions": [{
"name": "app",
"image": "123456789012.dkr.ecr.ap-south-1.amazonaws.com/orders:1.4.2",
"portMappings": [{ "containerPort": 8080 }],
"secrets": [{ "name": "DB_PASSWORD", "valueFrom": "arn:aws:secretsmanager:ap-south-1:123456789012:secret:orders-db" }],
"logConfiguration": { "logDriver": "awslogs",
"options": { "awslogs-group": "/ecs/orders", "awslogs-region": "ap-south-1", "awslogs-stream-prefix": "app" } },
"healthCheck": { "command": ["CMD-SHELL", "curl -f http://localhost:8080/health || exit 1"] }
}]
}Two roles, often confused:
- Task execution role: used by the ECS agent to pull the image from ECR, fetch secrets and write logs.
- Task role: used by your application code to call AWS (DynamoDB, S3) (IAM).
Fargate vs EC2 capacity
- No instances: each task gets its own isolated micro-VM
- Pay per task vCPU and memory per second
- No patching, no bin-packing, no cluster scaling
- Fargate Spot for interruptible tasks
- Limits: no GPUs, no privileged containers, no host access
- You run an Auto Scaling group of container instances
- Cheaper at high, steady utilisation; Savings Plans and Spot
- GPUs, custom AMIs, daemons, large instance types
- You patch the AMI and keep the cluster sized (capacity providers automate scaling)
Start with Fargate; move to EC2 capacity when cost at scale or special hardware justifies the operational work.
Networking
With the awsvpc network mode (required on Fargate), every task gets its own elastic network interface and private IP in your subnets — so security groups apply per task, and the ALB target group uses IP targets. Put tasks in private subnets; give them access to ECR, S3 and CloudWatch through VPC endpoints or a NAT gateway (VPC).
Service-to-service communication:
- Service Connect: ECS-managed service discovery plus a proxy that adds retries, timeouts and per-service metrics — call
http://payments:8080. - Cloud Map DNS-based discovery.
- An internal ALB per service.
Deployments and scaling
- Rolling updates controlled by
minimumHealthyPercentandmaximumPercent, with the deployment circuit breaker to roll back automatically when new tasks fail to stabilise. - Blue/green with CodeDeploy (or ECS native blue/green): a new set of tasks behind a second target group, test traffic, then shift.
- Service Auto Scaling with target tracking on CPU, memory or ALB requests per target — the same model as EC2 Auto Scaling.
EKS in brief
EKS runs a highly available Kubernetes control plane for you; you provide nodes via managed node groups, Karpenter (fast, flexible node autoscaling), or Fargate profiles — or let EKS Auto Mode manage nodes too. AWS integrations come through add-ons: the VPC CNI (pods get VPC IPs), the AWS Load Balancer Controller (Ingress → ALB), EBS/EFS CSI drivers, and Pod Identity for IAM roles per service account. Upgrades are your responsibility: Kubernetes minor versions have a limited standard support window, so plan regular cluster upgrades.
Choosing
| Situation | Pick |
|---|---|
| Event-driven, short, spiky work | Lambda |
| A web service or API, small team, AWS-only | ECS on Fargate (or App Runner for the simplest case) |
| Steady high load, cost-sensitive | ECS or EKS on EC2 with Graviton and Savings Plans |
| Existing Kubernetes skills, Helm charts, multi-cloud | EKS |
| Batch jobs | AWS Batch (on ECS/EKS/Fargate) or Step Functions + Fargate tasks |
QWhat's the difference between the ECS task role and the task execution role?medium
The task execution role is used by the ECS agent and Fargate infrastructure on your behalf to start the task: pulling the image from ECR, retrieving secrets from Secrets Manager or Parameter Store for injection, and sending logs to CloudWatch. The task role is assumed by the application code inside the container to call AWS APIs, such as reading from S3 or writing to DynamoDB. Keeping them separate follows least privilege.
QECS or EKS — how do you decide?medium
ECS is simpler, deeply integrated with AWS, has no control-plane fee and is quick for a small team to adopt. EKS gives you standard Kubernetes with its ecosystem — Helm, operators, service meshes, GitOps tools — and portability across clouds, at the cost of more complexity and ongoing upgrade work. Choose EKS if the team already knows Kubernetes or needs its ecosystem; otherwise ECS, usually on Fargate, gets you to production faster.
QWhen is Fargate a worse choice than EC2-backed containers?medium
When you need GPUs, privileged containers, host-level daemons or custom kernels; when tasks need very large CPU or memory sizes; and when you run large, steady workloads where well-packed EC2 instances with Savings Plans or Spot are meaningfully cheaper than per-task Fargate pricing.
- Store images in ECR with scanning, immutable tags and lifecycle policies; build arm64 for Graviton.
- ECS: task definitions describe containers, services keep tasks running behind load balancers, clusters group capacity.
- Fargate removes servers entirely; EC2 capacity is cheaper at scale and supports GPUs and host access.
- The task execution role starts the task; the task role is what your code uses.
- awsvpc gives each task its own ENI and security group; use Service Connect for service discovery.
- Choose Lambda for spiky events, ECS on Fargate for most services, EKS when you need Kubernetes.