Run Azure CLI Commands With Terraform

In this blog post, I will show you how to run Azure CLI commands with Terraform in cases the Azure provider doesn’t support an action.

Provisioners

When working with Microsoft Azure, you might come across a situation where the provider cannot act or even exist for service. This can happen since the community supports the Azure provider, and there is no sync between Microsoft Azure development and the provider modules development cycle.

For the above reasons, Terraform has the concept and provisioners where you can run Terraform and then run commands remotely or locally. In our case, I will use the Azure CLI command in my Terraform configuration.

The Azure CLI command that I’m using is just a sample, but the concept can be used with Azure CLI command or commands.

Configuration

Before you run the code, make sure you login to Azure using the following commands:

az login 
az account set --subscription name

Code

The code below, will create a resource group and run an Azure CLI command.

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "2.44.0"
    }
  }
}

provider "azurerm" {
  features {  }
}

resource "azurerm_resource_group" "rg" {
  name     = "TFRDemo"
  location = "australiasoutheast"

  provisioner "local-exec" {
    command = "az account list --all"

}
}

For more Terraform and Azure articles please visit our Terraform category page.


Posted

in

by