Home United States USA — software Taming Cloud Costs With Infracost

Taming Cloud Costs With Infracost

79
0
SHARE

A mistake in a Terraform file can cause your bill to balloon at the end of the month. Infracost parses your Terraform files and estimates costs for you.
Join the DZone community and get the full member experience.
When we combine the cloud with IaC tools like Terraform and continuous deployment we get the almost magical ability to create resources on demand. For all its benefits, however, the cloud has also introduced a set of difficulties, one of which is estimating cloud costs accurately.
Cloud providers have complex cost structures that are constantly changing. AWS, for example, offers 536 types of EC2 Linux machines. Many of them have similar names and features. Take for example “m6g.2xlarge” and “m6gd.2xlarge” — the only difference is that the second comes with an SSD drive, which will add $60 dollars to the bill. Often, making a mistake in defining your infrastructure can cause your bill to balloon at the end of the month.
So, how can we avoid this problem and use the cloud with confidence?
Infracost is an open-source project that helps us understand how and where we’re spending our money. It gives a detailed breakdown of actual infrastructure costs and calculates how changes impact them. Basically, Infracost is a git diff for billing.
Infracost has two versions: a VSCode addon and a command line program. Both do the same thing: parse Terraform code, pull the current cost price points from a cloud pricing API, and output an estimate.
We can see the estimates right in the IDE:
Or as comments in pull requests or commits:
To try out Infracost, we’ll need the following:
An Infracost API key. You can get one by signing up for free at Infracost.io.
The Infracost CLI installed in your machine.
Some Terraform files.
Once the CLI tool is installed, run infracost auth login to retrieve the API key. Now we’re ready to go.
The first command we’ll try is infracost breakdown. It analyzes Terraform plans and prints out a cost estimate. The –path variable must point to the folder containing your Terraform files. For example, imagine we want to provision an “a1.medium” EC2 instance with the following:
At current rates, this instance costs $28.62 per month to run:
If we add some extra storage (600GB of EBS), the cost increases to $155.

Continue reading...