Terraform cost estimation
Pricing a Terraform change before you apply it, so the review shows what it adds to the monthly bill.
Terraform cost estimation is the practice of pricing the resources in Terraform code or a Terraform plan before you apply them, so a reviewer sees the monthly cost change on the pull request. A tool reads the resources a change creates, updates or deletes, looks up each price, and reports the total and the delta. You have two ways in. One parses the .tf files. The other reads a saved plan: terraform plan -out writes the file, and terraform show -json turns it into JSON that lists each resource's before and after values. HCP Terraform offers cost estimation for AWS, Azure and Google Cloud as a run phase between plan and apply. Infracost's docs describe estimates from project files or plan JSON, with a comment on each pull request. Estimates price what the code declares, so usage-based charges stay uncertain. LevelFour's l4 estimate prices Terraform with no terraform plan and no deployed stack, and the PR bot posts the cost of each change on the pull request.
Frequently asked questions
How do you estimate Terraform costs before applying?
Run a cost tool against the code or a saved plan, then read the monthly total and the delta. To work from a plan, run terraform plan with -out to save it, then terraform show -json on that file, and pass the JSON to your estimator. Each entry in resource_changes carries its actions and its before and after values. To work from code, point the tool at the directory. Set the region either way, since cloud prices differ by region and a default can mislead you.
Does terraform plan show cost?
No. HashiCorp's terraform plan creates an execution plan that previews changes to your infrastructure, and it reports resources and attributes. Cost takes a second step that joins the plan to a price list. HCP Terraform does this once an administrator enables cost estimation, which adds a run phase between plan and apply and shows hourly and monthly cost plus the monthly delta for each estimated resource. Outside HCP Terraform, you pass the JSON plan to a separate estimator.
How accurate are Terraform cost estimates?
Estimates hold up for fixed-price resources and weaken for usage-based ones. A VM size and a provisioned disk map to a list price. Requests, data transfer and log volume depend on traffic that Terraform code doesn't declare, and HashiCorp's docs warn of unpredictable usage-based pricing. List prices also leave out commitments and negotiated discounts. AWS adds that its Price List APIs are informational, and the service pricing page wins any difference. Read the delta as a review signal, then reconcile it against the bill after deployment.
Can you block a Terraform change that costs too much?
Yes, with a policy or a CI gate. HCP Terraform exposes the estimate to Sentinel through tfrun.cost_estimate, in Legacy policy checks. HashiCorp's example policy fails a run that raises monthly spending by more than $100, and the enforcement level decides whether that stops the run. A mandatory run task can also stop a run. In any CI system, a step can exit non-zero over a threshold: per LevelFour's docs, l4 diff --fail-above 100 exits 2 when a change adds more than $100 a month. Gate on the delta, because a gate on the total fails each build in a workspace already above the line.
What is the difference between code-based and plan-based Terraform estimates?
A code-based estimate parses .tf files, and a plan-based estimate reads the output of terraform plan. Parsing needs no plan and no deployed stack, so a laptop or a pull request job can run it. A plan holds resolved values, because Terraform reads remote objects first, so it catches values that exist at plan time. Treat a saved plan with care: HashiCorp warns that sensitive data lands in the file in cleartext. Infracost's docs point to plan JSON for costs that resolve at plan time.
How do you add Terraform cost estimates to pull requests?
Add a CI job that runs on each pull request, prices the change against the base branch, and posts the result as a comment. GitHub Actions and GitLab CI both check out a shallow clone by default, so fetch enough history to find the merge base. Edit one comment on each push instead of adding new ones. Store the API token as a CI secret with read access, since a cost check changes nothing. A hosted bot skips the job: you connect the repository and it comments on each request.
Does Terraform cost estimation work with modules and variables?
Yes, if the tool can resolve them. A module call hides its resources until the tool downloads and reads the module, and a variable can change an instance size or a count. Pass the variable files your deployment uses, and let the tool fetch remote modules. Values Terraform can't know until apply show up under after_unknown in the JSON plan, and an estimator has to assume something for those. Check which region the estimate used, because a default region can return a plausible total that's wrong for your deployment.