🏗️ Day 20: Infrastructure as Code - Hands-On Guide

Terraform and ARM Templates (Windows)

Duration: 4 hours | Module: Release Management

Lab 1: Install Terraform on Windows 🔧

Objective: Install Terraform CLI
Time: 15 minutes

Exercise 1.1: Install Terraform

1 Download Terraform
  • Go to: https://www.terraform.io/downloads
  • Download Windows AMD64 ZIP file
  • Extract terraform.exe to C:\terraform
2 Add to PATH
  • Press Windows + R, type sysdm.cpl
  • Advanced → Environment Variables
  • Edit PATH → Add C:\terraform
  • Click OK
3 Verify Installation
# Open new PowerShell window terraform version Terraform v1.7.0 on windows_amd64
✅ Checkpoint: Terraform installed and working

Lab 2: Create First Terraform Resource 🎯

Objective: Create Azure resource group with Terraform
Time: 30 minutes

Exercise 2.1: Create Terraform Files

1 Create Terraform Directory
cd $env:USERPROFILE\Desktop mkdir terraform-demo cd terraform-demo
2 Create main.tf
notepad main.tf

Add Terraform configuration:

# Configure Azure Provider terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "~> 3.0" } } } provider "azurerm" { features {} } # Create Resource Group resource "azurerm_resource_group" "main" { name = "rg-terraform-demo" location = "East US" tags = { Environment = "Development" ManagedBy = "Terraform" Owner = "DevOps Team" } }

Save and close

3 Initialize Terraform
# Initialize Terraform (downloads Azure provider) terraform init Initializing the backend... Initializing provider plugins... - Finding hashicorp/azurerm versions matching "~> 3.0"... - Installing hashicorp/azurerm v3.85.0... - Installed hashicorp/azurerm v3.85.0 Terraform has been successfully initialized!
4 Login to Azure
az login You have logged in. Now let us find all the subscriptions to which you have access...
5 Plan Infrastructure Changes
terraform plan Terraform will perform the following actions: # azurerm_resource_group.main will be created + resource "azurerm_resource_group" "main" { + id = (known after apply) + location = "eastus" + name = "rg-terraform-demo" + tags = { + "Environment" = "Development" + "ManagedBy" = "Terraform" + "Owner" = "DevOps Team" } } Plan: 1 to add, 0 to change, 0 to destroy.
6 Apply Changes
terraform apply Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes azurerm_resource_group.main: Creating... azurerm_resource_group.main: Creation complete after 3s Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
7 Verify in Azure Portal
  • Open Azure Portal
  • Go to Resource Groups
  • See rg-terraform-demo created!
  • Check tags are applied
✅ Checkpoint: First infrastructure resource created with Terraform!

Lab 3: Create Complete Infrastructure 🏗️

Objective: Create Storage Account and Virtual Network
Time: 40 minutes

Exercise 3.1: Add Storage Account

1 Update main.tf
notepad main.tf

Add storage account resource:

# Create Storage Account resource "azurerm_storage_account" "main" { name = "stcalcdemo${random_string.suffix.result}" resource_group_name = azurerm_resource_group.main.name location = azurerm_resource_group.main.location account_tier = "Standard" account_replication_type = "LRS" tags = { Environment = "Development" ManagedBy = "Terraform" } } # Generate random suffix for unique storage name resource "random_string" "suffix" { length = 6 special = false upper = false } # Output storage account name output "storage_account_name" { value = azurerm_storage_account.main.name }

Save and close

2 Add Random Provider

Update terraform block at top of main.tf:

terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "~> 3.0" } random = { source = "hashicorp/random" version = "~> 3.0" } } }
3 Run Terraform
# Re-initialize for new provider terraform init # Plan changes terraform plan Plan: 2 to add, 0 to change, 0 to destroy. (random_string + storage_account) # Apply terraform apply -auto-approve random_string.suffix: Creating... random_string.suffix: Creation complete azurerm_storage_account.main: Creating... azurerm_storage_account.main: Creation complete Apply complete! Resources: 2 added, 0 changed, 0 destroyed. Outputs: storage_account_name = "stcalcdemoabc123"
✅ Checkpoint: Multiple resources created with Terraform

Lab 4: Terraform in Azure Pipeline 🔗

