Skip to content
Tutorials Center
My learning

Topics

Lesson 42 of 70 ยท Load balancing, scaling and delivery

Containers: ECR, ECS and Fargate

Run containers on AWS: store images in ECR, define tasks and services in ECS, choose Fargate or EC2, compare with EKS, and try a task-placement emulator.

A container packages your program with everything it needs, so it runs the same on a laptop and in production. If you have not used containers yet, the Docker tutorial teaches images and Dockerfiles from scratch. This lesson is about the next question: where do the containers run, and who keeps them alive?

AWS answers with three services that work together. ECR (Elastic Container Registry) stores your images. ECS (Elastic Container Service) is the orchestrator that decides where containers run and restarts them when they fail. Fargate is a way of running ECS containers without managing any servers. A fourth service, EKS, is managed Kubernetes for teams that want that instead.

None of these services can run on this site, so the commands and JSON are read-only. The Python examples are stand-ins: a task-definition checker with Fargate's CPU and memory rules, and a placement emulator that shows how a scheduler picks instances. They simplify the real scheduler.

ECR: the image registry

ECR is a private Docker registry inside your account. Each repository holds the versions (tags) of one image, such as shop/web. Access is controlled by IAM, images can be scanned for known vulnerabilities, and data is encrypted at rest.

The standard push workflow (read-only):

Terminal
aws ecr create-repository --repository-name shop/web --image-tag-mutability IMMUTABLE

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/web:1.4.2 .
docker tag shop/web:1.4.2 123456789012.dkr.ecr.eu-west-1.amazonaws.com/shop/web:1.4.2
docker push 123456789012.dkr.ecr.eu-west-1.amazonaws.com/shop/web:1.4.2

The login token is temporary (12 hours), which is why no long-lived password is needed. Good habits:

  • Immutable tags (as above) stop someone from replacing 1.4.2 with a different image. Avoid deploying latest, because you cannot tell which build is running.
  • Lifecycle policies delete old images automatically, for example "keep the last 20 images", so storage costs do not grow forever.
  • Scan on push and watch the findings, and keep base images small.

ECS concepts

ECS has a few nouns, and the whole service makes sense once you know them:

  • A task definition is the blueprint, like a Compose file for AWS. It lists one or more containers: image, CPU and memory, ports, environment variables, secrets, log configuration. It also carries the task role (what your code may call in AWS) and the execution role (what ECS needs to pull the image and write logs). Task definitions are versioned: web:1, web:2, and so on.
  • 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.
  • A cluster is the logical group of services and tasks (and, with the EC2 launch type, the instances).

A shortened task definition for a web container (read-only):

JSON
{
  "family": "web",
  "networkMode": "awsvpc",
  "requiresCompatibilities": ["FARGATE"],
  "cpu": "512",
  "memory": "1024",
  "executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
  "taskRoleArn": "arn:aws:iam::123456789012:role/web-task-role",
  "containerDefinitions": [
    {
      "name": "web",
      "image": "123456789012.dkr.ecr.eu-west-1.amazonaws.com/shop/web:1.4.2",
      "portMappings": [{ "containerPort": 8080 }],
      "environment": [{ "name": "APP_ENV", "value": "production" }],
      "secrets": [
        { "name": "DB_PASSWORD", "valueFrom": "arn:aws:secretsmanager:eu-west-1:123456789012:secret:shop/db-AbCdEf" }
      ],
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/web",
          "awslogs-region": "eu-west-1",
          "awslogs-stream-prefix": "web"
        }
      }
    }
  ]
}

With awsvpc networking, every task gets its own network interface with a private IP in your subnet and its own security group. That is why security groups can say "allow port 8080 only from the load balancer", exactly as for instances.

flowchart LR
    ECR["ECR<br/>shop/web:1.4.2"] --> TD["Task definition<br/>web:7"]
    TD --> SVC["ECS service<br/>desired count 3"]
    SVC --> T1["Task 1<br/>own IP"]
    SVC --> T2["Task 2<br/>own IP"]
    SVC --> T3["Task 3<br/>own IP"]
    ALB["Application Load Balancer"] --> T1
    ALB --> T2
    ALB --> T3

