[Avg. reading time: 7 minutes]

Terraform

What is Terraform?

Terraform is a tool used to:

Define and manage cloud infrastructure using code

It works across multiple cloud providers like:

  • Azure
  • AWS
  • Google Cloud

Why Terraform?

Instead of manually creating resources:

  • Clicking in portal
  • Running multiple commands

You write code once and Terraform:

  • Creates everything
  • Updates changes
  • Keeps things consistent

Core Idea

You describe:

  • What you want

Terraform figures out:

  • How to create it

How Terraform Works

  1. Write configuration (code)
  2. Run terraform plan : see what will happen
  3. Run terraform apply : create/update resources

How Idempotency works with Terraform

Terraform State

Terraform state is:

A file that keeps track of what Terraform has created

Terraform needs to know:

  • What already exists
  • What needs to change
  • What to delete

Without state:

  • Terraform would not know current infrastructure
  • It could create duplicates or break things

Where is State Stored?

  • Local file : terraform.tfstate
  • Remote storage : Azure Storage, S3, etc.

Terraform does NOT check Azure directly every time.

It relies on:

State file as the source of truth

Terraform Example: Create a VM in Azure

Step 1: Install Terraform

Download from: https://developer.hashicorp.com/terraform/downloads

Verify:

terraform -version

Step 2: Create Project Folder

terraform-vm/
 ├── main.tf

Step 3: Write Terraform Code

Create main.tf:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  name     = "demo-rg"
  location = "East US"
}

resource "azurerm_virtual_network" "vnet" {
  name                = "demo-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
}

resource "azurerm_subnet" "subnet" {
  name                 = "demo-subnet"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["10.0.1.0/24"]
}

resource "azurerm_network_interface" "nic" {
  name                = "demo-nic"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_linux_virtual_machine" "vm" {
  name                = "demo-vm"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  size                = "Standard_B1s"
  admin_username      = "azureuser"

  network_interface_ids = [
    azurerm_network_interface.nic.id
  ]

  admin_password = "YourPassword123!"

  disable_password_authentication = false

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18_04-lts"
    version   = "latest"
  }
}

Step 4: Login to Azure

az login

Step 5: Initialize Terraform

terraform init

Step 6: Preview Changes

terraform plan

Step 7: Apply (Create Resources)

terraform apply

Type:

yes

Step 8: Destroy Resources

terraform destroy

#azure #devops #terraformVer 6.0.25

Last change: 2026-04-21