terraformpre-commitci-cdcost-estimation

A pre-commit hook for Terraform cost: catch expensive changes before they leave your laptop

Add a pre-commit hook that estimates Terraform cost on every commit and blocks over-budget changes. Runs in seconds, works offline, and pairs with a CI gate that can't be skipped.

The C3X Team··6 min read

Quick answer

Add a pre-commit hook that runs C3X over your Terraform on every commit — it prints the monthly cost and can block the commit when a budget is exceeded, the earliest possible point to catch an expensive change. It runs in seconds, works offline with a pricing snapshot, and pairs with a CI gate that can't be skipped with --no-verify.

The cheapest place to catch a costly infrastructure change is before it leaves your laptop. A pre-commit hook puts the cost estimate right at the moment you write the change — no pull request, no pipeline wait — so the new managed database or scaled node pool shows its price while you can still reconsider in the same keystroke.

The hook

Using the pre-commit framework, add a local hook that runs C3X:

# .pre-commit-config.yaml
repos:
  - repo: local
    hooks:
      - id: c3x-cost
        name: Estimate Terraform cost
        entry: c3x estimate --path . --budget 2000
        language: system
        pass_filenames: false
        files: \.tf$

On every commit that touches a .tf file, the hook estimates the monthly cost and aborts the commit if it exceeds the budget. Drop the --budget flag if you only want the number printed as feedback.

Hook plus CI, not hook instead of CI

A pre-commit hook is fast local feedback, but it can be bypassed with git commit --no-verify. So treat it as the early-warning layer and keep the authoritative gate in CI, where it can't be skipped — the pattern in budget guardrails in CI and GitLab CI cost estimation. The hook saves round-trips; CI enforces.

Keep it fast and offline

C3X runs a static estimate in seconds, and with an offline pricing snapshot the hook needs no network — so commits stay fast and work on a plane or in an air-gapped environment (see air-gapped estimation). For very large repos, scope the hook to changed directories so it only prices what you touched.

FAQ

Can I check Terraform cost before committing?

Yes. Add a pre-commit hook that runs a cost estimate over your Terraform whenever you commit, so you see the monthly cost (and can block commits over a budget) before the change ever reaches a pull request. It's the earliest possible point to catch an expensive change.

How do I add a cost-estimation pre-commit hook?

Use the pre-commit framework with a local hook that runs C3X against your Terraform directory. Configure it to print the estimate and optionally exit non-zero when the monthly cost exceeds a threshold, which aborts the commit until you acknowledge or fix it.

Isn't CI cost estimation enough?

CI is the enforcement backstop, but a pre-commit hook gives instant feedback at the moment you write the change — no waiting for a pipeline. Use both: the hook for fast local feedback, CI as the authoritative gate that can't be skipped with --no-verify.

Will a cost hook slow down my commits?

Barely. C3X is a small binary that runs a static estimate in seconds. For very large repos you can scope the hook to changed directories so it only estimates what you touched, keeping the commit fast.

Can the hook block expensive commits?

Yes. Run C3X with a budget threshold in the hook; if the estimated monthly cost exceeds it, the hook exits non-zero and the commit is aborted. Developers can override locally with --no-verify, which is why you also keep the gate in CI where it can't be bypassed.

Does this work offline?

Yes. C3X can use an offline pricing snapshot, so the pre-commit hook works without network access — useful on laptops, in air-gapped environments, or just to keep commits fast and dependency-free.

What to do next

Install C3X, add the hook to .pre-commit-config.yaml, and every commit gets a cost check. The quickstart gets you a local estimate first, and the CI/CD guide wires up the matching gate that runs server-side.

Try C3X on your own Terraform

Free and open source. No API key required. One command to install, one command to estimate.