Skip to main content

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.

✓ Terraform integration ✓ GitHub Actions PR comments ✓ Cost diff on every PR ✓ Cost policy enforcement

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
AWSEC2, RDS, EKS, S3, ElastiCacheLambda, DynamoDB, data transfer
GCPCompute Engine, Cloud SQL, GKECloud Functions, BigQuery
AzureVMs, SQL Database, AKSFunctions, 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.

Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$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.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

MIT licensed · Instant download · No subscription

See what's included → Browse all deals →