Why did a team's Infracost job post a cost comment and stay green while a pull request added $4,000 a month, and what would make that cost block the merge?
answer
- an increase is not an error
- what the exit code really reports
- a threshold needs a place to live
- the required check has its own failures
basics
~20 sInfracost treats a cost increase as information: scan and diff exit non-zero only on errors such as critical diagnostics. Blocking needs a guardrail set to block the pull request, or your own script testing the JSON, plus a required check.
solid answer
~40 sA comment is reporting, and nothing in it fails the job. `infracost diff` exits zero on any increase, since a bigger number is not an error, and `infracost scan` exits non-zero only for failures such as a critical diagnostic. Even a triggered guardrail only appears in `guardrail_results` without changing the scan's exit code. To block, define a guardrail in Infracost Cloud with a total, increase or percentage threshold and the Block PR action, and check how your pull-request integration reports it. Alternatively, test `diff_total_monthly_cost` from `infracost diff --json` in your own job. Either way the VCS must require that check. Then weigh what you have built: a required check also fails on a parse error, an outage or an exhausted run limit, none of which is a cost problem.
code
bash · 6 lines# Price the base checkout, then compare the head checkout with it
infracost scan base --json > infracost-base.json
infracost diff head --compare-to infracost-base.json --json > cost-diff.json
# Exit 1 when the net monthly change reaches 500; the diff itself exits 0
jq -e '(.diff_total_monthly_cost | tonumber) < 500' cost-diff.jsongo deeper
Recall that the Infracost comment reports a cost change and does not stop anything by itself. Know that setting up the comment goes through infracost ci setup.
Explain what scan and diff exit codes mean, and the difference between a guardrail's total and increase thresholds. Say why a plain scan evaluates only a total.
Build a block end to end, from threshold to required check, and list what else now fails that check: parse errors, run limits and outages. Decide how errors should behave before enabling it.
Treat the required check as a dependency on a vendor and an estimate in your merge path. Weigh that against the size of the risk, and ask who gets paged when it blocks a hotfix.
## How the comment gets there `infracost ci setup` connects a repository in one of two ways: - **App integration**, the recommended route. It works with hosted GitHub and GitLab among others, with no YAML or secrets to manage, and comments on every pull request once connected. - **`infracost ci setup --pipeline`**, which writes CI config. A VCS host with no app integration, such as a self-hosted GitHub, falls back to this. The generated pipeline has two jobs, running from the image `ghcr.io/infracost/ci:0.1`: - On a pull request, it checks out base and head into two directories and runs `infracost-ci diff --base-path base --head-path head`. - On the default branch, it runs `infracost-ci scan --path .`. The jobs carry an Infracost token and, for the comment, a VCS token. They hold no cloud credentials. `infracost-ci` is a separate program from the `infracost` CLI. ## What an exit code reports The green job is correct behaviour once you know what the exit code means on the CLI: | Result | `infracost scan` | `infracost diff` | |---|---|---| | Cost rose, however far | Not compared | Exit 0 | | Guardrail triggered, or over budget | Exit 0, reported in the JSON | Not evaluated | | A policy fails | Exit 0, reported in the JSON | Not reported | | Critical diagnostic | Output rendered, then non-zero | Stops before rendering, non-zero | | Login, plugin or run-limit failure | Non-zero | Non-zero | So a green Infracost check shows that the tool ran. It does not show that anybody accepted the cost. ## The settings that can act on cost The CLI lists two kinds of limit, both configured in Infracost Cloud: | | Guardrail | Budget | |---|---|---| | Applies to | A repository, or projects through `project_filter` | The organization: all resources, or those with given tags | | Tests | `total_threshold`, `increase_threshold` or `increase_percent_threshold` | An amount for a period, against current cost | | Actions in the CLI's record | PR comment, Block PR, or alert only | PR comment, or alert only | | List with | `infracost guardrails [path]` | `infracost budgets` | One detail catches people. A plain `infracost scan` evaluates only guardrails with a **total** threshold, since a single scan has no base side to compute an increase from. And `infracost diff` output carries no guardrail results at all. Both records can carry text for the reader, `message` on a guardrail and `custom_overrun_message` on a budget. Use it to say who approves an exception, because a limit that names no owner tends to end in an argument on the pull request. ## Routes to a block 1. **A guardrail with Block PR.** This is the product's own route. The CLI records the action but does not enforce it through an exit code, so the block has to come from your pull-request integration. Test it on a throwaway pull request and see which check it raises before relying on it. 2. **Your own threshold in your own job.** Compare `diff_total_monthly_cost` from `infracost diff --json` with a limit, as in the example below. You keep the logic in your repository, and you also own its maintenance. 3. **Make the check required.** Neither route stops a merge until the VCS requires that check. ## What a required check brings with it A required check fails for every reason its job can fail, and most are not about cost: - **A parse error** in one project stops the job, even if the change is cheap. - **An exhausted run limit** ends the scan with an error, since an organization can exceed its monthly run limit. - **A dependency outage** in the pricing API or dashboard fails the job, unless you have decided the job fails open. - **A renamed job.** Checks are named after the job, and the CLI's own source warns that a check that disappears blocks every merge. The gate can also pass when it should not: - **Usage-driven cost** with no quantity adds zero, so the design from a surprise bill passes a dollar threshold. - **Offsetting changes** can net to nothing while large resources move. Decide in advance what happens when the job errors, and give people a documented override. Whether to gate at all is a separate decision, and most teams should start with the comment.
- Does `infracost diff` tell you whether a guardrail triggered?No. Its output holds the currency, per-project and per-resource changes, totals and usage flags, with no guardrail results. A plain `infracost scan --json` does report `guardrail_results` and `summary.triggered_guardrails`, but only for guardrails with a total threshold, because one scan has no base side for an increase.
- Should a required Infracost check fail open or fail closed when the job itself errors?Usually open for tool errors and closed only for a real cost decision. Otherwise a pricing API outage or an exhausted run limit stops every merge, including a hotfix, over something unrelated to the change. Failing open needs an alert so that a broken job does not stay silently broken, and the choice should be made in advance.
- What does an Infracost budget do that a guardrail does not?A budget sets an amount for a period across the organization, for all resources or those with given tags, so it can follow spending that crosses repositories. A guardrail is scoped to a repository or to projects and tests a total or an increase. In the CLI's record a budget can comment or alert, while only a guardrail lists Block PR.
- infracost diff exits non-zero when the monthly cost rises past a large amount.
- A triggered guardrail makes infracost scan exit non-zero in the job.
- A budget can block a pull request in the same way as a guardrail.
- A green Infracost check means the change's cost was reviewed and accepted.
- Once the cost comment appears on a pull request, the merge already depends on it.