Sub topic 6
Choosing task CPU and memory
This chapter assumes you can read a Fargate task definition and have registered one before. It covers how Fargate sizes a task, which CPU and memory pairs are valid, how container-level settings interact with the task-level ones, and how to pick a size from measurements.
Task-level size is mandatory
On Fargate you must set cpu and memory on the task definition itself. These two values define the task's size. Fargate provisions a micro-VM of that size and bills for it, whatever your containers actually use.
cpuis in CPU units, where 1024 units equal 1 vCPU. You can write the value as a string such as"512", or as a vCPU string such as"0.5 vCPU".memoryis in MiB, for example"1024", or as a GB string such as"1 GB".
You cannot choose any pair you like. Fargate accepts only the combinations in this table (Linux tasks).
| CPU units | vCPU | Valid memory |
|---|---|---|
| 256 | 0.25 | 512 MiB, 1 GB, 2 GB |
| 512 | 0.5 | 1 GB to 4 GB, in 1 GB steps |
| 1024 | 1 | 2 GB to 8 GB, in 1 GB steps |
| 2048 | 2 | 4 GB to 16 GB, in 1 GB steps |
| 4096 | 4 | 8 GB to 30 GB, in 1 GB steps |
| 8192 | 8 | 16 GB to 60 GB, in 4 GB steps |
| 16384 | 16 | 32 GB to 120 GB, in 8 GB steps |
The two largest sizes may not be available on every platform version or Region. Check the current Fargate documentation before depending on them.
An invalid pair fails when you register the task definition. For example, 256 CPU units with 4096 MiB returns No Fargate configuration exists for given values. The usual fix is to move up one CPU tier, not to cut memory.
What the task size does and does not limit
The task size is a ceiling for all containers in the task combined. Fargate does not give each container its own machine. Containers share the task's CPU and memory, and this has consequences.
- Sidecars count. A log router, a proxy, or an observability agent takes part of the same budget as your application container.
- Usable memory is slightly less than the task memory. The platform and the ECS agent need some memory too. Do not plan to use 100 percent of it.
- CPU is soft, memory is hard. If containers want more CPU than the task has, they are throttled and slow down. If a container exceeds its memory limit, the kernel kills it.
Container-level settings
Each container definition can also set cpu, memory, and memoryReservation. They are optional on Fargate, and they behave differently from the task-level values.
memoryis a hard limit for that container. If the container goes over it, it is killed.memoryReservationis a soft limit. The container can use more when memory is free, up to the hard limit or the task total.cpuis a relative weight among containers. It is not a cap. A container withcpu: 256next to one withcpu: 768gets about a quarter of the CPU under contention, and can use idle CPU otherwise.- The container-level
memoryvalues added together cannot exceed the task'smemory.
This fragment sizes a task at 1 vCPU and 2 GB and reserves part of it for a sidecar. It also selects Arm architecture, which is covered below.
{
"family": "web-app",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "1024",
"memory": "2048",
"runtimePlatform": {
"cpuArchitecture": "ARM64",
"operatingSystemFamily": "LINUX"
},
"containerDefinitions": [
{
"name": "web",
"image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/web-app:1.0.0-arm64",
"essential": true,
"memory": 1536,
"portMappings": [{ "containerPort": 8080, "protocol": "tcp" }]
},
{
"name": "log-router",
"image": "public.ecr.aws/aws-observability/aws-for-fluent-bit:stable",
"essential": false,
"memoryReservation": 128
}
]
}Here the web container is capped at 1536 MiB. The router reserves 128 MiB and can borrow from the remaining headroom.
Failure modes
Most sizing problems show up in one of three ways.
- Out of memory. The container stops with exit code 137, and the stopped reason reads
OutOfMemoryError: Container killed due to memory usage. For an essential container the task stops, and the service starts a replacement. This often looks like a crash loop under load. - CPU throttling. The task stays up but latency rises, and health checks can time out. The service-level CPU metric sits near 100 percent.
- Over-provisioning. Nothing fails, but you pay for capacity you never use. Utilization stays under about 20 percent.
Memory errors cost more than CPU errors, because the task dies instead of slowing down. When unsure, leave more memory headroom than CPU headroom.
Choosing a size from measurements
Do not guess. Start from a reasonable size, run realistic load, and read the utilization.
The ECS service metrics in the AWS/ECS CloudWatch namespace report CPUUtilization and MemoryUtilization as a percentage of the task's reserved size. This command reads the last hour of peak CPU for a service.
aws cloudwatch get-metric-statistics \
--namespace AWS/ECS \
--metric-name CPUUtilization \
--dimensions Name=ClusterName,Value=demo Name=ServiceName,Value=web \
--start-time "$(date -u -d '1 hour ago' +%Y-%m-%dT%H:%M:%SZ)" \
--end-time "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
--period 300 \
--statistics Average MaximumThe date -d syntax is for GNU date. On macOS, use date -u -v-1H instead. Repeat the call with --metric-name MemoryUtilization.
Use these rules of thumb.
- Memory peak above about 80 percent: move up a memory step or a CPU tier.
- Memory peak under about 40 percent for days: try the next size down.
- Average CPU well below your auto scaling target: the tasks are too large, or the service has too many of them.
- Always compare the maximum, not only the average. A short spike can cause an out-of-memory kill that an average hides.
Scale out instead of up
A bigger task is not always the answer. For a stateless web service, several small tasks usually beat a few large ones.
- Auto scaling adds capacity in small, cheap increments.
- A failed task takes out a smaller share of your capacity.
- Single-threaded runtimes such as Node.js or Python with one worker cannot use more than one vCPU anyway.
Choose a larger task when one process needs the memory, such as a JVM heap or an in-memory cache, or when the application is genuinely multithreaded.
Cost levers tied to size
Fargate bills per second for the vCPU and memory you configure, with a one-minute minimum. Sizing is therefore the largest direct cost lever, because a task half the size costs about half as much.
- Setting
cpuArchitecturetoARM64runs the task on Graviton processors. Fargate charges less for Arm tasks than for x86 tasks of the same size. You must build an Arm or multi-architecture image, and an x86-only image fails at startup with anexec format error. - Fargate Spot, covered in a later chapter, lowers the price further for interruption-tolerant work.
Exercise
A task definition sets cpu "256" and memory "4096", and registration fails. A different service with cpu "1024" and memory "1536" also fails. What is wrong with each, and what is the smallest valid fix for each?
Show answer
Neither pair is in Fargate's table. 256 CPU units allow only 512 MiB, 1 GB, or 2 GB, so the first task needs 512 CPU units (which allow 1 GB to 4 GB) to keep 4096 MiB. 1024 CPU units require at least 2 GB, so the second task must raise memory to 2048 MiB, or drop to 512 CPU units, which allow 1536 MiB only if the memory is given in whole GB steps, so 1024 or 2048 MiB is the safe choice there.
Exercise
A service shows MemoryUtilization peaks of 95 percent and tasks occasionally stop with exit code 137, while average CPU is 25 percent. Which change do you make first, and why?
Show answer
Raise memory, either to the next valid memory step for the current CPU tier or to the next CPU tier if the pair requires it. Exit code 137 with an out-of-memory stopped reason means a container exceeded its memory limit and was killed. Low CPU shows that CPU is not the constraint, so adding CPU alone would not help. Register a new revision and deploy it with the circuit breaker enabled.