Skip to main content
All posts

Guide

Terraform Cost Estimation: How It Works and the Tools That Do It

How a Terraform cost estimate is built from the plan and the clouds' price lists, what it misses, the tools that make one, and how to put it in code review.

Gabriel NielsenCo-founder and CPO
Published
Updated
Read time
9 min
Ler em portuguêsEstimativa de custo do Terraform: como funciona e as ferramentas

Terraform doesn't estimate cost on its own: its CLI plans and applies changes but has no cost command. You get an estimate from a tool that reads your plan or your code, looks up a price for each resource, and reports the monthly cost and what the change adds. Infracost's open source CLI runs in your pipeline, HCP Terraform adds an estimate phase on its Standard and Premium plans, env0 and Spacelift run Infracost inside their own runs, and Scalr prices plans through Infracost's pricing server. LevelFour's PR bot posts the estimate on every pull request that changes Terraform or CloudFormation, with cheaper settings you can commit from a comment.

We read HashiCorp's and the clouds' documentation and each tool's own pages, as of October 2026. We make LevelFour, so its PR bot gets the most room, and each claim about another tool links to that tool's page.

How Terraform cost estimation works

An estimate prices two versions of your infrastructure, the one in Terraform's state today and the one your change proposes, and reports the difference. Most tools start from the plan. Save it with -out, then turn it into JSON:

Terminal
terraform plan -out=tfplan
terraform show -json tfplan > plan.json

The JSON's resource_changes array lists every resource the plan touches, each with its actions and its attributes before and after the change. A tool maps each resource type and the attributes that set its price, such as an aws_instance's instance_type, to a price, multiplies hourly prices by 730 hours for a month, and adds up both versions. The saved plan and the JSON made from it hold sensitive values in plain text, so guard both like secrets. terraform plan.

Some tools skip the plan and read your .tf files instead. LevelFour's l4 estimate parses Terraform on your machine and sends only the resources to the LevelFour API, so it needs no terraform plan and no deployed stack.

Where the prices come from

Each cloud publishes its list prices through an API, and estimate tools build on those prices, directly or through a price database of their own.

  • AWS: the Price List Query API and the Price List Bulk API, both behind an IAM permission. The Query API has no Savings Plan prices, and when the API and a service's pricing page disagree, AWS charges the price on the pricing page. AWS Price List API.
  • Azure: the Retail Prices API answers without authentication, with savings plan prices only on its 2023-01-01-preview version. Azure Retail Prices API.
  • Google Cloud: the Cloud Billing Catalog API needs an API key, and the newer Pricing API can return the custom prices of your contract. Cloud Billing Catalog API.

These are list prices for one region. An estimate priced in us-east-1 for a stack that deploys in São Paulo misses by the difference between the two regions, so price in the region your stack runs in.

What an estimate can't know

A plan says what you will run, not how much traffic it will serve or what you pay after discounts. Three gaps separate an estimate from the bill:

  • Usage. AWS Lambda event functions charge per request and for the time your code runs, and Amazon S3 for storage, requests, and data transfer. None of that is in the code. Infracost reads the usage you expect for each resource from an infracost-usage.yml file, and its paid tiers set usage defaults centrally. Infracost on usage-based resources.
  • Your discounts. List prices leave out Savings Plans, Reserved Instances, committed use discounts, and private pricing, so a team with high commitment coverage sees estimates above its bill. Infracost Cloud supports a custom price book, and Google's Pricing API can return your contract prices.
  • Resources outside the change. Anything created by hand, by another stack, or by an autoscaler at run time isn't in the plan, so the estimate leaves it out.

Read an estimate as the size and direction of a change, and confirm the result on the bill after the merge.

Terraform cost estimation tools

Neither the Terraform CLI nor OpenTofu has a cost command, so the estimate comes from one of these. The Best Infracost Alternatives in 2026 compares them in more depth.

