Containers: ECS, EKS, Fargate, ECR
A container packages an application with its libraries into an image that runs the same way everywhere. AWS has several services that work together to run containers:
| Service | Role |
|---|---|
| Amazon ECR (Elastic Container Registry) | Stores your container images (a private Docker registry) |
| Amazon ECS (Elastic Container Service) | AWS's own container orchestrator: simple, deeply integrated |
| Amazon EKS (Elastic Kubernetes Service) | Managed Kubernetes control plane: portable, huge ecosystem |
| AWS Fargate | Serverless compute for ECS and EKS: no EC2 instances to manage |
| AWS App Runner | Simplest option: give it an image or repo, get an HTTPS URL |
The orchestrator (ECS or EKS) decides what runs where; the launch type decides what it runs on: EC2 instances you manage, or Fargate.
Pushing an image to ECR
aws ecr create-repository --repository-name shop-api
aws ecr get-login-password --region eu-west-1 | \
docker login --username AWS --password-stdin 123456789012.dkr.ecr.eu-west-1.amazonaws.com
docker build -t shop-api:1.4.0 .
docker tag shop-api:1.4.0 123456789012.dkr.ecr.eu-west-1.amazonaws.com/shop-api:1.4.0
docker push 123456789012.dkr.ecr.eu-west-1.amazonaws.com/shop-api:1.4.0Sample output (end of the push):
1.4.0: digest: sha256:4f2a9c1e0b7d8a6f3e5c2b1a0d9e8f7c6b5a4d3e2f1a0b9c8d7e6f5a4b3c2d1e size: 1786ECS concepts
- Task definition: the blueprint (JSON) of one or more containers: image, CPU, memory, ports, environment, IAM task role.
- Task: one running copy of a task definition.
- Service: keeps N tasks running, replaces failed ones, registers them in a load balancer target group.
- Cluster: a logical group of services and the capacity they run on.
{
"family": "shop-api",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "512",
"memory": "1024",
"containerDefinitions": [{
"name": "api",
"image": "123456789012.dkr.ecr.eu-west-1.amazonaws.com/shop-api:1.4.0",
"portMappings": [{ "containerPort": 8080 }],
"environment": [{ "name": "APP_ENV", "value": "production" }]
}]
}Model: placing tasks on EC2 capacity
With the EC2 launch type, ECS must find an instance with enough free CPU and memory for each task. A binpack strategy fills instances tightly (fewer instances, lower cost); spread distributes tasks (better availability). CPU is measured in CPU units (1024 = one vCPU).
instances = {"i-a": (2048, 4096), "i-b": (2048, 4096), "i-c": (2048, 4096)} # free (cpu, MiB)
tasks = [("api", 512, 1024)] * 4 + [("worker", 1024, 2048)] * 2
def place(strategy):
free = {i: list(cap) for i, cap in instances.items()}
placement = {i: [] for i in instances}
for name, cpu, mem in tasks:
fits = [i for i in free if free[i][0] >= cpu and free[i][1] >= mem]
if not fits:
print(f" {name}: no capacity (task stays PENDING)")
continue
if strategy == "binpack": # least free memory first
target = min(fits, key=lambda i: (free[i][1], i))
else: # spread: the instance with the fewest tasks
target = min(fits, key=lambda i: (len(placement[i]), i))
free[target][0] -= cpu
free[target][1] -= mem
placement[target].append(name)
return placement
for strategy in ("binpack", "spread"):
print(strategy)
for inst, names in place(strategy).items():
print(f" {inst}: {names}")binpack
i-a: ['api', 'api', 'api', 'api']
i-b: ['worker', 'worker']
i-c: []
spread
i-a: ['api', 'api']
i-b: ['api', 'worker']
i-c: ['api', 'worker']
Binpack fills i-a and i-b completely and leaves i-c empty, so it could be stopped. Spread puts tasks on every instance, so losing one instance hurts less. ECS can also combine strategies, for example spread across AZs and then binpack inside each AZ.
Model: Fargate or EC2?
With Fargate you pay for the vCPU and memory each task requests, per second. With EC2 you pay for whole instances, whether full or not. Using example prices:
FARGATE_VCPU_HOUR = 0.04048 # example
FARGATE_GB_HOUR = 0.004445 # example
EC2_INSTANCE_HOUR = 0.0832 # example price for a 2 vCPU / 8 GB instance
task_vcpu, task_gb = 0.5, 1.0
fargate_task_hour = task_vcpu * FARGATE_VCPU_HOUR + task_gb * FARGATE_GB_HOUR
for tasks in (1, 4, 12):
fargate = tasks * fargate_task_hour * 730
instances_needed = -(-tasks // 4) # 4 tasks fit on one instance (ceil division)
ec2 = instances_needed * EC2_INSTANCE_HOUR * 730
print(f"{tasks:>2} tasks: Fargate ${fargate:7.2f}/month EC2 ({instances_needed} inst.) ${ec2:7.2f}/month") 1 tasks: Fargate $ 18.02/month EC2 (1 inst.) $ 60.74/month
4 tasks: Fargate $ 72.08/month EC2 (1 inst.) $ 60.74/month
12 tasks: Fargate $ 216.24/month EC2 (3 inst.) $ 182.21/month
Fargate is cheaper for a few small tasks and needs no patching or capacity planning. Full, steady EC2 capacity can be cheaper at scale, especially with Savings Plans or Spot. Many teams start on Fargate and only move to EC2 capacity when the bill justifies the extra work.
ECS or EKS?
Choose ECS if you just want to run containers on AWS with the least operational effort. Choose EKS if you already use Kubernetes, need its ecosystem (Helm charts, operators) or want portability across clouds. Kubernetes itself is a big topic of its own; on AWS, EKS runs its control plane for you.
Test yourself
Which service stores container images?
- Amazon ECS
- Amazon ECR
- AWS Fargate
- Amazon EKS
What does AWS Fargate remove from your responsibilities?
- Writing Dockerfiles
- Managing the EC2 instances that containers run on
- Choosing CPU and memory per task
- Building images
In ECS, what keeps a desired number of tasks running and replaces failed ones?
- A task definition
- A service
- A cluster
- A repository