Using Infracost to estimate costs for Azure resources defined in Terraform Configurations

azure terraform cloud infrastructure-as-code cost-optimization cloud-costs devops

The intention of this post is to test the tool “Infracost” for estimating costs of Azure resources, which are defined in Terraform configurations. The Azure Pricing Calculator will be used for comparison

cover

1. Introduction

If you are provisioning resources in Azure by using Terraform, you probably think of the arising expenses after conducting the deployment(s). My approach so far was to use the Azure Pricing Calculator, which works great. But wouldn’t it be even better, if you could see the “price tags” of the diverse cloud resources already in your Code Editor? In addition, wouldn’t it be nice to get an overall sum of the whole Terraform configuration? Therefore, I was curious about trying the tool Infracost for estimating the costs.

This post is about applying “Infracost” for following resources:

To verify the costs provided by “Infracost”, the results are getting compared by using the Azure Pricing Calculator.

2. Getting Ready

Before conducting the first usage of Infracost, there are just three things to do:

2.1 Installation and API Key Registration

Follow this quick-start manual for the installation and the API key registration.

2.2 Install the Infracost extension for VS Code

Start your VS Code and go to “Extension”, after that install following:

vs-code

Ensure, that you are using a current version of VS Code, otherwise the installation could fail.

As everyhting is set up, create a new Terminal by selecting “Terminal” - “New Terminal”:

vs-code

Verify, that the installation was done properly - for example use following command to get the installed version:

infracost --version

This should look similar as seen in the picture below:

02_Infracost-Get-Version

2.3. Conducting the Authentication

Type following command in the Terminal, to start the authentication:

infracost --version
Infracost-Login

Check your browser, there you need to confirm the authentication by clicking at “Log in”:

05_Browser

After that, you should get a confirmation:

06_Authentication-Done

3. Estimate the costs of a Windows Virtual Machine

I’d like to start with estimating the costs of an Azure Virtual Machine. The specific Terraform configuration can be downloaded here:

https://github.com/patkoch/iac_terraform_azure/blob/main/vm/win10-20h1/main.tf

3.1 Apply Infracost for estimating the costs

I’ll open the folder, which locates the Terraform configuration file of the virtual machine:

07_VSCode-Open-Folder

This will show the configuration file “main.tf” in the explorer:

07_VSCode-Opened-Win10-Example

Now, I’m going to apply following command in the Terminal - ensure that the current directory is locating the Terraform configuration:

infracost breakdown --path . 

This will start the calculation:

09_Infracost-Win10-Breakdown

Finally, I’ll get the first result for a whole month: the overall total costs are about $ 155.57 - $ 2.92 are related to the Public IP - $ 1.54 can be referred to the S4 Storage.

09_Infracost-Win10-Result

That’s alright, Infracost provided a first result. But I’d like to get more details: I’d like to reveal those resources, which are free.

For that, I’ll conduct following command:

infracost breakdown --show-skipped --path . 
10_Infracost-Win10-Result-Show-Skipped

Adding “–show-skipped” lists the resources, which don’t allocate costs.

In addition, clicking at the Infracost icon leads to following view:

08_VSCode-Costs-Main-Terraform-File

So the total costs are displayed in the explorer of VS Code for a whole Terraform configuration - in addition the costs of the included resources are listed too.

3.2 Verify the costs with Azure Pricing Calculator

That worked well, but I’d like to verify the results, by conducting a second calculation - using the Azure Pricing Calculator:

Azure Pricing Calculator.

3.2.1 Verification: Public IP Address

At first, I’ll verify the Public IP Address with. This reveals the same amount of monthly costs, as it was estimated with Infracost:

19_Azure-Calculator_Public-IP-Address

3.2.2 Verification: Virtual Machine

After that, I’ll do the same with the Virtual Machine: again the result is the same.

08_VSCode-Costs-Main-Terraform-File

3.2.3 Verification: Storage (S4)

This needs a little bit more effort with regard to investigations, but finally also the costs of the OS disk can be determined - again the estimation of Infracost fits.

azure.microsoft.com - Pricing Managed Disks

24_Azure_Disk_S4

