Terraform Plan
By default, Infracost parses your Terraform HCL directly, which lets it shift left onto developer machines and pull requests without cloud credentials or a Terraform plan. Infracost can also estimate costs from a Terraform plan JSON file, the output of terraform show -json. Use plan JSON when your costs only resolve at plan time, when you run on HCP Terraform / Enterprise, or when you use Bitbucket Pipelines. Each case is covered below.
Costs that only resolve at plan time (data sources / dynamic lookups)
When Infracost parses Terraform HCL, some attribute values are not yet known: values that come from data sources, dynamic lookups, or other resources computed during terraform apply. Infracost prices everything that is statically defined, but costs that depend on those unresolved values cannot be estimated from HCL alone.
A plan JSON file is different. Terraform fully resolves every value before writing the file, so data sources and dynamic lookups appear as concrete values. Infracost reads those resolved attributes directly and prices them.
Because Terraform resolves the plan before Infracost sees it, the file is self-contained. Infracost does not download modules, read variables, or run the Terraform binary when processing it.
To use this, generate a plan JSON file and scan it.
Generate 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
Estimate costs on HCP Terraform / Enterprise
HCP Terraform (formerly Terraform Cloud) and Terraform Enterprise run your plans for you and produce the plan JSON as part of each run, so you do not need to generate it locally.
Infracost estimates costs from that plan JSON and reports them back to your pull requests. See the HCP Terraform / Enterprise integration for setup steps.
If you already have a plan JSON file from a run, you can scan it directly:
infracost scan plan.json
Use Infracost with Bitbucket Pipelines
Infracost does not provide a Bitbucket App the way it does for GitHub and GitLab. If you use Bitbucket, run Infracost inside Bitbucket Pipelines instead.
Most Bitbucket Pipelines that manage infrastructure already run terraform plan. Reuse that plan: convert it to JSON and point infracost scan at the file.
terraform plan -out tfplan.binary
terraform show -json tfplan.binary > plan.json
infracost scan plan.json
Infracost produces the cost breakdown in the pipeline, which you can post to the pull request or view in Infracost Cloud. For a full pipeline example, see CI/CD integrations.
OpenTofu plan JSON
Infracost supports OpenTofu plan JSON files in the same way as Terraform. Generate the file with the OpenTofu CLI and scan it:
tofu plan -out tfplan.binary
tofu show -json tfplan.binary > plan.json
infracost scan plan.json
Infracost detects a plan JSON file by its content, so OpenTofu and Terraform plan files are handled identically.
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:
- Infracost detects the file as a Terraform plan by inspecting its contents (it looks for the
format_versionandplanned_valueskeys that Terraform writes intoterraform show -jsonoutput). - 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.
- Infracost reports a cost breakdown that you can then drill into with
infracost inspect.