If you were using terraform excessively, you probably have seen output like that:

  • 84 resources to add
  • 17 resources to modify
  • 28 resources to remove

You can carefully review the changes, but there is always a room for an error. When deploying resources to the public cloud, you have to be extra careful, because mistakes in the configuration can be costly. You can easily create fleet of 100 servers instead of 10, or enable expensive settings by the mistake.

Those scenarios can be mitigated too some degree, by having budget caps, budget alerts, or budget monitoring. But they all will trigger post factum, when some of the damage is already done.

Recently, I was wondering if is there a tool for estimating infrastructure cost before running terraform apply. Infracost is doing exactly that task. It is checking the terraform plan for changes, then contacts their API for cloud prices.

Because it is CLI tool, it can be easily integrated into existing pipelines, and can be a part of merging process.

Sample output:

infracost breakdown --path . --terraform-var-file=dev.tfvars --show-skipped
INFO Autodetected 1 Terraform project across 1 root module
INFO Found Terraform project main at directory . using Terraform var files softserve.tfvars
WARN 2 google_container_node_pool prices missing across 1 resource
     2 google_container_cluster prices missing across 1 resource


Project: main

 Name                                                                                                     Monthly Qty  Unit                    Monthly Cost   
                                                                                                                                                              
 module.gke_cluster.google_container_cluster.primary                                                                                                          
 ├─ Cluster management fee                                                                                        730  hours                         $73.00   
                                                                                                                                                              
 google_compute_global_address.public_gw_addr                                                                                                                 
 └─ IP address (unused)                                                                                           730  hours                          $7.30   
                                                                                                                                                              
 module.gke_cluster.google_container_node_pool.pools["app-node-pool"]                                                                                         
 ├─ Instance usage (Linux/UNIX, on-demand, e2-small)                                                            2,190  hours                      not found   
 └─ Balanced provisioned storage (pd-balanced)                                                                    300  GB                         not found   
                                                                                                                                                              
 google_kms_crypto_key.gke_crypto_key                                                                                                                         
 ├─ Key versions                                                                                    Monthly cost depends on usage: $0.06 per months           
 └─ Operations                                                                                      Monthly cost depends on usage: $0.03 per 10k operations   
                                                                                                                                                              
 module.ssl_certificates["grafana"].google_dns_record_set.challenge["grafana-dev.goog.onefor.fun"]                                                            
 └─ Queries                                                                                         Monthly cost depends on usage: $0.40 per 1M queries       
                                                                                                                                                              
 module.ssl_certificates["webapp"].google_dns_record_set.challenge["webdev.goog.onefor.fun"]                                                                  
 └─ Queries                                                                                         Monthly cost depends on usage: $0.40 per 1M queries       
                                                                                                                                                              
 OVERALL TOTAL                                                                                                                                      $80.30 

*Usage costs can be estimated by updating Infracost Cloud settings, see docs for other options.

──────────────────────────────────
37 cloud resources were detected:
∙ 6 were estimated
∙ 23 were free
∙ 8 are not supported yet, see https://infracost.io/requested-resources:
  ∙ 2 x google_certificate_manager_certificate
  ∙ 2 x google_certificate_manager_dns_authorization
  ∙ 1 x google_binary_authorization_policy
  ∙ 1 x google_certificate_manager_certificate_map
  ∙ 1 x google_certificate_manager_certificate_map_entry
  ∙ 1 x google_compute_security_policy


Project ┃ Baseline cost ┃ Usage cost* ┃ Total cost ┃
main    ┃           $80 ┃           - ┃        $80 ┃

I am surprised why isn’t it already a part of public cloud infrastructure. It would so much convenient for the users, to know approximate price beforehand, instead of using price calculators manually.

Leave a Reply

Your email address will not be published. Required fields are marked *

+ ,