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
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:
- Azure Virtual Machine
- Azure Container Registry
- Azure Kubernetes Service
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:
- Installation of Infracost and API Key Registration
- Install the Infracost extension for VS Code
- Conducting the Authentication
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:
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”:
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:
2.3. Conducting the Authentication
Type following command in the Terminal, to start the authentication:
infracost --version
Check your browser, there you need to confirm the authentication by clicking at “Log in”:
After that, you should get a confirmation:
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:
This will show the configuration file “main.tf” in the explorer:
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:
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.
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 .
Adding “–show-skipped” lists the resources, which don’t allocate costs.
In addition, clicking at the Infracost icon leads to following view:
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:
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:
3.2.2 Verification: Virtual Machine
After that, I’ll do the same with the Virtual Machine: again the result is the same.
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
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:
- Container Registry: $ 50.00
- Kubernetes Cluster: $ 97.09 + $ 21.68 (P10 Storage)
A total overall of $ 168.77 per month:
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:
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:
Now, I can adapt the file “patricks-aks-acr.tf”: I’ll increase the amount of nodes from one to four:
Therfore the Terraform configuration is now changed:
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:
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.
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.
4.2.3 Verification: Storage (P10)
This specific costs can be found again at: azure.microsoft.com - Pricing Managed Disks.
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:
- Displaying the costs of the configuration in the VS Code Explorer: it provides a quick overview of the costs of the whole configuration.
- Easy and fast estimation of the costs of a whole configuration, including the details all resources which allocates costs. E.g.: IP Address, Virtual Machine Instance, Storage S4:
- Great option to compare differences between configurations:
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
azure.microsoft.com - Pricing Calculator
azure.microsoft.com - Pricing Managed Disks
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