The service starts tasks from a versioned task definition, and registers each task (by IP) in the load balancer's target group.

Services and deployments

When you update a service to a new task definition revision, ECS performs a rolling deployment by default. Two numbers control it: minimumHealthyPercent (how much of the desired count must stay healthy, default 100) and maximumPercent (how far above the desired count the service may go temporarily, default 200). With 3 desired tasks and 100/200, ECS starts new tasks first, waits until they are healthy in the load balancer, then stops old ones, so capacity never drops. The deployment circuit breaker can detect that new tasks keep failing and roll back automatically. ECS also supports blue/green deployments through CodeDeploy, and service auto scaling with the same target-tracking idea as Auto Scaling Groups, using CPU, memory or request count per target.

Fargate or EC2 launch type

A task runs on one of two kinds of capacity:

Fargate EC2 launch type
Servers to manage None. Each task runs in its own isolated micro-VM You run a fleet of instances (usually in an Auto Scaling group) with the ECS agent
Billing Per second for the vCPU and memory you ask for (1 minute minimum) You pay for the instances, used or not
Sizing Fixed CPU and memory combinations Any instance type, including GPUs and large local disks
Packing Not your concern You choose placement and fill instances well, or waste money
Good for Most web services, APIs, workers, batch, small teams Steady, large, cost-sensitive fleets, special hardware

For a new project, start with Fargate. Move to EC2 only when you have a measured reason: sustained high utilization where packing saves money, or hardware that Fargate does not offer. You can also mix both in one cluster using capacity providers, and run Fargate Spot for interruptible work at a discount.

Fargate accepts only certain CPU and memory pairs. The checker below encodes them for Linux tasks (always verify the current table in the documentation when you build something real) and estimates a monthly cost from example rates that are kept as variables you can change. They are not a promise of current prices.

Python Fargate sizes and a cost estimate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first and last combinations are valid. Memory of 4096 MiB is too high for 0.25 vCPU, and 500 is not a CPU size at all. The estimate shows that cost grows linearly with the number of tasks, which is why right-sizing matters, as in Cost Management and Optimization.

Placement on the EC2 launch type

With Fargate, AWS finds room for every task. With EC2, ECS must choose an instance that has enough free CPU and memory. Placement strategies guide the choice:

  • spread distributes tasks evenly across a value such as attribute:ecs.availability-zone, which improves availability.
  • binpack fills the instance that is most full (by CPU or memory) first, which leaves other instances empty so they can be scaled in, and saves money.
  • random picks any instance that fits.

Placement constraints add rules such as "only on m5.large instances". If no instance has room, the task stays pending until the cluster grows, which is why a capacity provider with managed scaling is so useful.

The stand-in below places 8 tasks of 512 CPU / 1024 MiB on four instances (two per zone, each with 2048 CPU and 4096 MiB) using spread across zones, then binpack on memory, and prints the result. It is a simplified model: real ECS also reserves resources for the agent and system.

Python Spread vs binpack placement

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Spread puts tasks in alternating zones and keeps all four instances busy, which survives a zone failure. Binpack fills i-a1 and i-a2 first (four tasks fit on each... only up to memory), leaving the b instances empty, so they can be removed, at the price of keeping everything in one zone until it is full. Real clusters often combine both: spread across zones first, then binpack inside each zone.

Service discovery and the EKS comparison

Tasks come and go, and their IPs change, so other services need a stable way to find them. Options:

  • Put an internal load balancer in front of the service, and use its DNS name. It is simple and works with everything.
  • ECS Service Discovery with AWS Cloud Map registers each task's IP in a private DNS namespace, so orders.shop.local resolves to the healthy tasks.
  • ECS Service Connect adds a managed proxy with short names, retries and metrics between services, without you running a service mesh.