LevelFour
How it estimates
Prices the Terraform or CloudFormation a pull request changes, and suggests cheaper settings
Where the estimate shows
A comment, a status, and inline suggestions on GitHub and GitLab; exit codes from the l4 CLI in CI
Best fit
Teams that want the estimate and the fix in the same review
Infracost
How it estimates
Open source CLI for Terraform, Terragrunt, CloudFormation, and more, with a hosted product on top
Where the estimate shows
Pull request comments on GitHub, GitLab, Azure Repos, and Bitbucket, and editor extensions
Best fit
Teams that want an open source CLI in any pipeline
HCP Terraform and Terraform Enterprise
How it estimates
An estimate phase between plan and apply, for resources on AWS, Azure, and Google Cloud
Where the estimate shows
The run, and Sentinel policies in legacy mode
Best fit
Teams that run Terraform in HCP Terraform on Standard or Premium, or in Terraform Enterprise
env0, Spacelift, and Scalr
How it estimates
Infracost inside their own runs, or its pricing server for Scalr, with your Infracost API key
Where the estimate shows
Their runs and policy engines; Spacelift also posts it on pull requests
Best fit
Teams that already deploy through one of them
Terracost
How it estimates
A Go library that estimates Terraform costs from cloud prices you load into MySQL
Where the estimate shows
Wherever you build it in
Best fit
Teams building their own estimate tooling

Sources: each tool's own documentation and pricing pages.

LevelFour

The LevelFour PR bot reads every pull request on GitHub and every merge request on GitLab that changes Terraform or CloudFormation, prices the change, and proposes cheaper settings where it finds them. The l4 CLI prices the same code in a pipeline. The next sections show both.

Infracost

Infracost's CLI is open source under the Apache 2.0 license and prices Terraform, Terragrunt, and CloudFormation, among others, on AWS, Azure, and Google Cloud, and its hosted repository integration adds AWS CDK. Its free CI/CD plan includes 1,000 runs a month and integrates with GitHub, GitLab, and Azure DevOps. Starter costs $250 a month, and Cloud, at $1,000 a month, adds Cost Guardrails, which can block a merge past a threshold you set, and a custom price book. Infracost pricing. Extensions for VS Code, JetBrains IDEs, and other editors show estimates as you write. Infracost editor extensions.

HCP Terraform

HCP Terraform, HashiCorp's hosted service, estimates resources on AWS, Azure, and Google Cloud in an extra run phase between plan and apply, with the hourly and monthly cost and the change. Cost estimation is off by default and comes with the Standard and Premium plans. Terraform Enterprise, the self-hosted edition, offers it too, and Sentinel policies can read the estimate in the legacy policy execution mode. HCP Terraform cost estimation.

env0, Spacelift, and Scalr

All three estimate with your Infracost API key: env0 and Spacelift run Infracost inside their own runs, and Scalr prices plans through Infracost's pricing server. env0 estimates Terraform, OpenTofu, and Terragrunt. Spacelift runs an Infracost breakdown that gives feedback on pull requests and feeds its plan policies. Scalr estimates Terraform and OpenTofu plans and passes the estimate to its OPA policies as cost_estimate.

Terracost

Cycloid's Terracost is an MIT-licensed Go library that estimates Terraform costs for AWS, Google Cloud, and Azure from prices you ingest into a MySQL database. Teams that build their own estimate tooling start from it. Terracost on GitHub.

Put the estimate in code review

Much of a cloud bill gets decided in code review. Someone picks an instance size out of habit, copies a disk type from another module, or raises a replica count for a load test and never lowers it. By the time the bill shows it, the change has been running for weeks. The LevelFour PR bot puts the estimate in the pull request, where the reviewer can still change the code. On each pull request it posts:

  • A cost comment with the cost of the changed resources before and after, and the monthly spend. The bot edits that one comment on each push, so the thread stays readable.
  • A LevelFour status in the checks panel on GitHub and in the pipeline status on GitLab, with the analysis summary. The status fails only when the analysis itself could not run.
  • Inline suggestions on the lines of the diff, each one a concrete change to a resource, ready to commit.

In CloudFormation, the bot reads YAML and JSON templates, SAM templates, and nested stacks whose template lives in the repository. It resolves parameters, conditions, and mappings before it prices, and lists anything it had to assume under Assumptions in the cost comment.

