Infracost is an open-source project released in June 2020 on their 0.1.0 version. It was created by cloud computer experts Hassan Khajeh-Hosseini, Ali Khajeh-Hosseini, and Alistair Scott. They have been working with cloud technologies since 2012, by providing solutions to tech giants such as Sony, Samsung, and Netflix.
Working with cloud providers and DevOps is all about speed, efficiency, and cost management. However, the cost of infrastructural changes can be challenging to gauge. A deployment that shifts allocated resources may lead to a displeasing bill at the end of the month.
Infracost aims to help companies estimate the cost of every IaC change made to their cloud infrastructure. One of the most difficult changes to determine is the cost associated with the deployment of new resources. Often they will require several structural adjustments. And this is where Infracost shines, providing a straight-to-the-point cost summary.
While Terraform Cloud offers a service similar to Infracost, there are some key differences between the two.
Their CLI tools are supported by the big three cloud providers, Amazon Web Services, Azure Cloud, and Google Cloud Platform. In addition, it can be integrated with many popular CI/CD tools, such as GitHub Action, GitLab CI, CircleCI, Bitbucket Pipelines, Atlantis, Jenkins, Azure DevOps, Terraform Cloud, and more.
Infracost offers a free program with open-source code, cost breakdowns, diffs, CI/CD integration, and has a large community behind it to offer support.
They offer a negotiable Enterprise program supporting multiple teams, a dashboard to centralize analysis and cost management for larger projects. In addition, they also provide the ability to integrate Source Control Management (SCM) enterprise editions of GitHub, GitLab, and BitBucket. It also has a self-hosting option at this tier and dedicated support that can be reached to assist with any issues and use cases. If interested, this program can be contracted by contacting their support at email@example.com.
Infracost works by using its Cloud Pricing API backend service. Using the CLI tool, you can parse a Terraform plan in JSON file format. The API then cross-references the individual cost of each cloud resource in use and their related cost parameters based on the cloud provider. The costs returned will give you a monthly approximation of the changes found in your plan.
Infracost will not need any credentials or secret information to perform its functions. It will also not make any alterations to Terraform or to the cloud resources it analyses. Instead, the API uses the count of Terraform resource types, which ensures that new resources are always accounted for, ensuring freshly deployed resources are part of the generated report. If you still have questions about how Infracost works, you can find more information in their frequently updated FAQ.
This command will download and install the CLI tool for a Linux machine and place it inside /usr/local/bin
To download, execute:
After downloading the CLI, you can get a free API key to start using the tool by executing:
The .yml file with the key is saved at:
From here, no additional configuration is needed in most cases, and both complete breakdowns and differences (diff) between plans can be obtained by using the following commands:
We have been using Infracost internally. However, we ran into an issue using our internal Terraform repository while running it with GitLabCI. As a result, we expected to be able to use our tfstate directly. Unfortunately, Infracost doesn't work with a remote tfstate. And since our tfstate is hosted on a remote S3 bucket, it failed.
To deal with this issue, we needed to add an extra step to our Terraform plan job to produce a plan file in JSON format, which Infracost can read and analyze.
To generate the file, we used the command:
This issue has to do with the current limitation of the tool, where it's unable to scan any remote tfstate. It might be by design, but it adds a problematic extra step to the workflow. Unfortunately, we weren't the first to face this issue. You can read more about it here.
Here’s what our .gitlab-ci.yml looked like and how we fixed this issue:
When our pipeline runs a new change to our TF code, this is what our Pull Requests / Merge Requests is going to output as a comment:
These variables needed to be define in our CI settings:
Infracost ongoing development has a lot in store, such as supporting more resources for AWS, GCP, and Azure Cloud. In addition, while Infracost is currently limited to Terraform, there are already plans to support Pulumi and CloudFormation.
If you wish to learn more about what Infracost has to offer for the future, which resources it will support next, new features planned, bugs fixed, and more, be sure to check their roadmap for the latest updates.
Infracost proved to be an incredible tool to use together with Terraform. It is essential to any DevOps Engineer looking to optimize and understand the costs of changes in infrastructure, deployment of services, and more. However, the tool still has plenty of space to improve. Supporting other cloud providers such as Digital Ocean will be a great addition. Integration with Slack and support for remote tfstate are on our wish list.