0
0
TerraformHow-ToBeginner · 3 min read

How to Use Azure Provider in Terraform: Syntax and Example

To use the azure provider in Terraform, declare it in your configuration with the provider block specifying azurerm. Then authenticate using environment variables or a service principal to manage Azure resources.
📐

Syntax

The provider block tells Terraform to use Azure's cloud services. You specify azurerm as the provider name. Inside, you can set the features block which is required but can be empty.

Authentication is usually done outside the provider block using environment variables or a service principal.

terraform
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0"
    }
  }
}

provider "azurerm" {
  features {}
}
💻

Example

This example shows how to configure the Azure provider and create a simple resource group in Azure. It assumes you have set environment variables for authentication.

terraform
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "East US"
}
Output
azurerm_resource_group.example: Creating... azurerm_resource_group.example: Creation complete after 2s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources]
⚠️

Common Pitfalls

  • Forgetting to include the features {} block inside the provider "azurerm" block causes errors.
  • Not setting up authentication properly leads to authorization failures. Use environment variables like ARM_CLIENT_ID, ARM_CLIENT_SECRET, ARM_TENANT_ID, and ARM_SUBSCRIPTION_ID.
  • Using an outdated provider version can cause incompatibility with Azure APIs.
terraform
provider "azurerm" {
  // Missing features block - this will cause an error
}

// Correct way:
provider "azurerm" {
  features {}
}
📊

Quick Reference

Remember these key points when using the Azure provider in Terraform:

  • Always declare azurerm in required_providers.
  • Include an empty features {} block in the provider.
  • Authenticate using environment variables or a service principal.
  • Use the latest stable provider version for best compatibility.

Key Takeaways

Declare the Azure provider with an empty features block to avoid errors.
Authenticate Terraform to Azure using environment variables or a service principal.
Specify the provider version to ensure compatibility with Azure APIs.
Always use the latest stable version of the azurerm provider.
Check for common mistakes like missing features block or improper authentication.