Objective: Automate infrastructure deployment in pipeline
Time: 45 minutes

Exercise 4.1: Create Terraform Pipeline

1 Initialize Git Repository
git init git add . git commit -m "Initial Terraform code"
2 Create Pipeline YAML
notepad azure-pipelines-terraform.yml

Complete Terraform pipeline:

# Terraform Infrastructure Pipeline trigger: paths: include: - terraform/** - '*.tf' pool: vmImage: 'ubuntu-latest' variables: terraformVersion: '1.7.0' workingDirectory: '$(System.DefaultWorkingDirectory)' stages: - stage: TerraformValidate displayName: 'Validate Terraform' jobs: - job: Validate steps: - task: TerraformInstaller@0 displayName: 'Install Terraform' inputs: terraformVersion: $(terraformVersion) - task: TerraformTaskV4@4 displayName: 'Terraform Init' inputs: provider: 'azurerm' command: 'init' workingDirectory: $(workingDirectory) backendServiceArm: 'Azure-Subscription' - task: TerraformTaskV4@4 displayName: 'Terraform Validate' inputs: provider: 'azurerm' command: 'validate' workingDirectory: $(workingDirectory) - stage: TerraformPlan displayName: 'Plan Infrastructure' dependsOn: TerraformValidate jobs: - job: Plan steps: - task: TerraformInstaller@0 inputs: terraformVersion: $(terraformVersion) - task: TerraformTaskV4@4 displayName: 'Terraform Init' inputs: provider: 'azurerm' command: 'init' workingDirectory: $(workingDirectory) backendServiceArm: 'Azure-Subscription' - task: TerraformTaskV4@4 displayName: 'Terraform Plan' inputs: provider: 'azurerm' command: 'plan' workingDirectory: $(workingDirectory) environmentServiceNameAzureRM: 'Azure-Subscription' - stage: TerraformApply displayName: 'Apply Infrastructure' dependsOn: TerraformPlan jobs: - deployment: ApplyTerraform displayName: 'Apply Terraform Changes' environment: 'Infrastructure' strategy: runOnce: deploy: steps: - checkout: self - task: TerraformInstaller@0 inputs: terraformVersion: $(terraformVersion) - task: TerraformTaskV4@4 displayName: 'Terraform Init' inputs: provider: 'azurerm' command: 'init' workingDirectory: $(workingDirectory) backendServiceArm: 'Azure-Subscription' - task: TerraformTaskV4@4 displayName: 'Terraform Apply' inputs: provider: 'azurerm' command: 'apply' workingDirectory: $(workingDirectory) environmentServiceNameAzureRM: 'Azure-Subscription'

Save and close

3 Push to Azure Repos
git add . git commit -m "Add Terraform pipeline" git remote add origin YOUR_AZURE_REPOS_URL git push -u origin main
4 Create Environment
  • Go to Pipelines → Environments
  • Create new: Infrastructure
  • Add approval for infrastructure changes
5 Run Pipeline
  • Create pipeline in Azure DevOps
  • Select terraform YAML file
  • Watch three stages:
  • ✅ Validate - Check Terraform syntax
  • ✅ Plan - Show what will be created
  • ✅ Apply - Create infrastructure (needs approval)
✅ Checkpoint: Infrastructure provisioned via automated pipeline

🎯 Lab Summary

What You've Accomplished:

  • ✅ Installed Terraform CLI on Windows
  • ✅ Created first Terraform configuration
  • ✅ Used terraform init, plan, apply workflow
  • ✅ Created Azure resources with Terraform
  • ✅ Automated Terraform in Azure Pipeline
  • ✅ Understood Infrastructure as Code concept

Terraform Workflow:

  1. Write: Create .tf files
  2. Init: Download providers
  3. Plan: Preview changes
  4. Apply: Create infrastructure
  5. Verify: Check in Azure Portal

📝 Terraform Quick Reference

# Initialize terraform init # Format code terraform fmt # Validate syntax terraform validate # Plan changes terraform plan # Apply changes terraform apply # Apply without prompt terraform apply -auto-approve # Show current state terraform show # List resources terraform state list # Destroy all resources terraform destroy

🎉 Day 20 Complete!

You've mastered Infrastructure as Code!

Ready for Day 21: Monitoring & Logging