Estimate your Infrastructure-as-code costs before deploying
I recently stumbled on a very useful API called Infracost, that allows you to estimate the cost of the resources that will be deployed by your Terraform code across Azure, AWS and GCP. You can get the cost estimates locally or even better, you can integrate with your CI/CD pipelines so with every pull request you can understand your cloud costs upfront and see how much the new changes will affect your bill before approving this pull request.
Setting up Infracost locally
Since I’m using Windows, I will use Chocolatey to install Infracost.
choco install infracost
Then, I need to register to get an API key.
Now, we are ready to try out the Infracost API. I have a sample Terraform code to create two virtual machines; a domain controller and a member server to joing this domain.
Running the Infracost CLI to get an estimate of the costs of the resources that will be created by our Terraform code.
infracost breakdown --path . --show-skipped
We can see it nicely breaks down the cost of our two virtual machines (calculating compute and disk) and public Ip address. Also at the end, it shows the free resources like virtual network, subnets and resources that are not yet supported by the API.
One other cool thing Infracost allows us to do is to create a baseline of our costs and then compare if we make any changes in the future to understand the drift that will happen in cost.
infracost breakdown --path . --format json --out-file infracost-base.json
Having our baseline saved, I’m going to modify the domain controller VM size and add a storage account to my Terraform code.
Running Infracost again to compare the changed against our baseline, we can see a breakdown of the changes to our infrastructure (old cost Vs new cost).
New storage account costs
Virtual machine size change
Monthly cost change
Integrating Infracost with CI/CD
Running Infracost is very useful to check your local code costs, but it can be more useful if it can integrate with our CI/CD pipelines for a more streamlines approach. Currently is has a variety of integrations with different platforms. I will try the GitHub actions integration.
First step, is to generate a repository secret for Infracost.
The, we need to add an action yaml definition. The documentation are pretty good with a template you can use, you only need to change the location of your terraform code. Basically the GitHub action will do the following:
- Authenticate to the Infracost API using the repository secret
- Create a baseline cost estimate and saves it in a temporary file
- Checks out the pull request branch
- Create a diff with the cost changes compared to the baseline file
After setting up our GitHub action, I will submit a new pull request with changes to the virtual machine size and OS disk type.
Once the pull request is submitted and GitHub action runs, I can see that Infracost shows me the cost changes I should expect if I approve the pull request with details on what has changed.
You May Also Like
Adding a user interface to your ARM templates
Have you ever deployed a complex ARM template with multiple parameters …
Improving your security posture with Governance Rules
Microsoft Defender for Cloud analyzes your resources on a regular …
SSH into your Azure Arc-enabled servers from anywhere
A new capability has been introduced for Azure Arc-enabled servers …