Hi, I'm Nicholas 👋

I'm a Senior Platform Engineer

Buy Me A Coffee

Integrate Infracost with Azure DevOps

Date published:

Several few weeks ago, I found out that there is a tool called infracost that allows you to estimate costs for resources deployed using Terraform. Infracost estimates cloud resources for Terraform that let you see the breakdown costs before you make any changes to the infrastructure environment either by using a pull request or locally. The tool can be integrated with other CICD tools such as GitHub Actions, Azure Pipeline and Terraform Cloud/Enterprise. Infracost aims to help companies estimate the cost of every IaC change in cloud infrastructure to determine the cost associated with new resources deployed to the cloud like Azure.

In this post, I will show you how to use the tool and integrate it using the Azure DevOps pipeline to manage your resource.

Running Infracost Locally

To view your resource cost locally, you need to do it download it from here https://www.infracost.io/docs/#quick-start and follow the instruction.

  1. Once downloaded, install the Infracost Visual Studio code extension.

  2. There are 2 ways you can run Infracost locally. I will be showing how you can easily run it against your local directory and export the plan to a JSON format to see the breakdown. Infracost and Terraform both only support JSON format so other formats will not work.

3.1) Next, open a terraform code and go to the directory where you would like to see the cost breakdown and type the following command on your terminal. In this example, I will go to my directory that will deploy Application Gateway and next to the resource you should see a cost highlighted next to it. Click on it and it should open a breakdown window similar to this below.

3.2) This can also be viewed on the terminal and it should give you the same results by running the command.

 infracost breakdown --path.

  1. The other method I will show you is how to use the plan to generate the cost.

Navigate to a terraform code directory and run the following command to create a terraform plan and convert it to JSON. This first command will output the plan and the second command will convert it into JSON format.

terraform init 
terraform plan -out tfplan.binary
terraform show -json tfplan.binary > plan.json
  1. Next, I will use the file that has been converted to see the breakdown cost. I believe this is useful as you could save the plan and use it anytime.

infracost breakdown –path plan.json

Running Infracost cost in Azure DevOps Pipeline?

In this section, I will show how it can be integrated and automated within your DevOps Pipeline.

  1. Go to your Azure DevOps and create a new pipeline and paste the following code. Please remember to update the TF_Root to your directory of the terraform code where you want it to scan the code.
  1. Add secret variables to the pipeline. Click on your project > Pipelines > Edit > Variables, and add the variable for the following.

infracostApiKey: with your Infracost API key as the value, and select ‘Keep this value secret’ - You can get a copy of your API key by login to the infracloud here

  1. After adding your API key run the pipeline and on the generate cost diff stage you should be able to see that it has detected your resource directory.

  1. When the pipeline has been completed you will need to head back to the infracost cloud and go to your organisation and click on the repo you have used to estimate the resource. In this example, you can see the repo I have used as would the same in Azure Repo too.

  1. Click on the see full estimate to view the breakdown cost of your resource. As you can see the estimation of the application gateway is the same cost as in step 3.2. This tells you that when you perform it locally on your via VScode or terminal and use it via CICD integration the cost will be the same.

Conclusion

This is an amazing feature that anyone can use to estimate the cost of resources before deployment. This can be integrated with docker or other methods too, but I hope I have given insights on how it can be used within your pipeline.

References