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.
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
- Code in Git, changes through pull requests.
- CI runs
fmt -check,validateandplanon every PR and posts the plan for review. applyonly from the pipeline, after approval, for production.- Use modules for repeated patterns (e.g. “standard Windows VM with tags, disks and backup”).
- 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 questionsTerraformInfrastructure as CodeCloudDevOpsAdvanced