Sub topic 19
Understanding Fargate cost
This chapter assumes you can read a Fargate task definition, know how task CPU and memory settings are chosen, and have run a service with a load balancer. It explains what you are billed for, how to estimate a monthly bill, and which changes reduce it.
What Fargate bills for
Fargate charges for the resources you request for a task, not for what the containers actually use. A task that asks for 1 vCPU and 2 GB costs the same whether it idles or runs at full load.
The main billing dimensions are:
- vCPU-seconds. The vCPU you set at the task level.
- GB-seconds of memory. The memory you set at the task level.
- Ephemeral storage. Every task gets 20 GiB at no charge. If you raise
ephemeralStorage.sizeInGiB(up to 200), you pay for the extra GiB-hours. - Operating system and architecture. Linux on ARM (Graviton) has lower rates than Linux on x86. Windows tasks add a licensing charge.
Billing is per second with a one-minute minimum. The clock starts when ECS begins pulling your image and stops when the task ends. A slow image pull or a long startup is therefore billed time. A task that crash-loops and restarts every 90 seconds is billed for each attempt.
Estimating a monthly bill
Rates differ by region and change over time. The figures below are approximate us-east-1 Linux rates, used only for the arithmetic. Check the AWS Fargate pricing page before you rely on them.
| Platform | Per vCPU-hour | Per GB-hour |
|---|---|---|
| Linux x86 | $0.04048 | $0.004445 |
| Linux ARM (Graviton) | $0.03238 | $0.00356 |
The following script turns the rates into a monthly estimate. A month is taken as 730 hours.
RATES = {
"x86": {"vcpu": 0.04048, "gb": 0.004445},
"arm": {"vcpu": 0.03238, "gb": 0.00356},
}
def monthly_cost(vcpu, memory_gb, tasks, arch="x86", spot_discount=0.0, hours=730):
rate = RATES[arch]
per_hour = vcpu * rate["vcpu"] + memory_gb * rate["gb"]
return per_hour * hours * tasks * (1 - spot_discount)
print(f"x86, 2 tasks: ${monthly_cost(0.25, 0.5, 2):.2f}")
print(f"arm, 2 tasks: ${monthly_cost(0.25, 0.5, 2, arch='arm'):.2f}")
print(f"x86, 70% spot: ${monthly_cost(0.25, 0.5, 2, spot_discount=0.7):.2f}")For two 0.25 vCPU / 0.5 GB tasks running all month, this prints about $18.02 on x86, $14.42 on ARM, and $5.41 with a 70 percent Spot discount. The Spot figure is a best case, because the actual discount varies with capacity.
Costs outside the task
The task is often not the largest line on the bill. A small service can cost more in supporting resources than in compute:
- Application Load Balancer. There is an hourly charge plus load balancer capacity units (LCUs) based on traffic.
- NAT gateway. Tasks in private subnets that pull from the internet pay an hourly charge plus a per-GB processing charge. Image pulls from ECR and log traffic can run through it.
- CloudWatch Logs. Ingestion is billed per GB, and storage continues until you set a retention period.
- Data transfer. Traffic between Availability Zones and out to the internet is billed.
- ECR. Image storage is billed per GB-month.
Cost levers
Right-size tasks
Because you pay for requested capacity, oversized tasks are the most common waste. Compare the CPU and memory your service uses with what it requests, using CloudWatch metrics such as CPUUtilization and MemoryUtilization. Look at peak values and not just averages. Memory needs headroom, because a task that exceeds its limit is killed.
Valid sizes come in fixed combinations. Moving a task from 1 vCPU / 4 GB to 0.5 vCPU / 2 GB halves its price. Often two smaller tasks are cheaper than one large one, and they also spread across Availability Zones. Auto scaling then adds capacity in smaller steps.
Use Graviton
ARM tasks cost about 20 percent less per vCPU and per GB. You need an image built for arm64, either with a multi-architecture build or an ARM-only build. Set the platform in the task definition:
"runtimePlatform": {
"cpuArchitecture": "ARM64",
"operatingSystemFamily": "LINUX"
}A task definition set to ARM64 that points at an x86-only image fails with an exec format error in the logs. Test the image on ARM before you change a production service.
Use Fargate Spot
Fargate Spot runs tasks on spare capacity at a discount of up to about 70 percent. AWS can reclaim that capacity. It sends the task SIGTERM and a two-minute warning, and the stopTimeout setting controls how long your container has to shut down.
Spot suits stateless workers, batch jobs, and extra web capacity. It does not suit a service that must have every task running. The capacity provider strategy from earlier in this tutorial handles this by keeping a base of regular Fargate tasks and sending the rest to Spot. The weight values split the remaining tasks between the two providers. Spot is available for Linux tasks only.
Compute Savings Plans
A Compute Savings Plan is a commitment to spend a set amount per hour for one or three years. In return, eligible usage is billed at a lower rate. It applies to Fargate, EC2, and Lambda usage. It suits the steady baseline of your usage. Keep bursty or experimental usage outside the commitment. If you commit above your real baseline, you pay for unused commitment. Spot usage is not covered by Savings Plans.
Common mistakes
- Leaving unused services running. Dev and test services at a desired count of 2 cost money around the clock. Scale them to zero outside working hours with scheduled scaling.
- Setting no log retention. Log groups keep data forever by default.
- Sizing for the peak only. If traffic spikes briefly, scale out with target tracking instead of permanently sizing every task for the spike.
- Ignoring startup time. Large images and slow startup add billed seconds on every task launch. This also slows scale-out.
- Moving everything to Spot. A service with all tasks on Spot can lose its full capacity when AWS reclaims it. Keep a regular
base.
Exercise
A service runs 3 tasks continuously for 730 hours on Linux x86. Each task is 1 vCPU and 2 GB. Using the x86 rates in this chapter, what is the monthly compute cost? Then name one change that would lower it without changing task count and one risk of that change.
Show answer
One task costs 1 × $0.04048 + 2 × $0.004445 = $0.04937 per hour. Over 730 hours that is about $36.04, so three tasks cost about $108.12 per month. Moving to ARM lowers the rates by about 20 percent, giving about $86. The risk is that the image must be built for arm64, and an x86-only image fails at startup. Other valid answers are right-sizing the tasks, which risks out-of-memory kills if you cut too far, or using Spot for some tasks, which risks interruption.