Commit a fix with one comment

Reply to the bot and it acts on the request. apply writes the suggestions into one commit on the branch and replies with its short SHA. On GitLab it also resolves the threads whose suggestions it applied. On GitHub, if the branch moved since the bot's last analysis, it refuses rather than overwrite newer work.

Pull request comment
@levelfourai apply all
@levelfourai apply aws_instance.batch
@levelfourai ignore AWS::EC2::Volume.Logs
@levelfourai snooze waiting for the load test
@levelfourai dismiss sized for the holiday peak

Anyone who can comment can run help. The commands that change something need write access to the repository on GitHub, or the Developer role or above on GitLab. dismiss and snooze take a reason, and the cost comment shows who made the call and why, so the next reviewer sees the decision.

Tune it per repository

A .levelfour/config.yml file at the root of the repository, on the default branch, turns the bot off, keeps the cost comment without inline suggestions, skips paths and resource types, or maps a CloudFormation template to the parameters file it deploys with:

.levelfour/config.yml
code_suggestions_enabled: false
excluded_paths:
  - tests/fixtures/**
  - examples/**
excluded_resource_types:
  - aws_iam_role_policy_attachment
  - AWS::Logs::*
cloudformation:
  parameter_files:
    stacks/app.yaml: stacks/params/prod.json

enabled: false turns the bot off, max_code_suggestions caps the inline suggestions at anywhere from 0 to 25, and region_override pins every estimate to one AWS region. The bot drops a field it does not recognize and warns about it at the bottom of its comment, so a typo such as enabld: false leaves the bot on.

Gate a pipeline on cost

The l4 CLI prices the same Terraform and CloudFormation from a terminal or a pipeline. With --fail-above, l4 estimate exits with code 2 once the monthly total passes your threshold, and a pipeline can branch on that code before anything deploys. l4 diff gates on what a change adds instead of the total.

Terminal
l4 estimate --fail-above 500 -q ./infra/

By default, l4 diff measures against the git merge-base with main or master, so the job needs full git history: fetch-depth: 0 on GitHub Actions and GIT_DEPTH: 0 on GitLab CI. The CI/CD guide has both jobs written out.

Get started

Connect GitHub or GitLab from the LevelFour dashboard, and the bot starts on the next pull request that touches Terraform or CloudFormation. On GitHub, an organization Owner installs the LevelFour app. On GitLab, you need the Maintainer or Owner role on each project, on GitLab.com or a self-managed instance. The cost check on pull requests comes with every plan.

The PR bot docs list every command, and the configuration reference has every field.

Start with LevelFour and see the cost of your next pull request before it merges.

Frequently asked questions

Does Terraform have built-in cost estimation?

The Terraform CLI has no cost command, and neither does OpenTofu. HCP Terraform, on its Standard and Premium plans, and Terraform Enterprise add a cost estimate phase between plan and apply. Everywhere else, the estimate comes from a tool such as LevelFour or Infracost.

How accurate is a Terraform cost estimate?

As accurate as the prices and usage behind it. Estimates use each cloud's list prices for one region, so they leave out Savings Plans, Reserved Instances, and private discounts, and usage-based costs such as Lambda requests or data transfer need usage figures you supply. Use the estimate for the size and direction of a change, and the bill to confirm it.

Which infrastructure as code does the LevelFour PR bot read?

Terraform and CloudFormation, on GitHub pull requests and GitLab merge requests. For CloudFormation it reads YAML and JSON templates, SAM templates, and nested stacks whose template lives in the repository. What is shift-left FinOps?.

Who can run the bot's commands?

Anyone who can comment can run help. dismiss, snooze, ignore, and apply need write access to the repository on GitHub, or the Developer role or above on GitLab.

How do I keep the cost comment and drop the inline suggestions?

Set code_suggestions_enabled to false in .levelfour/config.yml. The bot keeps posting the cost comment and the status.

Try LevelFour free for 14 days

See what it saves on your cloud, SaaS, and AI bills. No credit card required.

Start free trial

See what LevelFour can save you