Jesus Paz · 2 min read

How to Compute Cost per ECS Task When Using EC2 or Fargate

Detailed formulas for allocating EC2 and Fargate spend to each ECS task, including shared networking and storage.

ecs aws

Knowing the cost of an individual ECS task unlocks better product pricing, more accountable engineering teams, and faster FinOps cycles. Let’s break down the math for both EC2-backed and Fargate workloads.

Step 1: Gather the data

  • Task metadata: CPU/memory reservations, runtime, service name, cluster, capacity provider.
  • Instance metrics (EC2 launch type): Instance type, hourly price, CPU/memory totals, EBS cost.
  • Fargate pricing: Region-specific vCPU and memory per-second rates.
  • Shared services: Load balancers, NAT, CloudWatch logs.

ClusterCost captures all of this automatically via the ECS API and AWS billing feeds.

Step 2: EC2 launch type formula

For each instance hour:

instance_unit_cost_cpu = instance_hourly_cost / allocatable_cpu
instance_unit_cost_memory = instance_hourly_cost / allocatable_memory
task_cost = (task_cpu_reserved * instance_unit_cost_cpu) +
(task_memory_reserved * instance_unit_cost_memory)

Enhancements:

  • Use usage instead of reservations if you want to reward efficiency.
  • Include EBS cost by dividing volume spend by the tasks that mount it.

Step 3: Fargate launch type formula

task_cost = (vCPU * price_per_vCPU_second + memory * price_per_GiB_second) * runtime_seconds

Add the Windows/GPU premium if applicable, and include ephemeral storage when using the 20/200 GiB options.

Step 4: Attach shared infrastructure

  • Load balancers: Attribute by requests per service.
  • NAT gateways: Attribute by bytes transferred per service.
  • CloudWatch logs: Attribute by log volume (GB) per task group.

ClusterCost lets you configure these policies so the allocation happens automatically.

Step 5: Aggregate and publish

Useful views:

  • Task → Service → Team.
  • Task → Environment (prod/stage/dev).
  • Task → Customer/tenant.

Export the data weekly to finance and to engineering dashboards so everyone can see the impact of their deployments.

Step 6: Act on the insights

  • Highlight tasks with chronically low CPU usage and shrink reservations.
  • Compare EC2 vs. Fargate cost per task to choose the right launch type.
  • Surface idle services (no requests, no deployments) and retire them.

With ClusterCost calculating task costs in real time, you finally have the granularity needed to run ECS like a business, not a black box.***

Related reading

Join 1,000+ FinOps and platform leaders

Get Kubernetes and ECS cost tactics delivered weekly.