EKS runs the Kubernetes control plane for you, and you bring workloads as Pods and Deployments, on EC2 nodes, managed node groups or Fargate. Choose EKS when you already know Kubernetes, need its ecosystem (Helm charts, operators, a portable setup across clouds), or have a platform team to run it. Choose ECS when you want the shortest path on AWS: fewer concepts, deep IAM and load balancer integration, and no cluster fee (EKS charges per cluster each hour). Neither is "better"; the wrong choice is paying for Kubernetes complexity you do not use.

Driving ECS from the CLI

Everything the console does is an API call, and the aws ecs command groups them. The commands below are read-only examples: they need an account and cost real money if you run them.

Terminal
# clusters
aws ecs create-cluster --cluster-name demo
aws ecs describe-clusters --clusters demo
aws ecs list-clusters

# task definitions are versioned; registering the same family creates a new revision
aws ecs register-task-definition --cli-input-json file://web-task.json
aws ecs list-task-definition-families
aws ecs describe-task-definition --task-definition web:3
aws ecs deregister-task-definition --task-definition web:1   # marks it INACTIVE

# a service keeps tasks running
aws ecs create-service --cluster demo --service-name web \
  --task-definition web:3 --desired-count 3 --launch-type FARGATE \
  --network-configuration "awsvpcConfiguration={subnets=[subnet-0a],securityGroups=[sg-0b]}"
aws ecs update-service --cluster demo --service web --desired-count 5
aws ecs describe-services --cluster demo --services web

# a one-off task (a migration, a batch job) with no service
aws ecs run-task --cluster demo --task-definition migrate:2 --launch-type FARGATE \
  --network-configuration "awsvpcConfiguration={subnets=[subnet-0a],assignPublicIp=DISABLED}"
aws ecs list-tasks --cluster demo
aws ecs describe-tasks --cluster demo --tasks <task-id>
aws ecs stop-task --cluster demo --task <task-id>

run-task, start-task and services

  • create-service is for long-running software. ECS replaces failed tasks and connects them to a load balancer.
  • run-task starts a task and lets ECS choose where it runs, using your launch type or capacity provider and the placement rules. Use it for batch jobs and one-off work such as database migrations. When the essential container exits, the task stops and ECS does not restart it.
  • start-task is the EC2-only variant where you name the container instance. It is rare, for cases where you need a specific host. On Fargate you never use it.

Capacity providers

A capacity provider tells a cluster where tasks may run. The built-in ones are FARGATE and FARGATE_SPOT. You can also create one backed by an EC2 Auto Scaling group, with managed scaling so that ECS grows and shrinks the group to fit the tasks. A capacity provider strategy has a base (a minimum number of tasks on one provider) and a weight for the rest.

Terminal
aws ecs put-cluster-capacity-providers --cluster demo \
  --capacity-providers FARGATE FARGATE_SPOT \
  --default-capacity-provider-strategy \
    capacityProvider=FARGATE,base=1,weight=1 \
    capacityProvider=FARGATE_SPOT,weight=3

This strategy keeps the first task on regular Fargate (it will not be interrupted), then splits the rest 1 to 3 between Fargate and Fargate Spot. The next stand-in shows how many tasks each provider gets. ECS's own rounding for ratios that do not divide evenly can differ slightly, so the example uses counts that split exactly.

Python How a capacity provider strategy splits tasks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Deployment controllers and task sets

A service has a deployment controller: ECS (the rolling update described above), CODE_DEPLOY (blue/green through AWS CodeDeploy) or EXTERNAL (you or a third-party tool control the rollout). Services with the CodeDeploy or external controller work with task sets: a task set is a group of tasks of one task definition revision inside a service, with a scale percentage. A blue/green rollout is just two task sets, where traffic moves from the old set to the new one. The commands create-task-set, update-task-set, describe-task-sets, delete-task-set and update-service-primary-task-set manage them, and you will rarely type them yourself unless you build your own deployment tool.

Container instances, settings and tags

