Learn › CI/CD & DevOps

Terraform Basics: Infrastructure as Code for Windows Admins

Describe cloud infrastructure in code with Terraform: providers, resources, variables and outputs, the init–plan–apply workflow, state, and safe team practices.

Advanced 12 min read
By Gopinath Karthikeyan

Building servers by clicking in a cloud portal works once. Building the same thing again – for test, for another region, after a disaster – is where mistakes creep in. Terraform describes infrastructure in code, so you can review it, version it in Git, and recreate it exactly.

Terraform vs Ansible

They complement each other:

  • Terraform creates and removes infrastructure: networks, VMs, load balancers, DNS records, storage accounts.
  • Ansible (or DSC, GPO, scripts) configures what runs inside the servers.

Typical flow: Terraform builds the VM → Ansible configures Windows and the app.

Core concepts

Concept Meaning
Provider Plugin for a platform: azurerm, aws, vsphere, dns…
Resource Something to manage: a VM, a network, a record
Variable Input value, e.g. region or VM size
Output Value to show or pass on, e.g. a VM’s IP
State Terraform’s record of what it manages and the real IDs
Module A reusable package of resources

A small example (Azure resource group and network)

main.tf:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 4.0"
    }
  }
}

provider "azurerm" {
  features {}
}

variable "location" {
  type    = string
  default = "centralindia"
}

resource "azurerm_resource_group" "lab" {
  name     = "rg-winops-lab"
  location = var.location
  tags     = { owner = "windows-team", env = "lab" }
}

resource "azurerm_virtual_network" "lab" {
  name                = "vnet-winops-lab"
  address_space       = ["10.20.0.0/16"]
  location            = azurerm_resource_group.lab.location
  resource_group_name = azurerm_resource_group.lab.name
}

output "vnet_id" {
  value = azurerm_virtual_network.lab.id
}

Notice the references (azurerm_resource_group.lab.name): Terraform works out the order – resource group first, then the network.

The workflow

az login                    # authenticate (for Azure; AWS uses its own credentials)
export ARM_SUBSCRIPTION_ID="<your-subscription-id>"   # azurerm 4.x needs the subscription set
terraform init              # download providers
terraform fmt               # tidy formatting
terraform validate          # syntax check
terraform plan -out tf.plan # show exactly what will change – READ IT
terraform apply tf.plan     # make the changes in the plan
terraform destroy           # remove everything this config manages (lab only!)

The plan is your change record: + create, ~ update in place, -/+ replace (destroy and recreate – watch out for these on servers), - destroy.

Terraform workflow: write code, init, plan, apply, with remote state main.tf in Git Desired infrastructure Git-ல் main.tf தேவையான infrastructure 1 terraform init Downloads providers terraform init Providers download 2 terraform plan + create · ~ change · − destroy terraform plan + create · ~ change · − destroy 3 terraform apply Makes the changes terraform apply மாற்றங்களை செய்யும் 4 Azure / AWS resources Azure / AWS resources 5 State file Remote + locked State file Remote + lock
Write the desired state in code, review the plan, then apply. Terraform records what it manages in a remote, locked state file.தேவையான நிலையை code-ல் எழுதி, plan-ஐ review செய்து, apply செய்யுங்கள். Terraform நிர்வகிப்பதை remote, lock செய்த state file-ல் பதிவு செய்யும்.

State – handle with care

Terraform stores what it built in state (terraform.tfstate). Rules:

  • Never edit it by hand, and never commit it to Git – it can contain secrets.
  • For teams, use a remote backend (e.g. an Azure Storage container or S3 bucket) with locking, so two people can’t apply at once.
  • Changes made by hand in the portal cause drift; the next plan will try to undo them. Make changes through code.

Team practices

  1. Code in Git, changes through pull requests.
  2. CI runs fmt -check, validate and plan on every PR and posts the plan for review.
  3. apply only from the pipeline, after approval, for production.
  4. Use modules for repeated patterns (e.g. “standard Windows VM with tags, disks and backup”).
  5. Pin provider versions.

FAQ

Can Terraform manage existing infrastructure? Yes – import blocks (or terraform import) bring existing resources under management. Do it carefully and check the plan shows no changes afterwards.

What’s next

You’ve completed the CI/CD & DevOps track. Next: Cloud basics for Windows admins.

Practise this topic

Test yourself with free questions on this topic – instant answers and explanations.

Start practice questions

TerraformInfrastructure as CodeCloudDevOpsAdvanced

Gopinath Karthikeyan

Gopinath Karthikeyan – Windows Site Reliability Engineer with about 12 years in enterprise Windows Server infrastructure, and founder of WinOps Academy. YouTube · LinkedIn

More in CI/CD & DevOps