TerraformPilot

DevOps

Terraform Hyper-V Provider: Automate VMs & Switches

Use the Terraform Hyper-V provider to provision VMs, virtual switches, and manage Windows Server infrastructure with code.

LLuca Berton1 min read

Terraform for Hyper-V works through the community taliesins/hyperv provider, which talks WinRM to a Hyper-V host to create VMs, virtual switches, and storage. Hyper-V is Microsoft's hypervisor, embedded in Windows Server and Windows 11 Pro/Enterprise — a pattern of choice when an enterprise wants a non-VMware on-prem virtualization stack.

Terraform Hyper-V Provider Configuration

#

The taliesins/hyperv provider block tells Terraform how to reach the Hyper-V host over WinRM — set the host, credentials, and port before defining any Hyper-V resources.

terraform {
  required_providers {
    hyperv = {
      source  = "taliesins/hyperv"
      version = "~> 1.2"
    }
  }
}
 
provider "hyperv" {
  user            = var.username
  password        = var.password
  host            = var.hyperv_host
  port            = 5986
  https           = true
  insecure        = false
  use_ntlm        = true
  cacert_path     = var.cacert_path
}

Terraform Hyper-V Virtual Switch Configuration

#

A virtual switch in Hyper-V connects VMs to the host network. The hyperv_network_switch resource with Terraform creates and manages external, internal, and private switches. External switches bind to a physical NIC and bridge VM traffic to the host network or upstream:

resource "hyperv_network_switch" "external" {
  name                              = "vSwitch-External"
  switch_type                       = "External"
  net_adapter_names                 = ["Ethernet"]
  allow_management_os               = true
  enable_iov                        = false
}

Terraform Hyper-V Windows Server VM Provisioning

#

The hyperv_machine_instance resource provisions Windows VMs on Hyper-V. Reference the virtual switch created above, set generation 2 for UEFI and Secure Boot, and configure CPU, memory (static or dynamic), and VHDX storage paths. This example attaches the external switch and uses dynamic memory for flexible resource allocation:

resource "hyperv_machine_instance" "winsrv" {
  name                   = "tf-winsrv-1"
  generation             = 2
  processor_count        = 4
  memory_startup_bytes   = 4 * 1024 * 1024 * 1024
  static_memory          = false
  dynamic_memory         = true
  memory_minimum_bytes   = 2 * 1024 * 1024 * 1024
  memory_maximum_bytes   = 8 * 1024 * 1024 * 1024
 
  network_adaptors {
    name        = "ext0"
    switch_name = hyperv_network_switch.external.name
  }
 
  hard_disk_drives {
    controller_type     = "Scsi"
    path                = "C:\\Hyper-V\\tf-winsrv-1\\os.vhdx"
    controller_number   = 0
    controller_location = 0
  }
}

Best Practices

#
  • WinRM with HTTPS + a real cert — never insecure NTLM over HTTP.
  • Generation 2 VMs for UEFI + Secure Boot.
  • Dynamic memory for general workloads, static for SQL Server / latency-sensitive.
  • Track VHDX paths in Terraform — orphan VHDX files are the #1 storage waste.
#
#Terraform#Hyper-V#Windows Server#Virtualization#WinRM

Share this article