ECS, EKS, Fargate & ECR
Choose between ECS, EKS and Fargate for a containerised workload.
By Invicly Tech, , in AWS Solutions Architect – Associate. About 12 minutes to read.
In short
ECS is AWS's own container orchestrator and EKS runs managed Kubernetes. Both can run containers on EC2 instances you manage or on Fargate, which is serverless compute for containers. ECR is the private registry that stores your container images.
Explained simply
A container packs an app with everything it needs, such as libraries and runtime, into one image that runs the same on a laptop or in the cloud. Running one container is easy. Running hundreds across many servers, restarting them when they crash and rolling out new versions, needs an orchestrator.
AWS offers two orchestrators. Amazon ECS is AWS's own: simpler, with fewer moving parts. Amazon EKS runs standard Kubernetes, the popular open-source orchestrator, so your Kubernetes skills and tools work as usual.
Both need computers to run on. You can run containers on EC2 instances that you choose and look after, or on AWS Fargate, where AWS provides the computer for each task and you never see a server. Fargate costs a little more per unit, but removes patching and capacity planning.
Images have to live somewhere. Amazon ECR (Elastic Container Registry) is a private registry inside your AWS account. Your build pipeline pushes images to ECR, and ECS or EKS pulls them when starting containers, with IAM deciding who may push and pull.
Press Play on a diagram to walk through it one step at a time.
- 1Orchestrator
ECS (AWS-native) or EKS (Kubernetes API).
- 2Capacity
EC2 instances you manage or serverless Fargate.
- 3Workload unit
ECS task or Kubernetes Pod.
- 4Container image
Pulled from Amazon ECR.
- Simpler, AWS-native API
- No control plane fee
- Deep AWS integration
- Least operational overhead
- Standard Kubernetes
- Portable across clouds and on-prem
- Hourly control plane fee
- Rich open-source ecosystem
- 1Push image
CI builds and pushes web:1.5 to ECR.
- 2New revision
Register task definition web:8.
- 3Update service
Point the service at web:8.
- 4Start and check
New tasks must pass ALB health checks.
- 5Drain old tasks
Old tasks stop; rollback if new ones fail.
In depth
ECS building blocks
A cluster is a logical group for your workloads. A task definition is the blueprint: one or more containers, each with an image, CPU and memory, ports, environment variables, secrets and log settings, plus IAM roles and the network mode. Every change creates a new revision, such as web:7. A task is one running copy of a task definition.
A service keeps a desired number of tasks running, replaces failed ones, and registers them with a load balancer target group. Rolling deployments are controlled by minimum healthy percent and maximum percent; the deployment circuit breaker can roll back automatically if new tasks keep failing. ECS also supports blue/green deployments.
Capacity providers say where tasks run: FARGATE, FARGATE_SPOT, or an Auto Scaling group of EC2 instances. A capacity provider strategy can mix them, for example a base on Fargate with extra tasks on Fargate Spot. Standalone tasks, started by run-task or an EventBridge schedule, suit batch jobs.
- Service: web
- Desired count 3
- Behind an ALB target group
- Rolling deploy with rollback
- Service: worker
- Desired count 2
- Reads from an SQS queue
- Standalone tasks
- Batch jobs via run-task
- Scheduled by EventBridge
- Capacity providers
- FARGATE
- FARGATE_SPOT
- EC2 Auto Scaling group
Fargate or EC2 capacity
With Fargate, each task runs in its own isolated environment with its own kernel, so tasks never share a host with other customers' tasks or with each other. Fargate requires the awsvpc network mode. Task sizes range from 0.25 vCPU with 0.5 GB up to 16 vCPU with 120 GB, with 20 GiB of ephemeral storage by default, expandable to 200 GiB. You pay per vCPU and GB per second while tasks run. Fargate Spot runs interruptible tasks at a large discount, with a two-minute warning before AWS reclaims capacity.
With EC2 capacity, you choose the instance types, including GPU instances, which Fargate does not offer. You can run daemon tasks on every host and pack many small tasks onto large instances, which is cheaper at steady high scale. In exchange, you patch the AMI and scale the instances, though capacity provider managed scaling helps.
On the exam, "no servers to manage" means Fargate; "GPU" or "specific instance type" means EC2.
- No servers to patch or scale
- Per-task isolation
- Up to 16 vCPU and 120 GB per task
- No GPUs
- Fargate Spot for interruptible work
- You manage instances and AMIs
- Any instance type, incl. GPUs
- Daemon tasks and bin packing
- Cheaper at steady high use
- Spot instances via the ASG
Task role, execution role and networking
Two IAM roles confuse many people. The task execution role is used by ECS itself to start the task: pull the image from ECR, send logs to CloudWatch Logs, and fetch secrets from Secrets Manager or Parameter Store to inject as environment variables. The task role is used by your application code at runtime, for example to read from S3 or write to DynamoDB. Give each service its own task role with least privilege.
In awsvpc mode, every task gets its own elastic network interface, private IP and security group, just like an EC2 instance. That allows fine-grained rules such as "only the web service may reach the orders service". Load balancer target groups must use the ip target type for awsvpc tasks.
Tasks in private subnets need a path to ECR, CloudWatch Logs and other services: a NAT gateway, or VPC endpoints for ECR (api and dkr), S3 for image layers, and logs.
- ECS (execution role) to Amazon ECR: Pull image web:1.41Pull image web:1.4
- ECS (execution role) to Secrets Manager: Get DB password secret2Get DB password secret
- ECS (execution role) to App container (task role): Start container with secret env3Start container with secret env
- 4Call S3 and DynamoDB with task roleApp container (task role) to App container (task role): Call S3 and DynamoDB with task role
EKS in brief
EKS runs the Kubernetes control plane for you across three AZs, with automatic patching of control plane components, for an hourly fee per cluster. Each Kubernetes version has standard support and then a paid extended support period, so plan regular upgrades.
For worker capacity you can use managed node groups (EC2 instances in an ASG that EKS updates for you), self-managed nodes, Fargate profiles that run pods without nodes, Karpenter for fast, flexible node provisioning, or EKS Auto Mode, where AWS manages nodes, scaling and core add-ons.
Pods get IAM permissions through EKS Pod Identity or IAM roles for service accounts (IRSA). The Amazon VPC CNI gives each pod a real VPC IP address, which is simple but consumes IPs quickly, so size subnets generously.
Choose EKS when the team already uses Kubernetes, needs portability across clouds or on-premises, or relies on the Kubernetes ecosystem such as Helm charts and operators.
ECR features worth knowing
ECR repositories are private, Regional and controlled by IAM policies plus optional repository policies, which allow cross-account pulls. Image URIs look like account.dkr.ecr.region.amazonaws.com/name:tag.
Basic scanning checks images for known vulnerabilities on push. Enhanced scanning uses Amazon Inspector to scan continuously, including operating system and programming language packages, and rescans when new CVEs appear. Tag immutability stops anyone overwriting a tag like 1.4 with different content, which makes deployments repeatable.
Lifecycle policies expire old or untagged images automatically, keeping storage costs down. Replication copies images to other Regions or accounts for multi-Region deployments. Pull-through cache rules mirror public registries, such as ECR Public or Docker Hub, into your account, which avoids rate limits and keeps builds working if the upstream is down.
- Login: aws ecr get-login-password piped to docker login
- Enhanced scanning uses Amazon Inspector
- Tag immutability for repeatable deploys
- Lifecycle policies expire old images
- Replication and pull-through cache
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1Run a container on ECS with Fargate~45m
- 2Compare ECS and EKS~45m
- 3Choose EC2 or Fargate launch types~45m
- 4Push images to ECR~45m
Key points
- Fargate removes server management; pay per vCPU and memory per second.
- Choose EC2 launch type for GPUs, special instance types or cheaper steady capacity.
- Give each ECS task its own IAM task role for least privilege.
- Pick EKS when the team already uses Kubernetes or needs portability.
Key terms
Tap a card to flip it. 0/5 known
Examples
ACCT=123456789012; REGION=us-east-1
REPO=$ACCT.dkr.ecr.$REGION.amazonaws.com
aws ecr create-repository --repository-name web
aws ecr get-login-password --region $REGION | \
docker login --username AWS --password-stdin $REPO
docker tag web:latest $REPO/web:1.0
docker push $REPO/web:1.0Create an ECR repository and push an image.
A Fargate task definition for a web app
Step 1: write the task definition below: Fargate-compatible, awsvpc networking, 0.25 vCPU and 512 MB, an execution role for pulling the image and writing logs, and a task role for the app's own AWS calls. Step 2: register it with aws ecs register-task-definition --cli-input-json file://web.json. Step 3: create a service with --launch-type FARGATE, two private subnets, a security group, and the ALB target group (target type ip). Step 4: check the service events; new tasks appear, pass health checks, and receive traffic.
{
"family": "web",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "256", "memory": "512",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::123456789012:role/web-task",
"containerDefinitions": [{
"name": "web", "essential": true,
"image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/web:1.0",
"portMappings": [{"containerPort": 8080}],
"logConfiguration": {"logDriver": "awslogs", "options": {"awslogs-group": "/ecs/web",
"awslogs-region": "us-east-1", "awslogs-stream-prefix": "web"}}
}]
}ECS or EKS: two companies
Company A is a small team moving a few Docker Compose services to AWS. Nobody knows Kubernetes, and the brief says least operational overhead. Step 1: no Kubernetes requirement, so ECS. Step 2: no GPUs or special instances, so Fargate. Step 3: images in ECR, services behind an ALB. Company B already runs 40 microservices on Kubernetes on-premises with Helm charts and wants the same tooling in AWS and the option to run elsewhere later. Step 1: existing manifests and portability point to EKS. Step 2: managed node groups or Auto Mode to limit node work, plus Fargate profiles for small, spiky jobs. The deciding words are "existing Kubernetes" and "portability" for EKS, and "simplest" or "least overhead" for ECS on Fargate.
Quick reference
- ECS: AWS-native, no control plane fee. EKS: Kubernetes, hourly fee per cluster
- Fargate: no servers, awsvpc only, up to 16 vCPU and 120 GB per task, no GPUs
- Execution role: pull image, write logs, fetch secrets. Task role: your app's AWS calls
- awsvpc tasks need target type ip in the load balancer target group
- Fargate Spot: big discount, two-minute warning before interruption
- ECR login: aws ecr get-login-password | docker login --username AWS --password-stdin
- ECR lifecycle policies expire old images; tag immutability blocks tag overwrites
- Private subnet tasks need NAT or endpoints for ECR api, ECR dkr, S3 and logs
Common mistakes
- Picking EKS when the scenario stresses least operational overhead with no Kubernetes need.
- Granting permissions on the task execution role meant for the task role.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Add a lifecycle policy to the ECR repository web that keeps only the 10 most recent images.
- Exercise 2medium
Register the web task definition and run it as a Fargate service with 2 tasks in two private subnets.
- Exercise 3hard
Run the web service with 2 tasks always on regular Fargate and extra tasks split 1:3 between Fargate and Fargate Spot.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is Fargate more expensive than EC2?
- Per vCPU-hour, usually yes. But you pay only for task size while it runs and skip idle capacity and server management, so for small or spiky workloads it is often cheaper overall.
- Can I move from ECS to EKS later?
- Yes, since both run standard container images, but you must rewrite task definitions as Kubernetes manifests and change deployment tooling.
- Do I need ECR, or can I use Docker Hub?
- ECS and EKS can pull from any registry, but ECR integrates with IAM, VPC endpoints and Inspector scanning, and avoids public registry rate limits.
- What is ECS Anywhere or EKS Anywhere?
- They let you run ECS tasks or EKS clusters on your own servers on premises, managed with the same AWS tools.