Skip to content
Kuza Ndoto Academy

Containers: ECS, EKS & Fargate

Beginner → IntermediateChapter 3 · Page 13 of 304 min readUpdated 15 Nov 2026

A container packages your app with everything it needs to run, so it behaves the same on your laptop and in the cloud. AWS offers several ways to run containers: Amazon ECS and Amazon EKS orchestrate them, AWS Fargate runs them without servers to manage, and Amazon ECR stores the images.

Containers in one minute

With Docker, you describe your app's environment in a Dockerfile, build it into an image, and run that image as one or more containers. Here is a Dockerfile for a small Django shop app served by Gunicorn:

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "shop.wsgi:application"]

Running one container on a laptop is easy. Running twenty across several servers, restarting crashed ones, rolling out new versions without downtime and connecting them to a load balancer is the job of a container orchestrator.

The AWS container services

ServiceWhat it isChoose it when
Amazon ECRA private registry for container imagesAlways: it is where ECS and EKS pull your images from
Amazon ECSAWS's own orchestrator, simpler and deeply integrated with AWSYou want the easiest path to running containers on AWS
Amazon EKSManaged Kubernetes: AWS runs the control planeYou need Kubernetes, its tools and portability, or already know it
AWS FargateServerless compute for containers, used by ECS or EKSYou do not want to manage EC2 instances for your containers

ECS and EKS decide what runs where; Fargate or EC2 instances provide the machines. With the Fargate option you just say "this task needs 0.25 vCPU and 512 MB" and AWS finds the capacity. With the EC2 option you run and patch the instances yourself, which can be cheaper at scale and allows GPUs and special instance types.

ECS vocabulary

  • Cluster: a logical group for your tasks and services.
  • Task definition: a JSON blueprint: which image, how much CPU and memory, which ports, environment variables, IAM roles and log settings.
  • Task: one running copy of a task definition (one or more containers together).
  • Service: keeps a desired number of tasks running, replaces failed ones, registers them with a load balancer and handles rolling deployments.

A minimal Fargate task definition looks like this (account ID and names are placeholders):

{
  "family": "nairobi-shop",
  "requiresCompatibilities": ["FARGATE"],
  "networkMode": "awsvpc",
  "cpu": "256",
  "memory": "512",
  "executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
  "taskRoleArn": "arn:aws:iam::123456789012:role/nairobi-shop-task",
  "containerDefinitions": [
    {
      "name": "web",
      "image": "123456789012.dkr.ecr.eu-west-1.amazonaws.com/nairobi-shop:1.0",
      "essential": true,
      "portMappings": [{ "containerPort": 8000, "protocol": "tcp" }],
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/nairobi-shop",
          "awslogs-region": "eu-west-1",
          "awslogs-stream-prefix": "web"
        }
      }
    }
  ]
}

Notice the two roles. The execution role lets ECS pull the image from ECR and write logs; the task role is what your app's code uses to call AWS, for example to upload product photos to S3.

From laptop to ECS

1. Create a repository and push the image to ECR (placeholders throughout):

aws ecr create-repository --repository-name nairobi-shop

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 --platform linux/amd64 -t nairobi-shop:1.0 .
docker tag nairobi-shop:1.0 123456789012.dkr.ecr.eu-west-1.amazonaws.com/nairobi-shop:1.0
docker push 123456789012.dkr.ecr.eu-west-1.amazonaws.com/nairobi-shop:1.0

The --platform linux/amd64 flag matters if you build on an ARM laptop (such as an Apple Silicon Mac) but run on x86 Fargate. Alternatively, set the task's runtime platform to ARM64 and use Graviton.

2. Register the task definition, create a cluster and run a service:

aws ecs register-task-definition --cli-input-json file://taskdef.json
aws ecs create-cluster --cluster-name shop-cluster
aws ecs create-service --cluster shop-cluster --service-name shop-web \
    --task-definition nairobi-shop --desired-count 2 --launch-type FARGATE \
    --network-configuration "awsvpcConfiguration={subnets=[subnet-0aaa1111bbbb2222c,subnet-0ddd3333eeee4444f],securityGroups=[sg-0123456789abcdef0],assignPublicIp=ENABLED}"

For production you would add --load-balancers to attach an ALB target group, place tasks in private subnets and turn off public IPs. The console's ECS wizard and tools such as AWS Copilot or the CDK can set all of this up for you.

Costs and clean-up

Warning

Fargate charges for the vCPU and memory of each task for as long as it runs, around the clock. EKS adds an hourly charge for every cluster's control plane, even if it runs nothing, so do not leave practice EKS clusters running. Load balancers and NAT gateways used by your services also bill hourly. To clean up ECS: set the service's desired count to 0, delete the service, delete the cluster, then delete unused images from ECR (stored images are billed per GB-month).

aws ecs update-service --cluster shop-cluster --service shop-web --desired-count 0
aws ecs delete-service --cluster shop-cluster --service shop-web
aws ecs delete-cluster --cluster shop-cluster
aws ecr delete-repository --repository-name nairobi-shop --force

Key points

  • Containers package an app with its dependencies; images live in ECR.
  • ECS is AWS's simpler orchestrator; EKS is managed Kubernetes.
  • Fargate runs containers without servers to manage; the EC2 option gives more control.
  • An ECS service keeps a desired number of tasks from a task definition running.
  • Use a task role for app permissions, and shut down services and clusters after practising.
Up next · New chapter: Storage & DatabasesAmazon S3Object storage: buckets, objects, storage classes and permissions.
→

Tip: use the ← and → keys to move between pages.