With Infracost, what does `infracost scan` read and return, and why does it not need credentials for your cloud account?
answer
- a calculation, nothing deployed
- code in, priced projects out
- two kinds of plugin
- whose token does it hold?
basics
~20 sThe 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 linesinfracost scan ./terraform
infracost inspect --summary
infracost inspect --top 10
infracost scan ./terraform --json > infracost.jsongo deeper
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.
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.
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.
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.