Terraform
Terraform
June 1, 2021

Terraform Cost Estimation in 2021: The Definitive Guide

By
Ryan Fee

According to DevOps.com, $6.6 billion were wasted on oversized cloud resources in 2020. The inability to quickly visualize the cost of deployment and the lack of guardrail 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.

Summary

Infracost Scalr Terraform Cloud
Pricing Free Free up to 5 users, $20/user/month starting 6 users $70/user/month
Installation Binary Integrated Integrated
Policy As Code Framework Integration None Open Policy Agent Sentinel

Set up

Infracost

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.

Install Infracost

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 estimate the number of runs you need and then go to the Scalr sign-up page to see your cost estimates.

Create a free Terraform Cloud account

User Experience

Infracost

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

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).

Terraform Cloud

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.

Price accuracy

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.

us-west-2 AWS Reference Infracost Scalr Terraform Cloud
aws_db_instance 20 Go MySQL db.t2.micro $14.71 $14.71 $14.71 $14.46
aws_instance ubuntu t3.micro $8.39 $8.39 $8.39 $7.48
aws_ebs_volume 40 Go $4 $4 $4 $3.87

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. Terraform Cloud integrates with Sentinel, Hashicorp’s proprietary policy framework.

Infracost Scalr Terraform Cloud
No Open Policy Agent Sentinel

Conclusion

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.

Note: While this blog references Terraform, everything mentioned in here also applies to OpenTofu. New to OpenTofu? It is a fork of Terraform 1.5.7 as a result of the license change from MPL to BUSL by HashiCorp. OpenTofu is an open-source alternative to Terraform that is governed by the Linux Foundation. All features available in Terraform 1.5.7 or earlier are also available in OpenTofu. Find out the history of OpenTofu here.

Start using the OpenTofu & Terraform platform of the future.

A screenshot of the modules page in the Scalr Platform