With the EC2 launch type each instance runs the ECS agent, which registers it as a container instance (list-container-instances, describe-container-instances). Before maintenance, set an instance to DRAINING with update-container-instances-state, and ECS moves its service tasks elsewhere. update-container-agent upgrades the agent. A few other groups of commands:

  • Account settings (list-account-settings, put-account-setting-default, delete-account-setting) switch account-wide features on, for example Container Insights metrics or ENI trunking.
  • Attributes (put-attributes, list-attributes) label container instances so that placement constraints can use them.
  • Tags (tag-resource, untag-resource, list-tags-for-resource) work on clusters, services, task definitions and tasks, and feed cost allocation.

You will also see submit-task-state-change and discover-poll-endpoint in the API list. They are used by the ECS agent itself, not by people.

AWS Copilot

AWS Copilot is a separate command-line tool, copilot, for people who want to deploy containers without writing task definitions and CloudFormation by hand. copilot init asks for a Dockerfile and a service type (load-balanced web service, backend service, scheduled job), then builds the image, pushes it to ECR and creates the cluster, networking, roles and service. It has three layers of nouns: an application (the whole project), environments (test, prod) and services or jobs. Under the hood it generates CloudFormation stacks, so you can inspect what it did, and a short manifest file keeps your settings in Git. Copilot suits small teams and prototypes. When you need very custom networking or want everything in your own Terraform, use the CLI, CloudFormation or CDK directly.

Fargate: the trade-offs

Fargate is the easy choice, but it is not free of limits:

  • Cost at high utilization. You pay for the vCPU and memory you request, per second, with a one-minute minimum, plus extra for ephemeral storage above the 20 GiB included. A fleet of well-packed EC2 instances can be cheaper for a large, steady load. Arm (Graviton) tasks cost less, Fargate Spot is up to about 70% cheaper for interruptible work, and Compute Savings Plans apply to Fargate.
  • Fixed sizes. Only the allowed CPU and memory pairs exist, so a task that needs a lot of memory but little CPU pays for CPU it does not use.
  • No host access. There is no SSH, no privileged containers and no Docker-in-Docker. For debugging use ECS Exec, which opens a shell in a running container through Systems Manager.
  • Slower starts. A task must provision a micro-VM and pull its image, usually tens of seconds, which is slower than a process starting on a warm EC2 host. Small images and image caching help.
  • Limited hardware. No GPUs or special instance types.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the difference between an ECS task definition and a service?

    The task definition describes containers, resources and roles. The service uses a revision of it, keeps the desired count running and registers tasks with a load balancer.
  2. 2. What does the Fargate launch type remove compared with the EC2 launch type?

    Fargate runs each task in its own isolated environment, so you do not patch, scale or pack instances. You still need images, roles and networking.
  3. 3. Why use immutable image tags in ECR?

    Immutable tags guarantee that 1.4.2 always points to the same image, which makes deployments and rollbacks predictable.
  4. 4. What does the binpack placement strategy do on EC2-backed ECS?

    Binpack concentrates tasks to use instances fully and lets you scale unused ones in, while spread favors availability across zones.
  5. 5. A rolling ECS deployment runs with minimumHealthyPercent 100 and maximumPercent 200, desired count 4. What happens?

    With 100 percent minimum healthy, ECS may not go below 4 healthy tasks, and with 200 percent maximum it can run up to 8, so it starts new ones before removing old ones.

Summary

  • ECR stores images per repository. Use immutable tags, lifecycle policies and scanning, and log in with a short-lived token.
  • An ECS task definition is the blueprint, a task is one running copy, and a service keeps the desired count running and talks to the load balancer. Task roles and execution roles are separate.
  • Fargate removes server management and bills per vCPU and memory for each task. The EC2 launch type gives more control and cheaper packing for large steady fleets.
  • On EC2, spread favors availability and binpack favors cost. Rolling deployments are tuned with minimum healthy and maximum percent.
  • Use an internal load balancer, Cloud Map service discovery or Service Connect to find tasks. Choose EKS when you need Kubernetes itself, otherwise ECS is simpler.

Next: Elastic Beanstalk, App Runner and Amplify