Sub topic 18
Understanding Fargate cost
This chapter assumes you can read a task definition, know how task CPU and memory are sized, and have seen capacity providers. It explains what Fargate bills for and which settings change the bill.
What you pay for
Fargate charges for the vCPU and memory that a task requests, for as long as the task runs. Billing starts when the image pull begins and stops when the task stops. Each task has a minimum charge period of one minute. Usage is billed per second after that.
You pay for what you request, not what the container uses. A task sized at 1 vCPU and 2 GB costs the same whether it is idle or busy. This is why sizing is the largest cost lever.
Two smaller charges also apply:
- Ephemeral storage. Each task gets 20 GiB at no extra charge. You pay only for storage you configure above that, up to 200 GiB.
- Other services. Load balancers, NAT gateways, CloudWatch Logs, and data transfer are billed separately and often matter as much as compute.
Check current rates on the AWS Fargate pricing page. Prices vary by Region, operating system, and CPU architecture.
Estimating a monthly cost
The formula is the same for every task:
cost = (vCPU x vCPU price + GB x memory price) x hoursThe sketch below applies it. The prices are placeholders, so replace them with current ones for your Region.
VCPU_PER_HOUR = 0.04 # placeholder price, USD
GB_PER_HOUR = 0.004 # placeholder price, USD
HOURS_PER_MONTH = 730
def monthly_cost(vcpu, memory_gb, tasks):
hourly = vcpu * VCPU_PER_HOUR + memory_gb * GB_PER_HOUR
return hourly * HOURS_PER_MONTH * tasks
# The web-app task from earlier: 0.25 vCPU, 0.5 GB, two tasks always on
print(round(monthly_cost(0.25, 0.5, 2), 2))Multiply by the average task count, not the maximum. Auto scaling keeps that average low when traffic varies.
The main levers
| Lever | Effect | Trade-off |
|---|---|---|
| Right-size CPU and memory | Lowers cost in proportion to the reduction | Too little memory causes out-of-memory kills |
| Fargate Spot | Large discount, up to about 70 percent | Tasks can be interrupted with a two-minute warning |
| Graviton (ARM) tasks | Lower price per vCPU and often better performance | You must build an ARM64 image |
| Compute Savings Plans | Discount for a one- or three-year spend commitment | You pay the commitment even if usage drops |
| Auto scaling | Runs fewer tasks off-peak | Scaling lag and cooldown settings need tuning |
To use Graviton, set the runtime platform in the task definition and push an arm64 image:
"runtimePlatform": {
"cpuArchitecture": "ARM64",
"operatingSystemFamily": "LINUX"
}Costs outside the task
A private subnet with assignPublicIp=DISABLED needs a NAT gateway or VPC endpoints to pull images from ECR and send logs. NAT gateways charge per hour and per GB processed. Image pulls and log traffic can add up quickly. Interface endpoints for ECR, CloudWatch Logs, and a gateway endpoint for S3 often cost less than NAT at high volume.
Log retention also grows quietly. CloudWatch Logs keeps data forever by default, so set a retention period on each log group.
A practical order of work
- Measure real CPU and memory use in CloudWatch Container Insights or service metrics.
- Shrink the task to the smallest valid size with headroom of about 20 to 30 percent.
- Move fault-tolerant tasks to Fargate Spot, keeping a
baseof regular Fargate tasks. - Move to ARM64 if your dependencies support it.
- Cover steady baseline usage with a Savings Plan.
Exercise
A service runs four tasks at all times, each sized at 1 vCPU and 2 GB. Average CPU use is 15 percent. Name two changes that lower the bill without lowering availability.
Show answer
Right-size the tasks, for example to 0.5 vCPU and 1 GB, since billing follows requested size and not use. Alternatively, add target tracking auto scaling so off-peak hours run fewer tasks, or move some tasks to Fargate Spot with a regular Fargate base. Moving to ARM64 and adding a Savings Plan also lower the rate.