AVD - VMs are not adding to a hostpools

Hi there,
i have a code that should create AVD with VMS and add those VMs to HostPools.
Extension is installing ok on a VM, yet agents are installed on VMs, so the script is working, but still VM is not visible in a dedicated HostPool.

Cant add it manually, cause with 500+ deploy its gonna be a huge problem.

VM is creating, NIC and all is good.

Part of a code for VM and disk:

resource "azurerm_windows_virtual_machine" "vm" {
  count                      = var.VM_count
  name                       = "avd-${var.name_root}-${local.suffix}-0${count.index + 1}"
  computer_name              = var.name_root
  resource_group_name        = var.resource_group_name
  location                   = var.location
  size                       = var.vm_size
  admin_username             = var.user_password.user
  admin_password             = var.user_password.password
  network_interface_ids      = [azurerm_network_interface.vm_nic[count.index].id]
  tags                       = merge(var.resources_global_tags, tomap({ "type" = "VM" }), { "backup" = "YES", "patching" = "YES", "zone" = element(var.vm_zones, count.index)})
  zone                       = element(var.vm_zones, count.index)
  provision_vm_agent         = true
  encryption_at_host_enabled = true
  license_type               = "Windows_Client"
  enable_automatic_updates   = false
  patch_mode                 = "Manual"


HostPools:

resource "azurerm_virtual_desktop_workspace" "workspace" {
  name                = var.workspace2
  resource_group_name = var.resource_group_name 
  location            = var.location
  friendly_name       = "test_AVD"
  description         = "test_AVD"
}
# Create AVD host pool
resource "azurerm_virtual_desktop_host_pool" "hostpool" {
  count                    = var.VM_count
  resource_group_name      = var.resource_group_name
  location                 = var.location
  name                     = "test-${var.base_name_host_pool}-0${count.index + 1}"
  friendly_name            = "test-${var.base_name_host_pool}-0${count.index + 1}"
  validate_environment     = false
  start_vm_on_connect      = true
  custom_rdp_properties    = "targetisaadjoined:i:1;drivestoredirect:s:*;audiomode:i:2;videoplaybackmode:i:1;redirectclipboard:i:1;redirectprinters:i:1;devicestoredirect:s:*;redirectcomports:i:1;redirectsmartcards:i:1;usbdevicestoredirect:s:*;enablecredsspsupport:i:1;redirectwebauthn:i:1;use multimon:i:1;enablerdsaadauth:i:1;autoreconnection enabled:i:1"
  description              = "${var.prefix2} Terraform HostPool"
  type                     = "Pooled"
  maximum_sessions_allowed = 15
  load_balancer_type       = "BreadthFirst" #[BreadthFirst DepthFirst]
}
# # Associate Registration Key with hostpools
resource "azurerm_virtual_desktop_host_pool_registration_info" "registrationinfo" {
  count           = var.VM_count
  hostpool_id     = azurerm_virtual_desktop_host_pool.hostpool[count.index].id
  expiration_date = var.token
}
# Create AVD DAG

resource "azurerm_virtual_desktop_application_group" "dag" {
  count               = var.VM_count
  resource_group_name = var.resource_group_name
  host_pool_id        = azurerm_virtual_desktop_host_pool.hostpool[count.index].id
  location            = var.location
  type                = "Desktop"
  name                = "dag-${var.prefix2}-0${count.index + 1}"
  friendly_name       = "AVDDesktop"
  default_desktop_display_name = "AVD-${var.prefix2}-0${count.index + 1}"
  description         = "AVD application group"
  depends_on          = [azurerm_virtual_desktop_host_pool.hostpool, azurerm_virtual_desktop_workspace.workspace]
}
# Associate Workspace and DAG

resource "azurerm_virtual_desktop_workspace_application_group_association" "ws-dag" {
  count                    = var.VM_count
  application_group_id = azurerm_virtual_desktop_application_group.dag[count.index].id
  workspace_id         = azurerm_virtual_desktop_workspace.workspace.id
}

Few extensions:

resource "azurerm_virtual_machine_extension" "registersessionhost_dsc" {
  count                      = var.VM_count
  name                       = "dsc-${var.prefix2}-0${count.index + 1}"
  virtual_machine_id         = element(azurerm_windows_virtual_machine.vm.*.id, count.index)
  publisher                  = "Microsoft.Powershell"
  type                       = "DSC"
  type_handler_version       = "2.73"
  auto_upgrade_minor_version = true

  settings = <<-SETTINGS
    {
      "modulesUrl": "https://wvdportalstorageblob.blob.core.windows.net/galleryartifacts/Configuration.zip",
                     
      "configurationFunction": "Configuration.ps1\\AddSessionHost",
      "properties": {
        "HostPoolName":"${azurerm_virtual_desktop_host_pool.hostpool[count.index].name}"
      }
    }
SETTINGS

  protected_settings = <<PROTECTED_SETTINGS
  {
    "Properties": {
      "registrationInfoToken": "${azurerm_virtual_desktop_host_pool_registration_info.registrationinfo[count.index].id}"
    }
  }

PROTECTED_SETTINGS

  depends_on = [
    azurerm_virtual_desktop_host_pool.hostpool
  ]
}


resource "azurerm_virtual_machine_extension" "AADLoginForWindows" {
  count                      = var.VM_count
  name                       = "AADLoginForWindows"
  virtual_machine_id         =  azurerm_windows_virtual_machine.vm[count.index].id
  publisher                  = "Microsoft.Azure.ActiveDirectory"
  type                       = "AADLoginForWindows"
  type_handler_version       = "1.0"
  auto_upgrade_minor_version = true
  //depends_on                 = [ azurerm_virtual_machine_extension.OMS, azurerm_virtual_machine_extension.Dependency_agent ]

  lifecycle {
    ignore_changes = [
      tags
    ]
  }
}
1 Like

Copying my answer from the very similar question VMs in avd deployment in azure not connecting to the session host - #2 by mloskot

The Azure documentation on Add session hosts to a host pool - Azure Virtual Desktop | Microsoft Learn displays this:

image

OTOH, AADLoginForWindows should be deployable via Terraform as well as the JsonADDomainExtension for classic domain controllers. The Azure documentation is nto very informative clearly.