Free — no signup required

The Tool: Infracost

3 min read

What Infracost Does

Infracost is the industry-standard open-source tool for IaC cost estimation. It bridges the technical world of Terraform with the financial world of cloud pricing APIs, producing human-readable cost breakdowns from infrastructure code before anything is deployed.

Infracost supports Terraform natively and has growing support for Pulumi and Terragrunt. It covers all major cloud providers: AWS, Azure, and Google Cloud. Its pricing data is sourced from official cloud provider APIs and updated regularly, so estimates reflect current list prices.

How It Works: The Five-Step Pipeline

When you run infracost breakdown, the following happens internally:

Step 1 — Parse: Infracost reads your Terraform code or, preferably, a terraform plan -out=plan.json output. Using the plan JSON is more accurate because it reflects the actual values Terraform has resolved (including variables, data sources, and module outputs) rather than raw HCL that may contain unresolved references.

Step 2 — Identify: It scans the parsed configuration for "billable resources" — Terraform resource types that map to cloud services with associated costs. For example, aws_instance maps to EC2, aws_db_instance maps to RDS, and aws_nat_gateway maps to NAT Gateway charges.

Step 3 — Lookup: For each identified resource, Infracost queries its pricing database (backed by cloud provider APIs) using the resource's attributes. For an aws_instance with instance_type = "t3.large" in us-east-1, it retrieves the current On-Demand Linux price for that combination.

Step 4 — Calculate: It multiplies the unit price by usage parameters. Some parameters are deterministic from the code (e.g., storage size is declared in the Terraform config). Others require assumptions (e.g., how many hours per month will this instance run? Infracost defaults to 730 hours — a full month). Usage-based costs like data transfer can be specified in a infracost-usage.yml file.

Step 5 — Report: It outputs a detailed cost breakdown showing per-resource costs, sub-component costs (e.g., instance hours vs. EBS storage), and a total monthly estimate. In CI/CD mode, this becomes a Pull Request comment.

Running a breakdown against a Terraform directory produces output like this (abbreviated):

Name                                     Monthly Qty  Unit         Monthly Cost

aws_instance.web_server
├─ Instance usage (Linux/UNIX, on-demand)        730  hours             $60.74
└─ root_block_device
   └─ Storage (general purpose SSD, gp3)          50  GB                 $4.00

aws_db_instance.postgres
├─ Database instance (db.t3.medium)              730  hours             $52.56
└─ Storage (general purpose SSD, gp2)            20  GB                 $2.30

OVERALL TOTAL                                                          $119.60

The infracost-usage.yml File: Handling Unknown Quantities

Some costs cannot be determined from Terraform code alone because they depend on runtime behavior — how much data will flow through the NAT Gateway? How many Lambda invocations will occur? Infracost handles this with a usage file naming each resource and the estimated usage quantities it can't infer from the code alone — data processed through a NAT gateway, monthly invocation count and average duration for a Lambda function, and so on. This file lives in your repository alongside your Terraform code and should be updated as your understanding of actual usage improves. It transforms Infracost from a "compute-only" estimator into a full cost model for your infrastructure.

This is one of 18 chapters

Every chapter is free — Kubernetes, Terraform, SRE, distributed systems, and more — with fast daily review built in.

Sign up free