How do you integrate Infracost with your Terraform CI/CD pipeline to provide cost estimation and implement cost guardrails that prevent deployments exceeding budget thresholds?
Quick Answer
Infracost analyzes Terraform plan JSON to estimate monthly costs. Integrate in CI/CD by running 'infracost breakdown' on plan output, use 'infracost diff' for PR comments showing cost changes, and implement policy checks with 'infracost output --format json' piped to threshold validation scripts.
Detailed Answer
Infracost Architecture
Infracost parses Terraform plan JSON files and matches resources to its pricing database (covering AWS, Azure, GCP). It calculates estimated monthly costs based on resource types, sizes, regions, and usage patterns. It supports both flat estimation (what will this cost?) and diff estimation (how much more/less will this change cost?).
CI/CD Integration (GitHub Actions)
1. Run terraform plan -out=plan.tfplan in the PR pipeline 2. Run infracost breakdown --path plan.tfplan for full cost breakdown 3. Run infracost diff --path plan.tfplan --compare-to infracost-base.json to show cost delta 4. Post results as PR comment with infracost comment github 5. Implement guardrails in the pipeline script
Cost Guardrails
- Soft guardrail: Comment on PR with cost impact, require manual approval for changes > $500/month - Hard guardrail: Block merge if estimated monthly cost increase > $5000 without VP approval tag - Per-resource guardrails: Alert if any single resource costs > $1000/month (catches accidental large instance types)
Usage-Based Estimation
Infracost supports usage files for resources with variable costs (data transfer, API calls, storage growth). Create infracost-usage.yml with expected usage patterns for more accurate estimates.
FinOps Integration
Export Infracost data to your FinOps platform. Track cost trends over time per team/project. Compare Infracost estimates against actual AWS Cost Explorer data to calibrate accuracy. Typical accuracy is within 15-20% for compute-heavy workloads.
Code Example
# GitHub Actions workflow with Infracost
name: Terraform Cost Check
on: [pull_request]
jobs:
infracost:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: infracost/actions/setup@v3
with:
api-key: ${{ secrets.INFRACOST_API_KEY }}
# Generate baseline cost from main branch
- name: Generate Infracost baseline
run: |
git checkout main
terraform init
infracost breakdown --path . --format json --out-file /tmp/infracost-base.json
git checkout ${{ github.head_ref }}
# Generate cost diff for PR
- name: Generate Infracost diff
run: |
terraform init
infracost diff --path . \
--compare-to /tmp/infracost-base.json \
--format json \
--out-file /tmp/infracost-diff.json
# Post PR comment
- name: Post Infracost comment
run: |
infracost comment github \
--path /tmp/infracost-diff.json \
--repo ${{ github.repository }} \
--pull-request ${{ github.event.pull_request.number }} \
--github-token ${{ secrets.GITHUB_TOKEN }} \
--behavior update
# Cost guardrail - block if too expensive
- name: Check cost threshold
run: |
DIFF=$(jq '.diffTotalMonthlyCost | tonumber' /tmp/infracost-diff.json)
if (( $(echo "$DIFF > 5000" | bc -l) )); then
echo "::error::Cost increase of \$$DIFF/month exceeds \$5000 threshold"
exit 1
fi
# infracost-usage.yml for usage-based estimation
version: 0.1
resource_usage:
aws_s3_bucket.data:
standard:
storage_gb: 5000
monthly_tier_1_requests: 1000000 # PUT/POST/LIST
monthly_tier_2_requests: 10000000 # GET/SELECT
glacier:
storage_gb: 50000
aws_lambda_function.processor:
monthly_requests: 5000000
request_duration_ms: 200Interview Tip
Cost management in IaC is increasingly important in interviews, especially at cost-conscious companies. Know the difference between breakdown (absolute cost) and diff (cost change). Mention usage-based estimation for resources with variable costs (Lambda, S3, data transfer). The guardrail pattern (soft vs hard thresholds) shows organizational maturity.