skip to content

Cost Estimates with Infracost

Infracost reads infrastructure code and estimates its monthly cost, showing the change on the pull request. Interviewers ask what the estimate cannot see and how a team acts on it without a cost veto.

part ofTerraformread it →
on this pageshow

questions

6

With Infracost, what does `infracost scan` read and return, and why does it not need credentials for your cloud account?

level: juniorimportance: should knowfreq 28%

answer

  1. a calculation, nothing deployed
  2. code in, priced projects out
  3. two kinds of plugin
  4. whose token does it hold?

basics

~20 s

The infracost scan command reads infrastructure code in a directory, prices it through Infracost's pricing API and returns estimated monthly costs per project and resource. Prices come from that API, not your account, so cloud credentials are unnecessary.

solid answer

~50 s

`infracost scan [path]` takes a directory, defaulting to the current one, and finds the projects in it, either detected from the contents or listed in `infracost.yml`. A parser plugin reads the code, provider plugins price the resources through Infracost's pricing API, and the result is a monthly estimate per project, resource and cost component, plus policy results and diagnostics. Add `--json` for machine output, or use `infracost inspect` to slice the cached result without scanning again. It needs an Infracost token, from `infracost auth login` or `INFRACOST_CLI_AUTHENTICATION_TOKEN` in CI, and SSH keys if private modules must be fetched. It needs no AWS credentials, because prices come from Infracost's API rather than your account. That is also its limit: what only the cloud knows, such as the answer to a `data` source, is out of its reach.

code

bash · 5 lines
bash
infracost scan ./terraform
infracost inspect --summary
infracost inspect --top 10

infracost scan ./terraform --json > infracost.json

go deeper

for a junior

Recall the shape: code goes in, a monthly estimate per project and resource comes out, and prices come from Infracost's API. Name the Infracost token and the SSH keys it does need, so nobody hears no credentials at all.

for a middle

Explain the steps: project detection or infracost.yml, parser plugin, provider plugins and pricing API. Know that inspect reads a short-lived cache and that JSON output carries cost components with price and quantity.

for a senior

Point out what the absence of cloud credentials costs: no data source answers, no view of hand-made resources, no consumption figures. Treat a parse failure as a non-zero exit worth handling, not a zero estimate.

for a principal

Weigh the low-risk job, safe to run on every pull request with no cloud access, against a figure that is only as complete as the code. Decide what your organization may conclude from it.

