Lesson 4 of 11 · 12 min · Cloud basics
Containers in the cloud: ECS, EKS, Fargate and friends
What you will learn
- The options for running containers in the cloud
- ECS, Fargate, EKS and when to pick each
- Container registries and image flow from CI to the cluster
- Task definitions, networking, load balancing and logging
- IAM roles for containers: task role versus execution role
- Equivalents on Azure and Google Cloud
In simple words
You know how to run containers on one machine. In the cloud you choose how much of the work to hand over: run them on your own virtual machines, let the provider run the machines for you, or use a managed Kubernetes. This lesson compares those choices on AWS and shows how an image travels from your pipeline to production.
Words to know
- ECS: AWS's own service for running containers.
- EKS: Kubernetes run for you by AWS.
- Fargate: a way to run containers without managing any servers. You pay for each container.
- Registry (ECR): the place your container images are stored in the cloud.
From `docker run` to production
You can run containers on a single EC2 VM with Docker or Compose, and for small projects that is fine. Production needs more: multiple instances across zones, rolling deployments, restarts of failed containers, scaling, load balancing, secrets, logging and permissions. Cloud providers offer services that do this for you.
The image supply chain
code → CI builds image → scan → push to registry (ECR) → deploy references the immutable tag/digest
Amazon ECR is AWS's private registry: IAM-based access (no separate passwords), image scanning, immutable tags, lifecycle policies to delete old images, replication across regions, and pull-through caching of Docker Hub (avoiding rate limits). Others: Artifact Registry (GCP), Azure Container Registry, GitHub Container Registry. Authenticate Docker to ECR with a short-lived token:
aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-south-1.amazonaws.com
docker tag shop-api:sha-1a2b3c4 123456789012.dkr.ecr.ap-south-1.amazonaws.com/shop-api:sha-1a2b3c4
docker push 123456789012.dkr.ecr.ap-south-1.amazonaws.com/shop-api:sha-1a2b3c4
(The account number is a placeholder.) Tag with the commit SHA, enable tag immutability and scan on push (CI/CD course).
ECS concepts
- A cluster is a logical grouping.
- A task definition is the blueprint (like a Pod spec): containers, images, CPU/memory, ports, environment, secrets, logging, roles.
- A task is a running instance of a task definition (one or more containers that start and stop together).
- A service keeps a desired number of tasks running, replaces failed ones, registers them with a load balancer and handles rolling deployments (with optional circuit breaker for automatic rollback).
A minimal Fargate task definition (JSON):
{
"family": "shop-api",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "512",
"memory": "1024",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::123456789012:role/shop-api-task",
"containerDefinitions": [{
"name": "api",
"image": "123456789012.dkr.ecr.ap-south-1.amazonaws.com/shop-api:sha-1a2b3c4",
"essential": true,
"portMappings": [{"containerPort": 8080}],
"environment": [{"name": "APP_ENV", "value": "prod"}],
"secrets": [{"name": "DB_PASSWORD", "valueFrom": "arn:aws:secretsmanager:ap-south-1:123456789012:secret:shop/prod/db"}],
"logConfiguration": {"logDriver": "awslogs", "options": {"awslogs-group": "/ecs/shop-api", "awslogs-region": "ap-south-1", "awslogs-stream-prefix": "api"}}
}]
}
(The role and secret ARNs are placeholders.) Read it as the Docker/Kubernetes concepts in AWS syntax: image (immutable tag), port, environment variables, secrets injected from Secrets Manager (not stored in the definition), and logs shipped to CloudWatch. With awsvpc each task gets its own network interface and private IP in your subnet, with its own security group, which makes network rules precise (the database allows only the API's security group).
Two different IAM roles
This confuses everyone at first:
- Task execution role: used by the ECS agent to start your task: pull the image from ECR, fetch secrets, write logs. It is about launching.
- Task role: the identity your application code uses at run time to call AWS APIs (read an S3 bucket, publish to SQS). Give it only what the application needs (IAM lesson).
On Kubernetes the equivalent of the task role is IAM Roles for Service Accounts (IRSA) or EKS Pod Identity. Never put access keys in environment variables or images.
Networking and load balancing
- Run tasks in private subnets across at least two zones; put an Application Load Balancer in public subnets in front. The service registers task IPs as targets; the ALB health-checks
/healthzand routes by host/path. Security group rules: ALB accepts 80/443 from the internet; tasks accept the app port only from the ALB's security group; the database accepts only from the tasks' group (security group lesson). - Tasks reach the internet (to pull images, call APIs) through a NAT gateway or VPC endpoints for AWS services (NAT lesson).
- Service-to-service discovery: ECS Service Connect / Cloud Map, internal load balancers, or DNS names.
- On EKS: the AWS Load Balancer Controller creates ALBs from Ingress objects; pods get VPC IPs via the VPC CNI.
Deployments and scaling
- Rolling update is the default: new tasks start, pass health checks, old ones drain. Configure
minimumHealthyPercentandmaximumPercent(the same ideas as KubernetesmaxUnavailable/maxSurge) and enable the deployment circuit breaker to roll back automatically if new tasks keep failing. - Blue/green and canary via CodeDeploy.
- Auto scaling: target tracking on average CPU/memory or on ALB requests per target; scale between a minimum (at least 2 for availability) and maximum.
- Shut down gracefully: handle SIGTERM within
stopTimeout(Docker lesson).
Logging, monitoring, debugging
Send container stdout/stderr to CloudWatch Logs (or use FireLens to route to other systems); metrics (CPU, memory, task count) go to CloudWatch; set alarms on service health and 5xx rates. ECS Exec gives a shell into a running container (like kubectl exec) with audit logging. Containers that stop immediately are diagnosed from task "stopped reason" and the exit code (137 = OOM: raise memory).
Cost notes
Fargate bills for the vCPU and memory you request, per second, regardless of use: right-size task definitions (measure with CloudWatch Container Insights). Fargate Spot gives large discounts for interruptible tasks; Graviton (ARM) tasks are cheaper; EKS adds a fixed hourly fee per cluster plus nodes. Idle development services should scale to zero or be stopped.
Common mistakes
- Putting secrets in environment variables of the task definition instead of referencing Secrets Manager/Parameter Store.
- Confusing task role and execution role, leading to permission errors or over-permissioned roles.
- Public IPs on tasks instead of private subnets with a load balancer.
latesttags in task definitions.- Single task, single zone.
- Choosing EKS "because everyone does" for a handful of services.
- No memory limits/right-sizing, causing OOM kills or waste.
- Forgetting image clean-up in ECR (lifecycle policy).
Try it yourself
- Push an image to ECR (or a local registry), write a Fargate task definition and run it as a service behind an ALB in a sandbox account; then delete everything.
- Explain, for your application, which permissions belong in the task role and which in the execution role.
- Configure target-tracking auto scaling and test it with a load generator.
- Deploy the same image to Cloud Run or App Runner and compare the effort to ECS.
Check yourself
What is the difference between ECS and EKS?
ECS is AWS's own container orchestrator with a simpler model; EKS is managed Kubernetes. EKS offers the Kubernetes API and ecosystem at the cost of more complexity.
What does Fargate remove from your responsibilities?
Managing the servers that run containers: no instances to provision, patch or scale; you pay per vCPU and memory of the tasks.
What is the difference between the ECS task role and the task execution role?
The execution role lets ECS pull the image, fetch secrets and write logs when starting the task; the task role is what your application code uses to call AWS services.
How do containers get secrets securely on ECS?
The task definition references secrets in Secrets Manager or Parameter Store; ECS injects them at start using the execution role, so values are not stored in the definition or image.
Interview questions
How would you deploy a containerised API on AWS with high availability?
ECR for images; an ECS service on Fargate with at least two tasks across two private subnets in different zones; an ALB in public subnets with health checks; security groups chaining ALB → tasks → database; secrets from Secrets Manager; CloudWatch logs and alarms; auto scaling; and a CI/CD pipeline deploying immutable tags with circuit-breaker rollback.
When would you choose Kubernetes over ECS?
When you need the Kubernetes ecosystem (Helm, operators, service mesh, GitOps tools), multi-cloud/portable workloads, or teams already skilled in it. Otherwise ECS (or a serverless container service) is simpler.
How do you give a container access to an S3 bucket without access keys?
Attach an IAM role to the task (ECS task role) or pod (IRSA / Pod Identity); the SDK obtains temporary credentials automatically.
Summary
- Run containers with managed services: ECS (Fargate for serverless), EKS for Kubernetes, App Runner / Cloud Run for the simplest cases.
- Image flow: CI → scan → ECR with immutable SHA tags → task definition/Deployment.
- ECS concepts: cluster, task definition, task, service;
awsvpcgives each task its own IP and security group. - Use separate execution and task roles, private subnets behind an ALB, secrets from a secrets manager, circuit-breaker deployments and right-sized resources.
Going deeper
- Amazon ECS developer guide: concepts and tasks.
- Task definition parameters: every field.
- Amazon EKS user guide: managed Kubernetes on AWS.
- AWS Fargate: serverless compute for containers.
- Google Cloud Run documentation: serverless containers on Google Cloud.
- Azure Container Apps: serverless containers on Azure.
See how much of this you can explain
A free mock interview asks you new questions on Cloud platforms and AWS fundamentals for DevOps Engineer and scores every answer, with what to fix.