CPU and Memory

How to size tasks and containers with CPU units and memory limits.

ECS measures CPU in CPU units where 1024 units = 1 vCPU. Memory is in MiB.

Two levels

  • Task level — total cpu and memory for the whole task (required on Fargate).
  • Container levelcpu, memory (hard limit), and memoryReservation (soft limit) per container.

On EC2 the scheduler uses these numbers to decide which instance a task fits on.

Example

Example · json
{
  "family": "worker",
  "cpu": "1024",
  "memory": "2048",
  "containerDefinitions": [
    {
      "name": "worker",
      "image": "worker:2",
      "cpu": 768,
      "memory": 1536,
      "memoryReservation": 1024,
      "essential": true
    }
  ]
}

When to use it

  • A developer sets task-level CPU to 1024 units and container-level memory to 768 MB after profiling shows the app uses at most 600 MB under load.
  • An SRE increases the memory limit from 512 MB to 1024 MB after a container is repeatedly OOM-killed during a traffic spike.
  • A FinOps engineer right-sizes tasks from 2048 CPU to 512 CPU based on CloudWatch metrics showing average CPU usage below 10%, cutting Fargate costs by 75%.

More examples

Set Task-Level CPU and Memory

Sets the task-level resource reservation to 0.5 vCPU and 1 GB memory — valid Fargate combination that ECS validates at registration.

Example · json
{
  "family": "api",
  "cpu": "512",
  "memory": "1024",
  "requiresCompatibilities": ["FARGATE"],
  "networkMode": "awsvpc",
  "containerDefinitions": [
    {"name": "api", "image": "my-api:latest", "essential": true}
  ]
}

Container Hard and Soft Memory Limits

Sets a hard memory limit of 512 MB (container is killed if exceeded) and a soft reservation of 256 MB used for scheduling bin-packing.

Example · json
{
  "name": "worker",
  "image": "my-worker:latest",
  "cpu": 256,
  "memory": 512,
  "memoryReservation": 256,
  "essential": true
}

View CPU Utilization in CloudWatch

Fetches hourly average CPU utilization for an ECS service to identify over-provisioned tasks that can be right-sized.

Example · bash
aws cloudwatch get-metric-statistics \
  --namespace ECS/ContainerInsights \
  --metric-name CpuUtilized \
  --dimensions Name=ClusterName,Value=prod Name=ServiceName,Value=api-svc \
  --start-time 2024-01-01T00:00:00Z \
  --end-time 2024-01-02T00:00:00Z \
  --period 3600 \
  --statistics Average

Discussion

  • Be the first to comment on this lesson.