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
+------------------------------------------+| 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
# Generate a Terraform plan file firstterraform plan -out=plan.tfplan # Ask Infracost to break down the cost of that exact planinfracost 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.34infracost 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:
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
# .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 pushSupported Resources and Limitations
INFORMATIONTip 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:
# Fails the pipeline if monthly cost increases by more than $500infracost diff --path=plan.tfplan | infracost comment --policy-fail-on-percentage=20Quick 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 |