Skip to main content

Terraform Plan

By default, Infracost parses your Terraform HCL code directly, which lets it shift left onto developer machines and pull requests without needing cloud credentials or a Terraform plan. However, Infracost can also estimate costs from a Terraform plan JSON file — the output of terraform show -json. This is useful when:

  • Your costs depend on values that can only be resolved at plan time (for example, attributes that come from data sources or other dynamic lookups).
  • You use Bitbucket (Infracost does not have a Bitbucket App) and you already generate a plan in your CI/CD pipeline that you can reuse.
  • You use HCP Terraform / Terraform Enterprise, which produces the plan JSON for you (see Terraform Cloud/Enterprise).

Plan JSON is fully resolved by Terraform before Infracost sees it, so the file is self-contained: Infracost does not download any modules, read variables, or run the Terraform binary when processing it.

OpenTofu plan JSON files are supported in the same way.

Generating a plan JSON file

Run the following in your Terraform project to produce a plan JSON file:

terraform plan -out tfplan.binary
terraform show -json tfplan.binary > plan.json

Usage

Point infracost scan at the plan JSON file:

infracost scan plan.json

You can also point it at a directory that contains the plan JSON file, and Infracost will auto-detect it:

infracost scan path/to/dir

Infracost identifies plan JSON files by their content (not just their filename), so the file does not need to be named plan.json. Plan JSON detection takes priority over the Terraform (HCL) and CloudFormation parsers, so a .json file that is a Terraform plan is always treated as one.

How the plan JSON integration works

When infracost scan runs against a plan JSON file:

  1. Infracost detects the file as a Terraform plan by inspecting its contents (it looks for the format_version and planned_values keys that Terraform writes into terraform show -json output).
  2. It reads the planned resources and their fully-resolved attributes directly from the plan, then prices them and applies your organization's FinOps and tagging policies.
  3. Infracost reports a cost breakdown that you can then drill into with infracost inspect.