skip to content

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%

answer

  1. generated on every run otherwise
  2. a project needs a stable identity
  3. look where the scan points
  4. the path nobody checks

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.

solid answer

~50 s

Without the file, the CLI generates a configuration on each run by detecting projects under the scan target, using `infracost.yml.tmpl` as a template if one exists. Commit `infracost.yml` when the detected list is wrong or noisy, when each environment needs a stable name of its own, or when usage values must be attached. It holds `version`, a `projects` list with `path`, `name` and `type`, and settings such as `usage_file` and `currency`. Two traps: the CLI looks for the file only in the directory you scan, so `infracost scan ./terraform` ignores one at the repository root; and a `usage_file` path that does not exist is skipped with no error. The project name also acts as an identity. Rename a project and the next diff shows one removed and one added, and any filter matching the old name stops matching.

code

yaml · 9 lines
yaml
version: "0.3"
usage_file: usage.yml
projects:
  - path: live/staging
    name: payments-staging
    type: terraform
  - path: live/production
    name: payments-production
    type: terraform

go deeper

for a junior

Recall that scanning works with no config because projects are detected, and that infracost.yml lists projects explicitly with a path, a name and a type.

for a middle

Explain the lookup order of config file, template and generation, that the lookup covers only the scanned directory, and how usage files attach to the repository or to one project.

for a senior

Call out the silent failures: a mistyped usage file gives no error, a scan of a subdirectory misses the root config, and a project rename turns the next diff into a removal plus an addition.

for a principal

Decide whether the organization standardises on committed configs or on templates. Stable project names are what guardrails, usage defaults and comparisons rest on, so naming is worth a convention.

## What the file is `infracost.yml` is Infracost's repository configuration. It tells `infracost scan` which **projects** to price, where a project is one independently priced unit such as a Terraform root module. The file is optional, so the real question is when detection stops being good enough. ## What happens without it For each scan the CLI decides its project list like this: 1. It looks for `infracost.yml` in the scan's target directory. If the file exists, it is loaded and generation is skipped. 2. If not, it generates a configuration by detecting projects under that directory, skipping hidden directories and ignoring permission errors. 3. If an `infracost.yml.tmpl` sits in the directory, it is used as the template for that generation. A template configured in Infracost Cloud can also feed generation. A committed config beats a template: when both exist, the template is not used. The generated config is rebuilt for each scan, so what detection decides can change as the repository changes. ## What the file contains | Key | Meaning | |---|---| | `version` | The config format version; the current CLI's own tests use `0.3` | | `projects` | The list of projects to scan | | `projects[].path` | Where the project lives, relative to the scanned directory | | `projects[].name` | The project's name in output, diffs and filters | | `projects[].type` | The project type, such as `terraform` | | `usage_file` | A usage file for the whole repository, relative to the scanned directory | | `currency` | The price currency, which the `--currency` flag overrides | A project can also carry its own usage file, which takes the place of the repository-level file for that project. Project entries hold more settings than this table shows, including Terraform workspace and per-environment options. The schema itself lives in a separate Infracost package, so check its documentation for the exact keys. ## When committing it pays - **Detection picks the wrong set.** It is deciding from directory contents, so it cannot know that a directory is an example you never deploy, or that two directories form one thing to you. - **One directory serves several environments.** Separate entries with separate names keep the estimates apart. - **You rely on usage values.** The usage file has to be attached somewhere, and `infracost scan` has no flag for it. - **Project names are referenced elsewhere.** Filters in Infracost Cloud need names that do not move. - **You want a reviewable record.** A file in the repository shows up in code review, so a change to what is priced is visible. For a small repository with one root module and no usage file, detection is usually enough, and adding a file gives you something else to keep current. ## Why the project name is an identity The name looks like a label, but several features key on it: - **`infracost diff`** pairs projects by name and path, then by name alone. A project with no partner is reported as added or removed. - **Guardrails** match base and head projects by name to work out per-project cost changes, and a project-level guardrail can filter on project names. - **Usage defaults** in Infracost Cloud can be filtered by project name. - **Production filters** are evaluated using the repository, branch and project name. So renaming a project in the file makes the next diff show a large removal and an equal addition, and a filter written against the old name quietly stops applying. Treat a rename as a small migration: refresh baselines and check the filters that mention it. ## Traps - **Location.** The lookup joins the target directory with the file name and does not walk upward. `infracost scan ./terraform` reads `./terraform/infracost.yml` and never the one at the repository root, and paths in the file resolve against that same directory. - **A missing usage file.** The scanner uses `usage_file` only if the path exists and is a file. A typo produces no error, and the estimate simply falls back to whatever other usage values apply. - **Old examples.** The older CLI's example config uses `version: 0.1` and is aimed at `infracost breakdown --config-file`. On the current CLI the config is found by location, and `scan` has no `--config-file` flag.

  • What does `infracost.yml.tmpl` add over a plain `infracost.yml`?
    It feeds generation instead of replacing it: the CLI still generates a config at scan time, using the template as its basis. That suits a repository with many projects following a pattern, where listing each by hand would go stale. If an `infracost.yml` is also present, that file is used and the template is not.
  • How would you confirm which projects an Infracost scan actually priced?
    Read the result, not the config. `infracost scan --json` lists each project with `project_name` and `path`, and `summary.projects` gives the count. `infracost inspect --group-by project` on the cached result shows the same split, and running with `--debug` logs how many projects autodetection discovered and each project's name, path and type.

  • An infracost.yml is required, so a scan cannot run without one.
  • The CLI walks up to the repository root to find infracost.yml.
  • A wrong usage_file path fails the scan, so a typo gets caught straight away.
  • Project names are cosmetic labels you can rename at any time.
  • When infracost.yml and infracost.yml.tmpl both exist, the CLI merges them.