Infracost provided the same results compared with the Azure Pricing Calculator.

4. Estimate the costs of an Azure Container Registry and an Azure Kubernetes Cluster

After getting the estimated costs for an Azure Virtual Machine, I’m curious about the content of the price tags of an Azure Kubernetes Cluster and an Azure Container Registry. In addition, I’ll adapt the Azure Kubernetes Cluster resource in the Terraform configuration and Infracost should report the cost difference in contrast to the unchanged Terraform configuration.

The related Terraform configuration can be downloaded here:

https://github.com/patkoch/terraform_aks-acr/blob/main/patricks-aks-acr.tf

4.1 Apply Infracost for estimating the costs

Again, I’ll apply following command for calculating the overall costs:

infracost breakdown --path . 

This leads to following costs:

A total overall of $ 168.77 per month:

13_Infracost-K8s-Breakdown-Costs

To get more details, I’d like to know which resources don’t allocate costs - therefore I run again following command:

infracost breakdown --show-skipped --path . 

This lists following resources as free:

14_Infracost-K8s-Breakdown-Costs-Free

Imagine you are not sure about the final configuration of the Azure Kubernetes Cluster - e.g.: about the amount of nodes in the node pool.

The current Terraform configuration is defined with only one node in the node pool (see snippet below), but that’s maybe not ideal - some additional nodes would be great. But how would be the impact on the overall costs?

  default_node_pool {
    name       = "default"
    node_count = 1
    vm_size    = "Standard_D2_v2"
  }

Infracost provides a functionality with regard to that question. For that, I’m going to save the current Terraform configuration as .json file by conducting following command:

infracost breakdown --path . --format json --out-file infracost-base.json

After that, I get noticed that the file is saved:

15_Infracost-Save-Base-File

Now, I can adapt the file “patricks-aks-acr.tf”: I’ll increase the amount of nodes from one to four:

16_VSCode-Infracost-Base-File

Therfore the Terraform configuration is now changed:

17_VSCode-K8s-Change-4-Nodes

After saving the changed Terraform configuration, I just need to run following command, which refers to the previously generated .json file:

infracost diff --path . --compare-to infracost-base.json

As the content of the command suggests: a diff will be calculated in contrast to the content of the “infracost-base.json” file:

18_VSCode-K8s-Change-Show-Increase

Infracost reports about an increase of the costs of the Kubernetes Cluster resource of $356 - as the amount of nodes was scaled up from one to four. As expected, there are of course no increased costs of the Container Registry.

4.2 Verify the costs with Azure Pricing Calculator

Again, I’d like to verify the results by using the Azure Pricing Calculator.

4.2.1 Verification: Azure Kubernetes Service

I’ll consider the Azure Kubernetes Service (configuration including one node in the node pool). This results to an amount of $97.09 - fits with the calculation of Infracost.

20_Azure-Calculator_AKS

4.2.2 Verification: Azure Container Registry

The next resource to verify would be the Azure Container Registry. This calculation is done in a fast way: $50 are allocated per month - again the result is the same as calculated with Infracost.

21_Azure-Calculator_ACR

4.2.3 Verification: Storage (P10)

This specific costs can be found again at: azure.microsoft.com - Pricing Managed Disks.

22_Azure-Calculator_Disk
23_Azure-Calculator_Default-OS-Disk

Finally, the $21.68 can also be verified.

5. Conclusion

Infracost is a great tool, if you would like to estimate the costs of your cloud resources - defined in Terraform configurations. As a short recap, I would like to list my favorite features:

I’m a huge fan of automating everything, so it would be interesting in addition to add a proper task (API call of Infracost) in the dedicated ADO Pipelines, for provisioning the Azure resources. This is also mentioned in the Infracost manual. In retrospect: it was a lot of fun using Infracost - thanks for that!

References

infracost.io - quick-start

infracost.io

azure.microsoft.com - Pricing Calculator

azure.microsoft.com - Pricing Managed Disks

Terraform

https://github.com/patkoch/iac_terraform_azure/blob/main/vm/win10-20h1/main.tf

https://github.com/patkoch/terraform_aks-acr/blob/main/patricks-aks-acr.tf