Friday, 20 February 2026

Azure DevOps Tutorial: Automate Windows VM Deployment Using Terraform

Azure DevOps Tutorial: Automate Windows VM Deployment Using Terraform

vmcreation

Introduction

Deploying infrastructure as code (IaC) is a modern and scalable way to manage your cloud resources. In this guide, we’ll walk you step-by-step on how to create a Windows Virtual Machine in Azure using Terraform.

This tutorial is ideal for beginners and intermediate users who want a repeatable and automated way to spin up Windows VMs in Azure.

What You Will Learn

✔ Install and configure Terraform
✔ Write Terraform code to deploy a Windows VM
✔ Output VM details after deployment
✔ Set up Azure Service Principal

Prerequisites

Before starting, make sure you have:
✔ An Azure Subscription
Terraform installed on your machine
Azure CLI installed (optional but recommended)
✔ Basic understanding of IaC and Azure resources

1. Install Terraform

Download Terraform from the official website:
https://www.terraform.io/downloads
After installation, verify with:
terraform version

2. Configure Azure CLI & Login

Login to Azure using Azure CLI:

az login

(Optional) Set your desired subscription:

az account set --subscription "YOUR_SUBSCRIPTION_NAME"

3. Create Azure Service Principal

A Service Principal gives Terraform permission to provision resources in Azure.

Run:

az ad sp create-for-rbac --name "TerraformSP" --role="Contributor" --sdk-auth

4. Create Terraform Project Folder

Create a new folder:

mkdir azure-windows-vm
cd azure-windows-vm

Create the following files:

main.tf
variables.tf
outputs.tf






5. Define Provider — main.tf

First, lets configure the provider. In Terraform, a provider is a plugin that acts as the bridge between terraform and the target platform. In today's demo the target platform is azure.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
required_version = ">= 1.0.0"
}

provider "azurerm" {
features {}
}

6. Define Variables — variables.tf

variable "location" {
description = "Azure region to deploy resources"
default = "East US"
}

variable "resource_group_name" {
description = "Name of Azure resource group"
default = "rg-windows-vm"
}

variable "vm_admin" {
description = "Admin username for the VM"
default = "azureuser"
}

variable "vm_password" {
description = "Admin password for the VM"
type = string
}

7. Create Windows VM Resource — main.tf

Add this below the provider block:

resource "azurerm_resource_group" "rg" {
name = var.resource_group_name
location = var.location
}

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

resource "azurerm_subnet" "subnet" {
name = "subnet-windows"
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 = "nic-windows"
location = var.location
resource_group_name = azurerm_resource_group.rg.name

ip_configuration {
name = "ipconfig1"
subnet_id = azurerm_subnet.subnet.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.pubip.id
}
}

resource "azurerm_public_ip" "pubip" {
name = "pubip-windows"
location = var.location
resource_group_name = azurerm_resource_group.rg.name
allocation_method = "Dynamic"
}

resource "azurerm_windows_virtual_machine" "winvm" {
name = "winvm-terra"
resource_group_name = azurerm_resource_group.rg.name
location = var.location
size = "Standard_B1ms"
admin_username = var.vm_admin
admin_password = var.vm_password
network_interface_ids = [
azurerm_network_interface.nic.id
]

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

source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2019-Datacenter"
version = "latest"
}
}

8. Output VM Info — outputs.tf

output "vm_public_ip" {
description = "Public IP of the Windows VM"
value = azurerm_public_ip.pubip.ip_address
}

9. Initialize & Apply Terraform

Initialize Terraform:

terraform init

Review plan:

terraform plan -var="vm_password=YourP@ssw0rd!"

Deploy:

terraform apply -var="vm_password=YourP@ssw0rd!"

Type yes when prompted.

10. Verify Deployment

Once complete:

✔ Azure Portal → check Resource Group
✔ Check VM status
✔ Connect to VM using RDP and the output public IP

Success!

You now have a Windows VM running in Azure created entirely with Terraform!

Summary

In this guide we learned how to:

✔ Configure Terraform with Azure
✔ Create a Resource Group and Networking
✔ Deploy a Windows VM
✔ Output public connection details

This method gives you a repeatable, version-controlled way to manage your infrastructure.

For Creating linux vm please refer:
Step-by-step guide: Create Linux vm using terraform


No comments:

Post a Comment

Author Details

Hi, I'm Prashant — a full-time software engineer with a passion for automation, DevOps, and sharing what I learn. I started Py-Bucket to document my journey through tools like Docker, Kubernetes, Azure DevOps, and PowerShell scripting — and to help others navigate the same path. When I’m not coding or writing, I’m experimenting with side projects, exploring productivity hacks, or learning how to build passive income streams online. This blog is my sandbox — and you're welcome to explore it with me. Get in touch or follow me for future updates!

About Me

About the Author

Author

Hi, I'm Prashant — a full-time software engineer with a passion for automation, DevOps, and sharing what I learn. I started Py-Bucket to document my journey through tools like Docker, Kubernetes, Azure DevOps, and PowerShell scripting — and to help others navigate the same path.

When I’m not coding or writing, I’m experimenting with side projects, exploring productivity hacks, or learning how to build passive income streams online. This blog is my sandbox — and you're welcome to explore it with me.

Get in touch or follow me for future updates!