🖥️ From Zero to Hero: Terraform for Azure - Part 1

Introduction
Terraform is a powerful Infrastructure as Code (IaC) tool that enables you to define, provision, and manage Azure resources using declarative configuration files. This blog series, Zero to Hero: Terraform for Azure, will take you through Terraform from the basics to advanced topics, using hands-on examples tailored for Azure.
In this first post, we’ll focus on setting up your development environment and deploying your first resource on Azure using Terraform.
Prerequisites
Before we start, you need:
- An Azure account (a free account works fine)
- Basic knowledge of Azure (nice to have, but not required)
We’ll cover multiple setup options:
- Local installation (best for long-term development)
- Using Docker (best for isolated environments)
1. Setting Up the Development Environment
Option 1: Local Installation
For local development, you need to install:
Terraform
Download Terraform for your OS.
Verify installation:
terraform version
Azure CLI
Install Azure CLI for authentication and resource management:
Windows:
winget install --id Microsoft.AzureCLI -e
macOS:
brew install azure-cli
Linux (Debian-based):
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
Verify installation:
az version
Visual Studio Code
- Install VS Code.
- Install the Terraform extension from the Marketplace.
Option 2: Using Docker
If you prefer an isolated environment, you can use Terraform inside a Docker container.
Install Docker.
Run Terraform inside a container:
docker run --rm -it hashicorp/terraform:latest version
To work with local files, mount a volume:
docker run --rm -it -v $(pwd):/app -w /app hashicorp/terraform:latest init
2. Authenticating with Azure
To deploy resources, Terraform needs to authenticate with Azure.
Login using Azure CLI
Open a terminal and log in:
az login
If using multiple subscriptions, set the desired one:
az account set --subscription "<subscription-id>"
Using a Service Principal (For Automation)
For automation in CI/CD pipelines:
az ad sp create-for-rbac --name terraform-sp --role Contributor --scopes /subscriptions/<subscription-id> --sdk-auth
Copy the JSON output for use in Terraform.
3. Writing Your First Terraform Configuration
Let’s create a simple Terraform script to deploy an Azure Storage Account.
Step 1: Initialize a Terraform Project
Create a new directory:
mkdir terraform-azure && cd terraform-azure
Create a new Terraform file:
touch main.tf
Step 2: Define the Terraform Configuration
Open main.tf
in VS Code and add the following:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example" {
name = "terraform-rg"
location = "West Europe"
}
Step 3: Initialize Terraform
Run:
terraform init
Step 4: Preview Changes
terraform plan
Step 5: Apply the Configuration
terraform apply -auto-approve
Once completed, your resource group will be created in Azure.
4. Cleaning Up and Next Steps
To remove the deployed resources:
terraform destroy -auto-approve
Next Steps
In the next post, we’ll explore Terraform state management and remote backends.
Stay tuned for more hands-on Terraform learning! 🚀