Infracost : Cost estimates of the IaC Resources for Azure

Ajinkya Bhabal
FAUN — Developer Community 🐾
5 min readJun 30, 2021

--

Credits: Infracost

Introduction

As we are moving forward to automate the infrastructure deployment, Terraform has become the top choice among the DevOps and Software Engineers as an IaC tool. There are so many Continuous Integration/Continuous Deployment tools available in the Market for Automate Infrastructure deployment. Some of them are Azure DevOps, Jenkins, Atlantis, GitHub Action, GitLab, Circle CI.

The common workflow of Infrastructure as code deployment is after following the IaC best practices, the code will be stored in VCS and Code review operation will be done by the other Contributors, then it will be deployed in the public cloud. So the Contributors have to find cost estimates of the Infrastructure through external estimates tool, which involves lots of manual work.

So to avoid this Terraform has been providing cloud cost estimates feature in its paid plan.

A few days ago, I found this Awesome open-source tool named “Infracost” which supports over 140 resources, and the best thing is it works perfectly with the Terragrunt.

To get a basic introduction of the “Infracost” tool and a quick walkthrough of the CLI Tool, Please visit the below reference.

In this article, I will demonstrate a tutorial of “Infracost” integration with the two CI/CD Tools.

1. Infra Cost Estimation Review with Atlantis

Atlantis is Terraform Pull Request Automation Tool.

Here we will try out “Infracost” tool in the Atlantis Configured GitHub used for Terraform Deployment.

First, take a quick look at the Customized Atlantis Docker Image for the “Infracost”.

The above Atlantis Container Image contains the following elements,

  1. Install Azure CLI Tool.
  2. Install Terraform v0.15.5 Package.
  3. Install Infracost Tool.
  4. Configure and Start Atlantis Server

Next, Configure Repo Level repos.yaml file which allows us to instruct Atlantis on the structure of your repo and set custom workflows.

Now you can deploy this container image in Azure Kuberenetes Service and exposed with Load balancer Service mapping default Atlantis Port 4141 to 80.

Now we will create Pull request for Terraform Deployment, as you can see from below image Atlantis plan command is commented. So behind the scenes it runs the Terraform Plan command.

At the end of Plan output we can see the Cost estimates calculated by the Infracost tool.

Before deploying the resources we can set the approval gates, this feature is already provided by the Atlantis.

Next we will perform Atlantis Apply operation and all the Azure Resources will be deployed.

After the Apply Operation completes the pull request will automatically merged with the Master Branch.

In the Azure Portal, We can see the Deployed Resource.

2. Infra Cost Estimation Review with GitHub Actions

GitHub Actions is CI/CD tool used for Build, Test and Deploy your code from GitHub.

Let’s Start with the configuring GitHub Actions Pipeline yml file for customizing the workflow of Terraform along with “Infracost”.

We will be using GitHub Secrets to Store our Azure Authentication Credentials and Infracost API Key.

After configuring GitHub Actions we will create pull request where other contributors can check resource deployment along with Cost estimates using Infracost.

Here we can see the Infracost has sent the Cost estimates of all azure terraform resources in the comment.

Here also we set the approval using branch protection rule, where atleast one reviewer is needed to approve the Resource deployment.

Then we will Merge the pull request to the main branch.

In the pipeline configuration file, we have set the condition about when we merge the Pull request to the main branch, then Terraform Apply command will be executed.

Finally we can see the deployed resource in Azure Portal.

It’s always great to learn and explore something new, in IaC there are still so many blockades that we need to solve to get higher efficiency in the automation process.

If you’d like to read more about IaaC Tools or Popular Terraform wrappers, Just let me know in the comments. Thanks for reading!

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

--

--