TerraformPilot

DevOps

Terraform for Hyper-V Virtualization

Automate Microsoft Hyper-V with Terraform: taliesins/hyperv provider, VM provisioning, virtual switches, and Windows Server-based hypervisor management.

LLuca Berton1 min read

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

Provider

#
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
}

Virtual Switch

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

Windows Server VM

#
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