Skip to main content

Infracost

Infracost is an open-source tool that estimates the monthly cloud cost of a Terraform plan before you apply it — showing a cost breakdown per resource and the cost difference between your current and proposed infrastructure.

What Is Infracost?

Infracost is a command-line tool that reads your Terraform plan and tells you, in real currency, how much the infrastructure you are about to create will cost per month — before you ever run apply. It works by parsing the resources in your plan and matching them against live cloud provider pricing.

Imagine being able to see the total bill on a receipt before you actually buy anything, instead of finding out at the end of the month when the credit card statement arrives. That is exactly the gap Infracost closes for infrastructure changes.

At Meesho, an engineer once submitted a pull request to "right-size" a Kafka cluster for better throughput — changing the instance type from r6g.xlarge to r6g.4xlarge across nine brokers. The Terraform diff looked like a one-line change. Infracost's comment on the PR showed it would add roughly four lakh rupees a month to the AWS bill. The PR was revised to scale only the brokers that actually needed it, catching the cost impact before a single dollar was spent — not after the next invoice.

How Infracost Works

Bash
+------------------------------------------+
| terraform plan -out=plan.tfplan |
+------------------------------------------+
|
v
+------------------------------------------+
| infracost breakdown --path=plan.tfplan |
| reads every resource in the plan |
+------------------------------------------+
|
v
+------------------------------------------+
| Queries Infracost's pricing API (built |
| from AWS/GCP/Azure public pricing data) |
+------------------------------------------+
|
v
+------------------------------------------+
| Prints a per-resource monthly cost table |
| and a total estimated monthly cost |
+------------------------------------------+

infracost breakdown — Total Cost Estimate

Bash
# Generate a Terraform plan file first
terraform plan -out=plan.tfplan
# Ask Infracost to break down the cost of that exact plan
infracost breakdown --path=plan.tfplan
# Example output:
# NAME MONTHLY QTY UNIT MONTHLY COST
# aws_instance.app[0]
# ├─ Instance usage (Linux/UNIX, on-demand, t3.large)
# │ 730 hours $60.74
# aws_db_instance.main
# ├─ Database instance (on-demand, db.r6g.large)
# │ 730 hours $233.60
#
# OVERALL TOTAL $294.34

infracost diff — Cost Difference for a PR

This is the command most teams actually run in CI — it shows only what is CHANGING, not the entire infrastructure's cost:

Bash
infracost diff --path=plan.tfplan --compare-to=infracost-base.json
# Example output:
# Project: payments-infra
# ~ aws_db_instance.main
# +$155.20 ($78.40 -> $233.60) <-- this line is what gets posted on the PR
#
# Monthly cost change: +$155.20 (+198%)

GitHub Actions Integration

YAML
# .github/workflows/infracost.yml
- name: Setup Infracost
uses: infracost/actions/setup@v3
with:
api-key: ${{ secrets.INFRACOST_API_KEY }}
- name: Generate Infracost diff
run: |
infracost diff --path=. \
--format=json \
--out-file=infracost-diff.json
- name: Post Infracost comment
uses: infracost/actions/comment@v3
with:
path: infracost-diff.json
behavior: update # updates the same comment instead of spamming a new one per push

Supported Resources and Limitations

Note

Tip from a senior engineer: Infracost covers the most common billable resources extremely well — EC2, RDS, S3, Lambda, EKS, and their GCP/Azure equivalents. It does NOT account for usage-based costs that depend on runtime behavior, like actual S3 data transfer volume or Lambda invocation counts — those numbers don't exist until the resource is actually used. Treat Infracost's number as the cost of the infrastructure shape, not a guarantee of the final bill.

Cost Policies

Some teams set a threshold and fail the CI pipeline if a PR crosses it:

Bash
# Fails the pipeline if monthly cost increases by more than $500
infracost diff --path=plan.tfplan | infracost comment --policy-fail-on-percentage=20

Quick Reference

Command What It Does
infracost breakdown --path=<plan> Shows total estimated monthly cost
infracost diff --path=<plan> --compare-to=<base> Shows cost CHANGE vs a baseline
infracost comment Posts the cost diff as a PR/MR comment
Error Root Cause Fix
No resources found in plan Plan file path wrong, or plan has zero changes Verify the --path points at the actual .tfplan or plan JSON file
Cost shows as $0.00 for a known resource Resource type not yet supported in Infracost's pricing catalog Check Infracost's supported resources list, fall back to manual estimate
Invalid API key Missing or expired INFRACOST_API_KEY Regenerate the key from the Infracost dashboard and update the CI secret
Diff comment not appearing on PR CI token lacks PR comment permission Check that the workflow's GitHub token has pull-requests: write permission

Frequently Asked Questions

How does Infracost calculate a cost estimate without actually deploying the infrastructure?

Infracost parses the output of `terraform plan` (specifically the plan JSON) to see exactly which resources will be created, modified, or destroyed and with what configuration, then maps each resource type and its attributes against AWS/Azure/GCP public pricing APIs to compute an hourly and monthly estimate. Because it reads the plan rather than live state, it can price a change before you ever run `terraform apply`.

Where does Infracost typically get wired into a workflow, and what does it miss?

Most teams run it as a CI check on pull requests, posting a cost diff comment so reviewers see the dollar impact of a Terraform change alongside the code diff, catching an oversized instance type or an accidentally duplicated resource before merge. It can't account for usage-based costs that depend on runtime behavior — data transfer volume, Lambda invocation counts, S3 request counts — since those aren't knowable from a static plan; it estimates the pricing structure, not variable usage.