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
Verify the installation:
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
- Visit https://dashboard.infracost.io/
- Sign up or log in
- Generate an INFRACOST_API_KEY from the dashboard
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 }}
3. Authenticate Locally for Development
Option A: Interactive Login
infracost auth login
Option B: Set API Key Manually
infracost configure set api_key <YOUR_API_KEY>
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
Step 4: Generate a Cost Estimate
infracost breakdown --path . --format table
This generates a table with estimated monthly costs for each resource:
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
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.
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:
- What changed
- New monthly cost estimate
- Increase or decrease in cost
- Which specific resources were affected
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.
- Set up GitHub Actions or another CI system
- Use the https://github.com/infracost/actions
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
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.
Top comments (0)