Webhook icon
WorkingDirectory icon
Clone icon
Commands icon
Docker icon
Script icon
Process icon
If icon
SlackIncomingWebhook icon
Fail icon
Log icon

Terraform Cost Budget Gate with Infracost

Gate Terraform PRs on cloud cost — run Infracost for a monthly estimate, compare against your budget, and alert Slack and fail the build when over.

Categories
CloudInfrastructure

Cloud cost surprises are cheapest when they are still a pull request. This blueprint turns the monthly cost of a Terraform change into a CI signal: clone the IaC repository, run Infracost against the HCL — no terraform init, no cloud credentials, no apply — and compare the estimated monthly total with a budget input. Over the ceiling, Slack gets the estimate, the budget, and the percentage, and the execution fails so the CI check on the open pull request turns red. Under it, a quiet log keeps a cost trend in the execution history. It closes the gap between "the plan looks fine" and "the plan looks fine, and costs what we agreed".

How it works

  1. The on_iac_push trigger (io.kestra.plugin.core.trigger.Webhook) exposes a keyed URL for your CI to POST to on push or pull-request events.
  2. workspace (io.kestra.plugin.core.flow.WorkingDirectory) opens one shared filesystem for the checkout, the estimate, and the report parser.
  3. clone_repository (io.kestra.plugin.git.Clone) checks out repository_url at branch; public repositories need no credentials, private ones take a username and token on the task.
  4. run_cost_estimate (io.kestra.plugin.scripts.shell.Commands on the io.kestra.plugin.scripts.runner.docker.Docker runner, image infracost/infracost:latest) runs infracost breakdown --path <iac_path> --format json into infracost-report.json, authenticated with the INFRACOST_API_KEY secret. Infracost parses the HCL directly, so the estimate never touches a cloud account.
  5. parse_estimate (io.kestra.plugin.scripts.python.Script on the Process runner) extracts the total monthly cost tolerantly (wrapped breakdown, top-level, or summed projects), computes pct_of_budget, and raises if the report is unreadable — a broken gate must never look like a cheap change.
  6. check_budget (io.kestra.plugin.core.flow.If) branches on over_budget. Over: alert_over_budget posts the numbers to Slack and fail_the_build (io.kestra.plugin.core.execution.Fail) turns the CI check red. Under: log_within_budget records the pass.
  7. The errors block alerts Slack when the gate itself fails, naming the three things that usually break: API key, repository URL, path input.

What you get

  • Cost reviewed at PR time, when the change is still cheap to fix — not at invoice time.
  • A budget that lives in an input, adjustable per repository or environment without editing the flow.
  • No cloud credentials required for the estimate: Infracost reads HCL, not accounts.
  • A budget_check JSON output (estimate, budget, percentage, verdict) for dashboards and trend reporting.
  • A red CI check on over-budget pull requests, not just a message in a channel.

Who it's for

  • Platform and infrastructure teams enforcing cloud budgets on infrastructure-as-code.
  • FinOps engineers who want cost review inside the pull-request workflow.
  • Engineering leads who have approved a "harmless" Terraform change that quietly doubled a bill.

Why orchestrate this with Kestra

Infracost estimates cost but does not watch repositories, remember budgets, or fail builds. Kestra provides the loop around it: a webhook trigger from CI, a hermetic container for the tool, a branch that separates the gate verdict from the evidence, alert routing to Slack, and an execution history that doubles as a cost trend. The same logic in a CI script is a YAML blob nobody reviews, with no alerting when the script itself breaks.

Prerequisites

  • A free Infracost API key from app.infracost.io — the estimate runs on Infracost's cloud registry of prices.
  • A worker with Docker access (the estimate runs in the Infracost container) and Python 3 on the host for the parser, or switch parse_estimate to a Docker task runner.
  • A Slack incoming webhook for over-budget and failure alerts.
  • A CI system that can POST to the Kestra webhook and honor the execution's exit state as a check.

Secrets

  • INFRACOST_API_KEY: Infracost API key used by the estimate container.
  • COST_GATE_WEBHOOK_KEY: key segment guarding the webhook URL — treat it as a secret.
  • SLACK_WEBHOOK_URL: Slack incoming webhook URL.

Quick start

  1. Add the three secrets to your Kestra namespace.
  2. Set repository_url, branch, and — for monorepos — iac_path to the Terraform root module you want estimated.
  3. Run the flow once manually and read budget_check from the outputs to see your estimate, then set budget_usd to the real ceiling.
  4. Point your CI (GitHub Actions on: pull_request, GitLab pipeline, etc.) at /api/v1/{tenant}/executions/webhook/{namespace}/terraform-infracost-budget-gate/{COST_GATE_WEBHOOK_KEY} and treat a failed execution as a failed check.
  5. To test the gate end to end, point iac_path at a root module you know is over budget and confirm the Slack alert and the failed execution.

How to extend

  • Private repositories: add username and password: "{{ secret('GIT_TOKEN') }}" to clone_repository.
  • Advisory mode: delete fail_the_build (or move it behind another If on a hard_gate input) to alert without failing.
  • PR diff costs: compare against the default branch with infracost diff --path . --compare-to <base> and gate on the delta instead of the absolute total.
  • Multiple environments: fan out with Parallel or Loop over a list of iac_path values and aggregate the totals.
  • Scheduled trend: add a Schedule trigger alongside the webhook to estimate nightly regardless of pushes.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.