Estimating Cloud Costs with Infracost/Terraform


The Problem

Before, if you wanted to know how expensive your cloud infrastructure would be, you would have to use a tool like Azure’s Pricing Calculator. It’s a nice tool that Azure provides, but it can involve a lot of tedious manual calcuations. Wouldn’t it be nice if you could automate price estimates?

Enter Infracost

Infracost uses your Terraform code to tell you how much your resources will cost before deploying them. This allows you to skip tedious calculations, avoid surprise billing, as well as include budgeting in your Terraform code pipelines!

Infracost is

  • Incredibly easy to use
  • built to work with AWS, Azure, and Google Cloud
  • Runs on Mac OS, Linux, and Windows *It’s Free!

Installation

Install for Mac OS

brew install infracost

infracost --version # Should show 0.10.33

Install for Linux

# Downloads the CLI based on your OS/arch and puts it in /usr/local/bin
curl -fsSL https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh | sh

Install with Chocolatey

choco install infracost

infracost --version # Should show 0.10.33

Get API key

Next, you have to register for a free API key.

infracost auth login

You will be prompted to register for a free API key. Once registered, click on β€œOrg Settings” to obtain your API key. Then set it on the CLI:

Infracost API Key

infracost configure set api_key MY_API_KEY

You can then get your api key with the following command.

infracost configure get api_key

Running a Cost Breakdown

cd ~/code/terraform-code/
infracost breakdown --path .

You will get an estimated breakdown of costs based on your defined Terraform resources.

Evaluating Terraform directory at .
  βœ” Downloading Terraform modules
  βœ” Evaluating Terraform directory
  βœ” Retrieving cloud prices to calculate costs

Project: Timothy-Pulliam/aks-terraform-dev

 Name                                            Monthly Qty  Unit                      Monthly Cost

 azurerm_application_gateway.this
 β”œβ”€ Gateway usage (basic v2)                             730  hours                          $146.00
 └─ V2 capacity units (basic)                            730  CU                               $5.84

 azurerm_key_vault_certificate.this
 β”œβ”€ Certificate renewals                  Monthly cost depends on usage: $3.00 per requests
 └─ Certificate operations                Monthly cost depends on usage: $0.03 per 10K transactions

 azurerm_postgresql_flexible_server.this
 β”œβ”€ Compute (GP_Standard_D8ds_v4)                        730  hours                          $519.76
 β”œβ”€ Storage                                              512  GB                              $58.88
 └─ Additional backup storage             Monthly cost depends on usage: $0.095 per GB

 azurerm_private_dns_zone.db
 └─ Hosted zone                                            1  months                           $0.50

 azurerm_public_ip.appgw_pip
 └─ IP address (static)                                  730  hours                            $3.65

 OVERALL TOTAL                                                                               $734.63
──────────────────────────────────
18 cloud resources were detected:
βˆ™ 5 were estimated, 3 of which include usage-based costs, see https://infracost.io/usage-file
βˆ™ 13 were free, rerun with --show-skipped to see details

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓
┃ Project                                            ┃ Monthly cost ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━┫
┃ Timothy-Pulliam/aks-terraform-dev                  ┃ $735         ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━┛

More Features

Infracost can do so much more, including

  • Save cost breakdowns as JSON files
  • Diff your current breakdown to previous saved JSON file
  • Integrate billing into your CI/CD pipelines
  • Display your breakdown in a web-based graphical dashboard by uploading a JSON file to Infracost Cloud
infracost upload --path infracost-base.json

Dashboard Charts

And so much more.

Check out the Docs for more information

https://www.infracost.io/docs/

or ask questions on the Infracost Community Slack channel

https://www.infracost.io/community-chat