Terraform Cost Estimation in 2021: The Definitive Guide
According to DevOps.com, $6.6 billion were wasted on oversized cloud resources in 2020. The inability to quickly visualize the cost of a deployment and the lack of guardrails implementation within organizations are some of the reasons why.
For companies who leaped to infrastructure as code (and especially Terraform), some tools help prevent such waste from happening. Terraform cost estimators calculate the cost of a deployment by calling the cloud provider’s pricing APIs based on the Terraform templates they receive as input. The output is a breakdown of resource cost and their evolution compared to the current state. It can be displayed in pull request comments, CLI outputs, or used as input in policy frameworks code (such as Open Policy Agent or Sentinel).
In this article, we review the three main providers of cost estimation for Terraform: Infracost, Scalr and Terraform Cloud. We limited the scope of this guide to AWS, but these tools also have support for Google (Infracost, Scalr, Terraform Cloud) and Azure (Terraform Cloud). Please refer to their respective documentation (Infracost, Scalr, Terraform Cloud) to get an exhaustive view of the resources they support.
Our goal is to help you decide which of these three cost estimation providers is the best fit for your particular use case. We’ll review the setup process, the pricing, the resources coverage, the estimation accurateness and the integration with the policy as code frameworks.
||Free up to 5 users, $20/user/month starting 6 users
|Policy As Code Framework Integration
||Open Policy Agent
Infracost is free and open source. It must either be installed on your local machine (which is great for getting started or testing) or integrated into your CI/CD pipeline through GitHub Actions or Jenkins for example.
Scalr & Terraform Cloud
Scalr and Terraform Cloud differ from Infracost because they are not just cost estimation tools, but rather remote state & operations backends that offer a cost estimation feature. Scalr and Terraform Cloud provide features around automation and collaboration on Terraform.
To estimate Terraform costs with Scalr & Terraform Cloud, you’ll first need to create a free account, and then link your AWS cloud credentials to enable cost estimates. Please note that you will have to run Terraform operations (plan, apply, …) remotely either from Scalr or Terraform Cloud.
Create a free Scalr account
Create a free Terraform Cloud account
To run Infracost, just type the
infracost command in the Terraform root directory. Infracost displays a detailed estimation of the monthly costs, broken down by resource. It also can post the results of the cost estimation to a pull request comment, where you’ll be able to visualize the evolution of the cost based on the current state.
Scalr automatically runs cost estimation during the plan and apply phases. It displays an estimation of the monthly costs as well as the evolution of the cost based on the previous state in the Scalr UI (for all runs) and in the CLI (for CLI-triggered runs).
Like Scalr, Terraform Cloud displays an estimation of the monthly costs as well as the evolution of the cost based on the previous state on the UI and the CLI.
We ran a quick experiment to see how accurate the cost estimations were compared to the AWS reference price on three resources that all providers support. Both Infracost and Scalr predicted the same and correct cost, while Terraform Cloud always slightly underestimated it.
20 Go MySQL db.t2.micro
Policy As Code Integration
While it is already great progress to let developers visualize the cost of a Terraform deployment in the CLI or a pull request before running terraform apply, what if you could set guardrails and automatically prevent certain deployments if they go over a certain cost threshold? This is where policy as code frameworks come into play.
Infracost has no integration with a policy as code framework, so you’ll have to wire things up yourself to automatically enforce policy. Scalr integrates natively with Open Policy Agent, which is an open-source policy framework that works across the cloud-native stack. [Here is an example of an OPA policy that checks if a cost estimate is above a certain threshold(https://github.com/Scalr/sample-tf-opa-policies/blob/master/cost/limit_monthly_cost.rego). Terraform Cloud integrates with Sentinel, Hashicorp’s proprietary policy framework.
||Open Policy Agent
Infracost is great to start experimenting with now or if you already have a DIY Terraform pipeline in place and have no plan to upgrade it to a remote state & operations backend. Infracost also has the best coverage for AWS resources, and the ability to comment on pull requests with cost estimation results.
Scalr & Terraform Cloud are similar since they integrate cost estimation as part of their remote operations backend. You’ll have to use them as a remote backend to leverage their cost estimation features. This can make sense if you are looking to standardize your Terraform usage and make it easier to collaborate on Terraform as a team. The main differences between the two are the pricing ($20/user/month for Scalr if your team is larger than 6 vs. $70/user/month for Terraform Cloud) and the policy as code framework you want to work with (Open Policy Agent vs. Hashicorp’s Sentinel).
In any case, Terraform cost estimation is still a burgeoning area of Terraform tools, and 2021 will most likely be the year where we will see a lot of improvements, in terms of resources or cloud providers supported.
Ready to get started?
Scalr is free for up to 5 users