Last Updated on August 7, 2025 by Arnav Sharma
Terraform is an Infrastructure as Code (IaC) tool by HashiCorp that enables you to define and provision infrastructure using a high-level configuration language. It allows you to create, update, and manage Azure resources in a consistent and repeatable manner. This guide will help you get started with Terraform on Azure.
Prerequisites
Before you begin, ensure you have the following:
- Azure Account: An active Azure subscription.
- Terraform Installed: You can download Terraform from the official website.
- Azure CLI Installed: You can download the Azure CLI from the official website.
Step 1: Install Terraform and Azure CLI
Terraform:
- Download the Terraform binary from the Terraform website.
- Extract the downloaded file and move it to a directory included in your system’s PATH.
Azure CLI:
- Follow the instructions for your operating system from the Azure CLI installation guide.
Step 2: Authenticate with Azure
Use the Azure CLI to authenticate your Terraform configuration.
az login
This command opens a web browser where you can sign in with your Azure credentials.
Step 3: Create a Service Principal
A Service Principal is an identity created for use with applications, hosted services, and automated tools to access Azure resources.
az ad sp create-for-rbac --name terraform --role="Contributor" --scopes="/subscriptions/{your-subscription-id}"
Replace {your-subscription-id} with your actual Azure subscription ID. This command will output JSON containing appId, displayName, password, and tenant. Save this information securely, as you’ll need it for Terraform configuration.
Step 4: Understanding Providers
In Terraform, a provider is a plugin that Terraform uses to interact with APIs of cloud providers, SaaS providers, and other services. Providers enable Terraform to manage and provision resources. Each provider has its own configuration and set of resources.
How to Find Providers
You can find providers on the Terraform Registry. The registry contains information about various providers, including the Azure provider, AWS provider, and many others. To find a specific provider:
- Go to the Terraform Registry.
- Use the search bar to find a provider (e.g., type “Azure” to find the Azure provider).
- Click on the provider to see its documentation, including available resources, data sources, and configuration options.
Step 5: Finding Terraform Code Examples
To find Terraform code examples for specific Azure resources, using the Azure Resource Manager, you can:
- Terraform Registry: Browse the Terraform Registry. Each provider page often includes example configurations for various resources.
- GitHub: Search for Terraform repositories on GitHub. Many users and organizations share their Terraform configurations. Use keywords like “Terraform Azure” to find relevant repositories.
- Official Documentation: The Terraform documentation and the Azure Provider documentation provide detailed examples and usage instructions.
- Community Blogs and Tutorials: Many community members share their experiences and configurations through blogs and tutorials. A quick web search for “Terraform Azure example” will yield numerous results.
Step 6: Set Up Terraform Configuration
Create a new directory for your Terraform configuration files and navigate into it.
mkdir terraform-azure
cd terraform-azure
Create a file named main.tf and open it in your favorite text editor. Add the following configuration to define the Azure provider, create a resource group, and deploy a storage account:
# main.tf
provider "azurerm" {
features {}
subscription_id = "<your-subscription-id>"
client_id = "<your-appId>"
client_secret = "<your-password>"
tenant_id = "<your-tenant-id>"
}
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "East US"
}
resource "azurerm_storage_account" "example" {
name = "examplestoracct"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
account_tier = "Standard"
account_replication_type = "LRS"
tags = {
environment = "Terraform Demo"
}
}
Replace <your-subscription-id>, <your-appId>, <your-password>, and <your-tenant-id> with the values obtained from the Service Principal creation step.
Step 7: Initialize Terraform
Initialize your Terraform configuration to install the necessary plugins.
terraform init
Step 8: Plan and Apply the Configuration
Use the terraform plan command to create an execution plan and ensure the configuration is correct.
terraform plan
If the plan looks good, apply the configuration to create the resource group and storage account in Azure.
terraform apply
Terraform will prompt you to confirm the action. Type yes to proceed.
Step 9: Verify the Deployment
Log in to the Azure Portal and navigate to “Resource Groups” to see the newly created resource group named example-resources and the storage account named examplestoracct.
Step 10: Clean Up Resources
To avoid incurring costs for resources you no longer need, use the terraform destroy command to remove the resources.
terraform destroy
Terraform will prompt you to confirm the action. Type yes to proceed.