## What the command is **Infracost** estimates cloud costs from infrastructure code. `infracost scan [path]` is its core command: point it at a directory, or let it default to the current one, and it reports what the code there would cost per month. This answer describes the current CLI, version 2.18.3. Commands such as `infracost breakdown` belong to the older 0.10 generation. The estimate is a calculation, not a measurement. Nothing is deployed, and nothing is read back from a running system. ## What it reads A scan moves through four steps: 1. **Find the projects.** If the target directory holds an `infracost.yml`, its project list is used. Otherwise the CLI generates a configuration by detecting projects from the directory contents. A **project** is one priced unit, such as a Terraform root module. 2. **Parse the code.** A parser plugin reads each project; Terraform and Terragrunt are among the supported types. Infracost downloads its plugins automatically, and they are not the Terraform providers named in your configuration. 3. **Price the resources.** Provider plugins look up each resource's cost components through Infracost's pricing API, by default `https://pricing.api.infracost.io`. 4. **Apply organization settings.** Policies, guardrails, budgets and usage defaults configured in Infracost Cloud are fetched for the run and applied to the result. The README says the target must be a directory. The code also accepts a single file of a type a plugin recognises, and a source comment names Terraform plan JSON as one such type. ## What it returns Without flags you get a human-readable summary. With `--json` the same result is written as JSON: | Field | Holds | |---|---| | `currency` | The price currency, USD unless `--currency` or the config says otherwise | | `summary` | Totals, such as `total_monthly_cost`, `costed_resources` and `triggered_guardrails` | | `projects[].resources[]` | Each resource, with its `cost_components`: name, unit, price, quantity, monthly cost | | `projects[].finops_results` | Results of cost-saving policies | | `projects[].tagging_results` | Results of tagging policies | | `projects[].diagnostics` | Parse errors and warnings per project | | `guardrail_results`, `budget_results` | How the run compares with organization limits | A project that fails to parse carries a critical diagnostic and no costs. The command still renders its output and then exits non-zero, so a broken project does not read as a successful $0 scan. ## Reading a result again `infracost inspect` works on the saved result and does not re-run the analysis: - `infracost inspect --summary` prints the headline numbers. - `infracost inspect --top 10` lists the ten most expensive resources. - `infracost inspect --min-cost 100` keeps resources at or above that monthly cost. - `infracost inspect --diagnostics` shows what went wrong per project. The cache is short-lived: an entry lasts one hour by default (`INFRACOST_CLI_CACHE_TTL`), and changing a source file invalidates it, after which `inspect` tells you to scan again. `--file` reads a saved JSON result instead of the cache. ## What it needs to run | Credential | Why | Where it comes from | |---|---|---| | Infracost token | Reach Infracost Cloud and the pricing API | `infracost auth login`, or `INFRACOST_CLI_AUTHENTICATION_TOKEN` in non-interactive jobs | | SSH keys, if needed | Fetch private modules over SSH | The standard `~/.ssh` keys, or `--ssh-key-file` | | Cloud account credentials | Not used for pricing | None; the CLI's scan passes the parser none | A team that cannot reach Infracost Cloud can set `INFRACOST_CLI_PRICING_API_KEY` and `INFRACOST_CLI_PRICING_ENDPOINT` to use a self-hosted pricing API. That mode needs no login, and it turns off policies, guardrails, budgets and usage defaults. ## What working from code costs you Holding no cloud credentials makes the job low-risk and easy to run on any pull request, and it sets the limits of the answer: - **Data sources.** With no cloud credentials nothing can query the provider, so a size or count derived from a `data` block cannot use the real answer. - **Running infrastructure.** The scan sees code, not what exists. Something created by hand is invisible to it. - **Consumption.** Request counts and stored gigabytes are not in the code, so those costs rest on usage values you supply or on none at all. HCP Terraform has its own built-in cost estimation, a separate feature that is off by default and runs as a phase between plan and apply.

  • What does a private module change about the credentials an Infracost scan needs?
    The parser has to fetch the module, so the job needs access to wherever it lives. For SSH sources the scan uses the standard `~/.ssh` keys, or the ones named by `--ssh-key-file`. That is access to a git host, not to a cloud account, so the job can still hold no AWS credentials.
  • Can an Infracost scan run with no route to Infracost Cloud?
    Yes, in self-hosted pricing mode. Set `INFRACOST_CLI_PRICING_API_KEY` and point `INFRACOST_CLI_PRICING_ENDPOINT` at your own Cloud Pricing API. The CLI then needs no login and sends no telemetry, but policies, guardrails, budgets, usage defaults and config templates are all skipped. The key is environment-only so it stays out of `ps` and shell history.

  • Infracost needs read access to the AWS account to know what the code will cost.
  • The scan's monthly figure comes from the current cloud bill.
  • infracost inspect re-runs the analysis every time you call it.
  • No cloud credentials means an Infracost scan needs no credentials at all.
  • A project that fails to parse simply shows up as costing nothing, with a clean exit.
open as a page

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?

level: seniorimportance: should knowfreq 20%

basics

~20 s

Infracost 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.

open as a page

With Infracost, why can a pull request adding a function behind a busy queue show almost no monthly increase while the bill later jumps, and how do you handle it?

level: seniorimportance: should knowfreq 24%

basics

~20 s

Infracost prices what code declares, and request counts or stored gigabytes are not in the code. Such a component shows a unit price and adds nothing to the total, so supply usage values and read the total as partial.

open as a page

When pull requests show an Infracost monthly cost diff and leadership wants large increases blocked, who should own the threshold, and should cost ever block a merge?

level: principalimportance: should knowfreq 17%

basics

~20 s

Default to a visible comment routed to an accountable owner, not a veto: the estimate misses usage, and engineers cannot usually approve spending alone. The budget holder owns the threshold; reserve blocking for high-confidence limits with a fast, recorded override.

open as a page

On Infracost CLI 2.x, what happens to an old script running `infracost breakdown --path . --format json --out-file base.json` and `infracost comment github`?

level: middleimportance: nice to knowfreq 15%

basics

~20 s

On Infracost CLI 2.x, breakdown warns and runs scan, keeping only the path and silently dropping the format and output flags, so no JSON file appears. The comment command fails outright and points to infracost ci setup.

open as a page

With Infracost, when would you commit an `infracost.yml` instead of relying on autodetection, and what does the file pin down?

level: middleimportance: nice to knowfreq 14%

basics

~20 s

Commit an infracost.yml when detection's project list or names are not what you want: it pins each project's path, name and type, plus a usage file. Names matter because diffs, guardrail costs and usage filters match projects by name.

open as a page