Infracost Cloud Cost Estimation
Show estimated monthly cloud costs in every Terraform pull request before merging. Catch expensive misconfigurations early, compare configuration options by cost, and enforce cost policies in CI.
Installation
# macOS brew install infracost # Linux / CI curl -fsSL https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh | sh # Authenticate (get free API key at infracost.io) infracost auth login # Verify installation infracost --version
Basic Terraform Integration
# Run a cost breakdown on your Terraform directory infracost breakdown --path ./terraform # Generate a plan file and use it (more accurate) cd terraform terraform init terraform plan -out tfplan.binary terraform show -json tfplan.binary > tfplan.json infracost breakdown --path tfplan.json --format table # Compare two branches infracost diff --path . \ --compare-to infracost-base.json \ --format diff
Usage File for Variable Costs
# infracost-usage.yml — provide usage estimates for pay-per-use resources
version: 0.1
resource_usage:
aws_lambda_function.api_handler:
monthly_requests: 5000000 # 5M requests/month
request_duration_ms: 150 # average 150ms execution
memory_mb: 512
aws_dynamodb_table.events:
monthly_read_request_units: 1000000
monthly_write_request_units: 100000
storage_gb: 50
aws_s3_bucket.assets:
storage_gb: 500
monthly_get_requests: 2000000
monthly_put_requests: 50000
# Run with usage file
infracost breakdown --path . --usage-file infracost-usage.yml GitHub Actions Workflow
# .github/workflows/infracost.yml
name: Infracost Cost Estimate
on:
pull_request:
paths:
- 'terraform/**'
jobs:
infracost:
name: Run Infracost
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout base branch
uses: actions/checkout@v4
with:
ref: '${{ github.event.pull_request.base.ref }}'
- name: Setup Infracost
uses: infracost/actions/setup@v3
with:
api-key: ${{ secrets.INFRACOST_API_KEY }}
- name: Generate Infracost cost estimate for base branch
run: |
infracost breakdown \
--path=./terraform \
--format=json \
--out-file=/tmp/infracost-base.json
- name: Checkout PR branch
uses: actions/checkout@v4
- name: Generate Infracost cost estimate for PR branch
run: |
infracost breakdown \
--path=./terraform \
--format=json \
--out-file=/tmp/infracost-pr.json
- name: Post Infracost comment on PR
uses: infracost/actions/comment@v1
with:
path: /tmp/infracost-pr.json
compare-to: /tmp/infracost-base.json
behavior: update # update existing comment on re-run Cost Policy — Block PRs Over Threshold
# Add this step after the PR comment step
- name: Check cost increase threshold
run: |
DIFF=$(infracost diff \
--path /tmp/infracost-pr.json \
--compare-to /tmp/infracost-base.json \
--format json | jq '.diffTotalMonthlyCost | tonumber')
echo "Monthly cost increase: $$DIFF"
if (( $(echo "$DIFF > 500" | bc -l) )); then
echo "ERROR: Cost increase ${DIFF}/month exceeds $500 threshold."
echo "Please get finance approval before merging."
exit 1
fi What Infracost Supports
| Cloud | Well Supported | Partial / Usage-based |
|---|---|---|
| AWS | EC2, RDS, EKS, S3, ElastiCache | Lambda, DynamoDB, data transfer |
| GCP | Compute Engine, Cloud SQL, GKE | Cloud Functions, BigQuery |
| Azure | VMs, SQL Database, AKS | Functions, Cosmos DB |
Frequently Asked Questions
What is Infracost and how does it work with Terraform?▼
Infracost is an open-source tool that parses Terraform plan files and maps resource configurations to cloud pricing APIs to estimate monthly costs before you apply changes. It reads your .tf files or a terraform plan JSON output, looks up pricing for each resource type (EC2 instance size, RDS instance class, S3 storage tier, etc.), and produces a cost breakdown. The key value is seeing cost impact in pull requests — a reviewer can see 'this change adds $340/month' before merging. Infracost supports AWS, GCP, and Azure.
How accurate are Infracost estimates?▼
Infracost estimates are directionally accurate for compute and storage costs but have limitations. It doesn't account for data transfer costs (which can be significant), API call volumes for Lambda/DynamoDB, or actual utilization. Estimates assume 730 hours/month (full month) for all resources. For resources billed per-request or by usage (Lambda invocations, DynamoDB read units), Infracost uses usage estimates you provide in a usage file. Treat estimates as a floor — actual costs will be higher once you include data transfer and usage-based billing. They're most valuable for comparing two configurations, not predicting exact bills.
Can I block a PR if costs exceed a threshold?▼
Yes. Infracost supports a policy-as-code approach using the --policy-output flag and OPA (Open Policy Agent) or a built-in threshold check. In GitHub Actions, you can use the infracost/actions/comment action and add a step that reads the total monthly cost from the JSON output and fails the workflow if it exceeds a threshold. For example: if the diff shows an increase of more than $500/month, fail the PR and require finance approval. This creates a cost gate alongside your security and quality gates.
Does Infracost support Terragrunt and Terraform modules?▼
Yes. For Terragrunt, use infracost breakdown --path . with the INFRACOST_TERRAFORM_BINARY=terragrunt environment variable set, or use the --terraform-init-flags to pass required variables. For modules, Infracost recursively processes module calls and inlines the cost of module resources into the parent module's estimate. If a module uses a for_each or count to create multiple resources, Infracost evaluates the full count. Remote module sources (GitHub, Terraform Registry) are fetched during the breakdown run.
Related Products
on GumroadPremium Workflow Pack
Production-grade workflows, architecture guides, and deployment scripts for teams that ship. 10 advanced CI/CD workflows (blue-green, canary, E2E, security), 5 architecture deep-dives, code review checklist, PR process guide, and 5 battle-tested Bash deployment scripts.
Buy on Gumroad →GitHub Actions CI/CD Templates Pack
12 production-ready GitHub Actions workflow templates covering Node.js, Python, Docker, multi-environment deployments, security scanning, and auto-changelog. Copy, paste, ship.
Buy on Gumroad →Developer Productivity Bundle
Stop rebuilding the same setup every project. 51 VSCode snippets, 40 Git aliases, 5 Docker Compose stacks, 5 GitHub Actions workflows, 5 README templates, and a dotfiles kit. Set up a professional dev environment in under an hour.
Buy on Gumroad →You Might Also Need
Terraform Variable Builder
Build Terraform variable declarations for variables.tf with a visual form. Set type, default value, description, sensitivity, and validation rules.
Open free tool →
Terraform Variable Validator
Validate your Terraform variables.tf online. Catches missing type constraints, undocumented variables, unmasked secrets, and type/default mismatches. Runs 100% in your browser.
Open free tool →
Terraform Variable Generator
Generate variables.tf and terraform.tfvars from a visual form. Add variables with type, description, defaults, sensitive flag, and validation blocks.
Open free tool →
More Free Tools
Related Articles
FinOps Engineering: A Developer's Guide to Cloud Cost Optimization in 2026
A practical guide to FinOps for engineers: AWS, GCP, and Azure cost tools, Infracost for Terraform, right-sizing, spot instances, reserved capacity, auto-scaling, and the FinOps maturity model. With code examples for CI/CD cost gates.
Terraform vs Pulumi 2026: Infrastructure as Code Comparison with Code Examples
Complete Terraform vs Pulumi comparison for 2026 — syntax, state management, multi-cloud support, testing, pricing, and when to choose each IaC tool.
AWS CDK vs Terraform vs Pulumi: Infrastructure as Code Comparison 2026
Complete comparison of AWS CDK, Terraform, and Pulumi for infrastructure as code in 2026 — language support, state management, multi-cloud, community, and when to use each.
Pulumi vs Terraform 2026: Which IaC Tool Should You Choose?
A deep comparison of Pulumi and Terraform for infrastructure as code in 2026. Covers syntax, state management, provider ecosystem, team adoption, and when to choose each tool.
Terraform Infrastructure as Code: Complete 2024 Guide
Master Terraform for infrastructure as code. Learn HCL syntax, providers, state management, modules, and best practices to automate your cloud infrastructure.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
Unlock AI-Powered Dev Tools
- ⚡ AI Code Review, Doc Generator & SQL Builder
- ⚡ All premium templates & early access
- ⚡ Member discounts on Gumroad products
$9/mo after trial · Cancel anytime
Want the full toolkit?
Get DevPlaybook Pro
Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.
MIT licensed · Instant download · No subscription
See what's included → Browse all deals →