# Shift Left: Azure Cost Management with Infracost

# Infracost signup

For this article, we are only utilising the free version of Infracost. This is called Infracost CI/CD, and can be found under their Pricing window on [https://www.infracost.io/pricing/](https://www.infracost.io/pricing/)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1742029800703/a2071b3a-d3a6-451a-a3f8-0561fb1ef541.png align="center")

You can see their other services and features on this page. For this article, we are focusing only on the free plan. We will be limited to 1,000 monthly runs to scan our IaC, which is enough for a startup or small IaC estate.

First, select the **Sign up / Log in** button at the top right of the screen. From this screen, sign up for Infracost using one of the multiple methods. Once you have verified your signup, you will be presented with the CI/CD integration window, which we will follow up with later in this post.

# Executing Infracost locally

We will need to install Infracost locally first to run it. This can be very powerful with the shift left mentality/approach, as you can run cost estimates before checking in the IaC to your repo.

If you are running macOS and use Brew to manage package installations, you can install Infracost using the following command:

```bash
brew install infracost
```

Or, if you are running Windows as your local OS, download the latest installation using this [link](https://github.com/infracost/infracost/releases/latest/download/infracost-windows-amd64.zip).

When ready, using the terminal, enter the following command to authenticate to InfraCost:

```bash
infracost auth login
```

Now you are authenticated locally; you can execute InfraCost commands in the root directory of your IaC, which contains your Azure resources. This is an example of an Azure resource in Terraform:

```json
terraform {
  required_version = ">= 1.0.0"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.54.0"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "rg-example"
  location = "eastus"
}

resource "azurerm_mssql_server" "example" {
  name                         = "mssqlserverexample123"
  resource_group_name          = azurerm_resource_group.example.name
  location                     = azurerm_resource_group.example.location
  version                      = "12.0"
  administrator_login          = "adminuser"
  administrator_login_password = *****
  minimum_tls_version          = "1.2"
}

resource "azurerm_mssql_database" "example" {
  name      = "mssqldatabaseexample"
  sku_name  = "Basic"
  collation = "SQL_Latin1_General_CP1_CI_AS"
  server_id = azurerm_mssql_server.example.id
}

output "mssqlserver_name" {
  value = azurerm_mssql_server.example.name
}

output "mssqldatabase_name" {
  value = azurerm_mssql_database.example.name
}
```

I execute the following command in the directory where I have my IaC:

```bash
infracost breakdown --path .
```

This generates the following estimates for me:

```bash
INFO Autodetected 1 Terraform project across 1 root module
INFO Found Terraform project main at directory .

Project: main

 Name                               Monthly Qty  Unit            Monthly Cost   
                                                                                
 azurerm_mssql_database.example                                                 
 ├─ Compute (BASIC)                         730  hours                  $4.90   
 ├─ Long-term retention (RA-GRS)  Monthly cost depends on usage: $0.05 per GB   
 └─ PITR backup storage (RA-GRS)  Monthly cost depends on usage: $0.20 per GB   
                                                                                
 OVERALL TOTAL                                                         $4.90 

*Usage costs can be estimated by updating Infracost Cloud settings, see docs for other options.

──────────────────────────────────
3 cloud resources were detected:
∙ 1 was estimated
∙ 2 were free

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Project                                            ┃ Baseline cost ┃ Usage cost* ┃ Total cost ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━╋━━━━━━━━━━━━┫
┃ main                                               ┃            $5 ┃           - ┃         $5 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━━━━┛
```

# Integrating Infracost to GitHub

Now we return to the InfraCost dashboard ([https://dashboard.infracost.io/](https://dashboard.infracost.io/)). Select **GitHub** as the source control integration and select **Next**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1742376654593/7dbf8082-29fc-453e-b847-d7b9d360a27d.png align="center")

You will then need to select the **Connect** button to start the integration with GitHub.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1742376663929/77be8862-3a22-4fb4-a373-9e4347cd4327.png align="center")

The **Install Infracost** window appears, asking you to select an organisation to which you want to install this.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1742376673750/8cd36595-13e3-4097-af0d-984ce8b77fed.png align="center")

You can choose all repositories or a select number when an organisation is selected. For this article, I selected **All repositories** and proceeded by selecting **Install**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1742376691800/84e7e561-b34f-47ea-a4bd-83f68831246b.png align="center")

Finally, you can see the repositories in the Infracost dashboard; here, you can select the repositories you want to onboard to Infracost.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1742376702219/1759aa69-f4c9-4a87-a709-0560784c06bd.png align="center")

Once selected, you are asked to generate a pull request. You can either create a pull request with the example Terraform Infracost provides to confirm the integration or raise a legitimate PR which contains the Terraform you produced.

When a PR is generated, you will see Infracost comment on the PR about resource costs. But please be aware that Infracost does not support all resource types yet. As such, you must review what is not supported if the output suggests it cannot find a price.

%%[buymeacoffe-butyellow]
