skip to content

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%

answer

  1. what a resource block never says
  2. zero is not free
  3. an asterisk on the figure
  4. values you must supply yourself

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.

solid answer

~50 s

A resource block says a function exists and how it is sized. It does not say how many requests will arrive, so that cost has a price but no quantity. Infracost marks such a component `usage_unknown` and reports `Monthly cost depends on usage` with the unit price, and it contributes zero to the total. That is the near-zero figure in this pull request. The fix is to supply quantities: a usage file named in `infracost.yml`, or usage defaults in Infracost Cloud, with the file merged on top. Values you supply are still assumptions, so base them on metrics from something similar. Treat the total as what could be priced from the code, not a forecast. It is good for catching a wrong instance size or a multiplied count, and it misleads most on consumption-heavy designs.

code

yaml · 5 lines
yaml
version: "0.1"
resource_type_default_usage:
  aws_lambda_function:
    monthly_requests: 100000
    request_duration_ms: 500

go deeper

for a junior

Recall that some costs depend on how much a resource is used, and that the code does not say that. Know that Infracost can show a price per unit with no monthly figure.

for a middle

Explain price times quantity, where the quantity comes from for each kind of cost, and how a usage file attaches through infracost.yml. Read the usage_based and usage_unknown flags in JSON.

for a senior

Diagnose a too-low estimate from the output's own flags, spot a mistyped usage file, and list the other gaps: 730-hour assumption, unpriced types, parse failures, offsetting changes. Say what the figure is good for.

for a principal

Decide how much weight the figure may carry for consumption-heavy systems, and who keeps usage assumptions current. A shared default across teams gives consistency, while stale values give false comfort.

## Two kinds of cost Infracost multiplies a **price** by a **quantity**. The pricing API supplies the price. The quantity has to come from somewhere else, and that is where the two kinds of cost split: | | Fixed by the code | Driven by usage | |---|---|---| | Quantity comes from | The configuration: size, count, hours | Traffic, data and behaviour at run time | | Example | An instance's hourly charge | A function's `monthly_requests` | | With no extra input | A real monthly figure | A figure on assumed usage, or a unit price with no monthly figure | | In the total | Counted | Counted only when a quantity exists | A function behind a busy queue is the second kind. The configuration says how it is built and says nothing about how many messages will arrive next month, and no amount of parsing can recover that. A Terraform plan would not help either, because a plan describes resources and not future traffic. ## How the output marks it The scan and diff output carry this distinction, so you can check before arguing with the number: - **`usage_based`** on a cost component means it is priced per unit of usage, so it rests on assumed usage unless you supplied your own. - **`usage_unknown`** means the provider gave no quantity, so a zero there means depends on usage, not free. - **`cost_depends_on_usage`** in a diff means one side is usage-driven, so there is no delta worth printing. - **`cost_change_incomplete`** in a diff means part of the change comes from usage that only one side knows. - **`has_usage_costs`** rolls this up to the project and the whole diff. Human output shows the same thing as the line `Monthly cost depends on usage` with a per-unit price, and as an asterisk on figures that rest on assumed usage. ## Supplying usage The current CLI takes usage values from two places: 1. **Usage defaults** configured in Infracost Cloud, optionally filtered by project name. 2. **A usage file** named by `usage_file` in `infracost.yml`, merged on top of those defaults. A project can name its own. `infracost scan` has no `--usage-file` flag. That belongs to the older CLI, as does `--sync-usage-file`, which synced a usage file with missing resources. Self-hosted pricing mode skips the Cloud defaults, leaving only the file. The file's `resource_type_default_usage` map sets values for every resource of a type, and the older CLI's example file also shows `resource_usage` for a single resource address. ## Diagnosing the surprise For the pull request in the question: 1. Read the diff's detail, not only its total. Look for `Monthly cost depends on usage` on the new resources. 2. Confirm with `infracost diff --json`, checking `has_usage_costs` and the flags on each component. 3. Check whether the `usage_file` path is right, since a mistyped path is skipped with no error. 4. Add values from metrics of a comparable service, then rerun. 5. Compare the new figure with the bill later and correct the values, because an old assumption looks exactly as precise as a fresh one. ## What else the number cannot see Usage is the largest gap, not the only one: - **Hours.** Hourly prices are scaled to a 730-hour month, so a sandbox destroyed each night reads high unless a usage value corrects it. - **Unpriced types.** A resource type Infracost cannot price appears with `is_supported: false` and adds nothing. - **Parse failures.** A project that fails to parse has no costs. `infracost scan` exits non-zero for it, and `infracost diff` stops, because the project would otherwise read as a mass deletion. - **Net figures.** Increases and decreases can cancel, so `Monthly estimate unchanged` does not mean nothing changed. - **Prices.** The figure is a price times a quantity from Infracost's data, and it is not read from your invoice. ## What it is good for The estimate is strong on **relative change in declared capacity**: a wrong instance size, a count that multiplied, a whole environment added by accident. Its value is that someone sees this before merging. It is weak as a **forecast**, and weakest on designs where consumption is most of the cost, which is exactly where a confident total misleads most.

  • In an `infracost diff`, how does a resource that loses its usage values read?
    It does not read as a saving. If either side of a component has no known quantity, the component is marked `cost_depends_on_usage` and shows `Monthly cost depends on usage` instead of a delta, because a $100 component losing its usage must not look like a $100 reduction. When a resource has a fixed charge too, it keeps a total and is flagged `cost_change_incomplete`.
  • Where should the values in an Infracost usage file come from?
    From measurements of something comparable: the same service in another environment, or a load test, adjusted for the expected traffic. A guess is acceptable only if the review knows it is one. The team should revisit values against real bills, because the file gives no signal when it goes stale and the estimate looks equally precise either way.
  • Would pointing Infracost at Terraform plan JSON solve the usage problem?
    No. Plan JSON can improve what is known about the configuration, since values resolved during planning are written into it, but it still describes resources and not next month's traffic. A plan adds no request counts, so a usage-based component needs usage values either way, and you have taken on a plan job that needs cloud credentials.

A builder's quote prices the boiler and radiators from the plans, but it cannot price your gas bill. That depends on how warm you keep the house, so the quote can only give the rate per unit.

  • A usage-based component with no quantity is free, so a zero means no cost.
  • Infracost reads current usage from the cloud account's metrics during a scan.
  • Running terraform plan first gives Infracost the request counts it was missing.
  • On the current CLI, infracost scan --usage-file is how you feed in usage values.
  • The monthly total from a scan is a forecast of the next cloud bill.