DEV Community

Getting Started with Infracost: Estimating Cloud Costs

Getting Started with Infracost: Estimating Cloud Costs in Terraform Projects

Cloud cost surprises are no fun, especially when they show up after deployment. If using Terraform to manage AWS infrastructure, there's a powerful open-source tool that helps bring cost visibility into the development workflow: meet Infracost.

What is Infracost?

Infracost estimates cloud costs directly from the Terraform code, even before you apply it. It supports major cloud providers and services, and integrates easily with GitHub, GitLab, Bitbucket, and CI tools.

In short: see the financial impact of changes before they hit the cloud bill.

Prerequisites

  • Terraform
  • Infracost CLI
  • Terraform AWS project (EC2, S3, RDS, etc.)
  • AWS credentials configured

Step 1: Install Infracost

Run the following command based on OS:

curl -s https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh | sh
Enter fullscreen mode Exit fullscreen mode

Verify the installation:

infracost --version
Enter fullscreen mode Exit fullscreen mode

Infracost version
fig: Infracost version

Step 2: Generate API Key for Infracost

Infracost requires an API key to fetch accurate, real-time cloud pricing data and to post cost estimates in pull requests.

1. Generate an API Key

2. Use the API Key in GitHub Actions

  • Navigate to Settings → Secrets and variables → Actions
  • Click New repository secret
  • Name the secret INFRACOST_API_KEY
  • Paste the API key

Reference in GitHub workflow:

env:
  INFRACOST_API_KEY: ${{ secrets.INFRACOST_API_KEY }}
Enter fullscreen mode Exit fullscreen mode

3. Authenticate Locally for Development

Option A: Interactive Login

infracost auth login
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Option B: Set API Key Manually

infracost configure set api_key <YOUR_API_KEY>
Enter fullscreen mode Exit fullscreen mode

Replace <YOUR_API_KEY> with the value copied from the Infracost dashboard.

Once set, the key is stored locally and used for all Infracost CLI commands, including breakdown and diff.

Step 3: Initialize Terraform Project

cd ~/my-terraform-project
terraform init
Enter fullscreen mode Exit fullscreen mode

Step 4: Generate a Cost Estimate

infracost breakdown --path . --format table
Enter fullscreen mode Exit fullscreen mode

This generates a table with estimated monthly costs for each resource:

Image description

Can also use --format json for CI/CD integrations.

Step 5: Compare Cost Changes (Pull Request Workflow)

One of Infracost's most powerful features is the ability to compare infrastructure cost before and after code changes — a workflow that's especially useful in pull requests (PRs).

What it does:

  • Reads Terraform code before changes
  • Calculates baseline cost
  • Saves to infracost-base.json
  • Compares with updated code
  • Generates a diff report

Image description

Image description

Before the Change – Create a Baseline
This command should be run before making any infrastructure edits, typically from the main or dev branch:

infracost breakdown --path . --format json --out-file infracost-base.json

What it does:

  • Scans the current Terraform state
  • Calculates the monthly cost estimate
  • Saves the cost structure to infracost-base.json (baseline)

This baseline will be compared against the new code later.

Image description

Make Infrastructure Changes

Modify the Terraform code, for example:

  • Add EC2 instance
  • Resize RDS instance
  • Modify EBS volume

After the Change,run the Diff

infracost diff --path . --compare-to infracost-base.json

What it does:

  • Reads the new version of the code
  • Compares it against infracost-base.json
  • Outputs a human-readable diff showing:
  1. What changed
  2. New monthly cost estimate
  3. Increase or decrease in cost
  4. Which specific resources were affected

Image description

Why This Matters in a PR Workflow

In a team environment using GitHub/GitLab:

  • This infracost diff step can run in the CI/CD pipeline
  • It adds a comment to the pull request, like: Cost estimate change: +$40/month
  • aws_instance.app added
  • aws_db_instance upgraded from db.t3.medium to db.t3.large

This gives reviewers visibility into not just what changed, but how much it will cost.

Step 6: Integrate with GitHub Pull Requests

Infracost can post cost diff comments automatically to GitHub PRs.

Example workflow snippet:

name: Infracost Cost Estimation

on:
  pull_request:
    branches:
      - dev
      - stage
      - main
    paths:
      - '*.tfvars'
  push:
    branches:
      - dev
      - stage
      - main
    paths:
      - '*.tfvars'
permissions:
  contents: read
  pull-requests: write

env:
  INFRACOST_API_KEY: ${{ secrets.INFRACOST_API_KEY }}

jobs:
  infracost:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup Infracost
        uses: infracost/actions/setup@v3
        with:
          api-key: ${{ secrets.INFRACOST_API_KEY }}

      - name: Generate Infracost Baseline
        run: |
          git fetch origin ${{ github.base_ref }}
          git checkout origin/${{ github.base_ref }} -- .
          infracost breakdown \
            --path=. \
            --terraform-var-file=${{ <file_path> }} \
            --format=json \
            --out-file=infracost-baseline.json

      - name: Generate Infracost Diff
        run: |
          git checkout ${{ github.head_ref }}
          infracost diff \
            --path=. \
            --terraform-var-file=${{ <file_path> }} \
            --compare-to=infracost-baseline.json \
            --format=json \
            --out-file=infracost-diff.json

      - name: Upload Infracost diff to Cloud Dashboard
        run: |
          infracost upload --path=infracost-diff.json

      - name: Post Infracost Comment to PR
        if: github.event_name == 'pull_request'
        run: |
          infracost comment github \
            --path=infracost-diff.json \
            --repo=${{ github.repository }} \
            --pull-request=${{ github.event.pull_request.number }} \
            --github-token=${{ secrets.GITHUB_TOKEN }} \
            --behavior=update

      - name: Show Human-Readable Cost Breakdown in Logs
        run: |
          infracost breakdown \
            --path=. \
            --terraform-var-file=${{ <file_path> }} \
            --format=table

      - name: Upload Infracost Table Breakdown
        run: |
          infracost breakdown \
            --path=. \
            --terraform-var-file=${{ <file_path> }} \
            --format=table \
            --out-file=infracost-breakdown.txt
        continue-on-error: true

      - name: Upload breakdown as artifact
        uses: actions/upload-artifact@v4
        with:
          name: infracost-breakdown
          path: infracost-breakdown.txt
Enter fullscreen mode Exit fullscreen mode

Image description

Why Use Infracost?

  • No more budget surprises
  • Empower developers with cost insights
  • Enable cost-aware reviews
  • Catch expensive changes before deploy

Final Thoughts

Infracost is a simple, open-source tool that helps teams understand cloud cost impacts before provisioning infrastructure.

Resources

Top comments (0)