Originally published at DevStackHub β Read the full in-depth guide with advanced modular setups and production tips.
Manual infrastructure provisioning in the Azure Portal quickly leads to configuration drift, deployment errors, and zero auditability. Infrastructure as Code (IaC) with Terraform provides a declarative, repeatable, and scalable way to manage cloud topology.
Here is the 5-step blueprint to provision enterprise-ready Azure infrastructure using HashiCorp Terraform.
1. Configure the Azure Provider (provider.tf)
Define the required provider versions and initialize the azurerm provider block:
terraform {
required_version = ">= 1.5.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.90.0"
}
}
}
provider "azurerm" {
features {}
}
- Define Input Variables (variables.tf) Keep your configuration modular and dynamic by avoiding hard-coded values:
variable "resource_group_name" {
type = string
description = "Name of the target resource group"
default = "rg-production-eastus"
}
variable "location" {
type = string
description = "Target Azure region"
default = "eastus"
}
variable "vnet_address_space" {
type = list(string)
description = "Address prefix CIDR block for the Virtual Network"
default = ["10.0.0.0/16"]
}
- Provision Core Networking & Resources (main.tf) Declare your primary Resource Group, Virtual Network (VNet), and Subnet:
# Create Resource Group
resource "azurerm_resource_group" "rg" {
name = var.resource_group_name
location = var.location
tags = {
Environment = "Production"
ManagedBy = "Terraform"
}
}
# Create Isolated Virtual Network
resource "azurerm_virtual_network" "vnet" {
name = "vnet-app-prod"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
address_space = var.vnet_address_space
}
# Create Subnet for Services
resource "azurerm_subnet" "subnet" {
name = "snet-web-prod"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = ["10.0.1.0/24"]
}
- Format & Validate Declarative Files Before applying changes, validate syntax and enforce standard formatting:
# Enforce canonical HCL formatting
terraform fmt
# Initialize providers and local state
terraform init
# Validate configuration syntax
terraform validate
- Generate Plan & Apply Infrastructure Review speculative execution before committing changes to Azure:
# Generate and inspect the execution plan
terraform plan -out=tfplan
# Apply the execution plan
terraform apply tfplan
Key Best Practices
Remote State: Always store your .tfstate file in a secure Azure Blob Storage container with distributed locking rather than local disk.
Avoid Plain-Text Secrets: Pass sensitive values via environment variables (TF_VAR_variable_name) or Azure Key Vault references.
π Read the complete tutorial: Terraform on Azure: 5 Complete Steps to Provision Infrastructure on DevStackHub.
Top comments (1)
Are you currently managing your Azure infrastructure using Terraform, Bicep, or standard ARM templates? Whatβs your preferred way to handle remote state locking in your team?