Cost estimation with Infracost

Intermediate · 14 min

Put a price delta next to the plan in the pull request: real list prices, how Infracost works, and what its 2.0 CLI change means for older tutorials.

Before thisPolicy as code with OPA, Checkov and Trivy

More than Certified · 14 min

Cost estimation with Infracost

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by More than CertifiedReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A cost estimate is quantity times list price; a one-line instance resize can add hundreds of dollars a month per instance.
  • Estimates miss discounts, usage-based charges you did not model, data transfer and growth, so they are a tripwire, not a forecast.
  • Infracost estimates from Terraform code or plan JSON, sends no cloud credentials, and posts the cost diff on pull requests.
  • Infracost 2.0 uses infracost setup and infracost scan; older tutorials show v0.10 commands breakdown and diff, so check current docs.

The change that doubled the bill

Someone changes instance_type from a 2-vCPU to an 8-vCPU machine because a load test was slow, and forgets to change it back. The plan says ~ instance_type in one line. The review approves it, because the diff is one line and the line is plausible. The invoice arrives three weeks later.

Nothing in Terraform’s plan shows money. It shows attributes. Cost estimation is a step that reads the same configuration or plan, looks up list prices and puts a dollar figure and a delta next to the diff, while a human can still say “wait.”

What a price delta is made of

An estimate is quantity times unit price, summed. For a virtual machine the quantity is hours. I looked at Azure’s public Retail Prices API, which needs no account, for one size in West Europe, and did the arithmetic by hand with 730 hours as a month, a common convention that I chose and that Infracost’s docs, as far as I read, do not state:

$ curl -s -G 'https://prices.azure.com/api/retail/prices' --data-urlencode \
    "$filter=serviceName eq 'Virtual Machines' and armRegionName eq 'westeurope' and armSkuName eq 'Standard_D2s_v5' and priceType eq 'Consumption'" \
  | jq -c '.Items[] | select(.productName|test("Windows")|not) | ... | {skuName,retailPrice,unitOfMeasure}'
{"skuName":"Standard_D2s_v5","retailPrice":0.115,"unitOfMeasure":"1 Hour"}
{"skuName":"Standard_D8s_v5","retailPrice":0.46,"unitOfMeasure":"1 Hour"}

Standard_D2s_v5 0.115 x 730h = 83.95
Standard_D8s_v5 0.46 x 730h = 335.8
diff 251.85

These are real list prices in US dollars, fetched on 28 November 2026 (the jq filter is trimmed in this listing, and the last three lines come from a small arithmetic script of mine). The resize is about $252 a month for one instance, per environment. With 12 instances it is over $3,000. That is the number you want printed in the pull request.

Two caveats from this exercise. The estimate is list price for on-demand usage, so reservations, savings plans and discounts are not in it. And hours are easy while usage-based services are not: object storage, data transfer, serverless invocations and logs depend on how much you use, which the code cannot say. Infracost lets you give it usage estimates, and its README says it can estimate usage-based resources such as S3 and Lambda. The result is only as good as your estimate.

Infracost

Infracost is an open-source tool, with a hosted dashboard, that does this for Terraform, Terragrunt, CloudFormation and AWS CDK. Its README says it covers over 1,100 resources across AWS, Azure and Google Cloud. It parses your project locally, asks its pricing API for prices, and prints a breakdown. The docs state that no cloud credentials or secrets are sent to the API, and that it can also work from a Terraform plan JSON. It asks for a free API key, obtained through infracost setup.

There is a current-versus-popular-video problem here, and you should know about it before you follow a tutorial. As of 28 November 2026, the infracost/infracost repository describes an “Infracost 2.0” release, in which the code was split into focused repositories. The CLI repository’s latest release is v2.21.0, from 24 November 2026, and its README leads with infracost setup and infracost scan, with infracost inspect for deeper detail. The older v0.10 line, last released as v0.10.51 on 25 November 2026, is the one older tutorials show with infracost breakdown, infracost diff and infracost comment. I read the READMEs and docs, and I did not verify whether the v0.10 commands still work with 2.x. I did not run Infracost, so I cannot show its output.

What stays the same across versions is the workflow, which the docs describe as a pull request comment with the cost difference, policy violations and tagging issues, with integration guides for GitHub Actions, GitLab CI and Azure DevOps. Cost works as a gate too: Infracost has FinOps and tagging policies, which the README describes as guardrails defined in its cloud dashboard. That is the same idea as the policy lesson with a dollar threshold, and the embed shows how to combine Infracost output with OPA.

Where in the pipeline

The estimate belongs in the pull request, next to the plan, before approval. It does not belong only on a dashboard someone checks monthly. The CI order that the pipeline lessons build up to is:

  1. fmt, validate, tflint (seconds, no credentials)
  2. terraform test with mocks
  3. plan, saved as a file
  4. policy scan and cost estimate on the plan
  5. human approval of the plan, with the cost delta in view
  6. apply that exact plan

Teams usually set a rule such as “a delta over a threshold needs a second approver” rather than blocking on any increase, because most increases are intended.

What an estimate misses

Count the things that are not in the bill estimate: taxes, support plans, data transfer between regions, the second environment someone forgot to destroy, and the growth of storage over time. The estimate catches the one-line resize and the extra NAT gateways. It does not catch a log bucket that grows a terabyte a month. Treat it as a tripwire for config changes, and use the cloud’s own budgets and alerts for the rest, as covered in the devops lesson on cloud cost and FinOps.

About the video

The embed is More than Certified’s “Controlling AWS costs in your Terraform GitHub Actions Workflows with Infracost and OPA”, 13 minutes 35 seconds, October 2024. Its description says that Infracost’s free tier together with Open Policy Agent can prevent AWS cost overruns in Terraform GitHub Actions pipelines, and that it shows how to set it up. It predates Infracost 2.0, so the commands it uses are likely the v0.10 ones, which I cannot confirm still work. It covers AWS only, and I did not watch it in full. English was judged from the title, description, channel and its English auto-caption track. I found no second video worth recommending, so deeper has only documentation links.

Where this fits

That finishes the quality gates: lint, test, policy, cost. The next group wires them into a pipeline, starting with Terraform in CI with saved plans and approvals.

Check yourself

  1. 01Why does the estimate belong in the pull request?

    So a reviewer sees the price delta next to the plan while approval is still pending.

  2. 02What are two costs a Terraform-based estimate usually misses?

    Usage-based charges such as data transfer and storage growth, and discounts, taxes or resources created outside Terraform.

  3. 03A video shows infracost breakdown and diff. What should you check?

    Whether those commands still exist in the 2.x CLI, which the current README presents with infracost setup and infracost scan.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark