Azure Virtual Desktop Host Pool Provisioning using terraform

Hi,

I want to deploy Azure Virtual Desktop host pool via terraform. I want to add virtual machines to the host pool with the relevant configuration.

In the provider documentation under argument reference there is a parameter called vm_template. It has following description.

(Optional) A VM template for session hosts configuration within hostpool. This is a JSON string.

Unfortunately there is no documentation on how to specify the necessary parameters as JSON.

Provider for this is azurerm_virtual_desktop_host_pool

Could someone help on this on how to specify this JSON string.

Thank You

I agree - that the documentation is poor. You could raise an issue against the azurerm provider to have the documentation improved for this. But the MS documentation for this resource in ARM is not much more verbose: ( Microsoft.DesktopVirtualization/hostPools - Bicep, ARM template & Terraform AzAPI reference | Microsoft Learn

The vm_template attribute is for sessionhosts configuration within hostpool.

Here is an example taken from a template of an existing host pool via the export template feature of the Azure portal:

"vmTemplate": "{\"domain\":\"\",\"galleryImageOffer\":\"windows-11\",\"galleryImagePublisher\":\"microsoftwindowsdesktop\",\"galleryImageSKU\":\"win11-22h2-avd\",\"imageType\":\"Gallery\",\"customImageId\":null,\"namePrefix\":\"avdtesthost\",\"osDiskType\":\"StandardSSD_LRS\",\"vmSize\":{\"id\":\"Standard_D2as_v5\",\"cores\":2,\"ram\":8},\"galleryItemId\":\"microsoftwindowsdesktop.windows-11win11-22h2-avd\",\"hibernate\":false,\"diskSizeGB\":128,\"securityType\":\"TrustedLaunch\",\"secureBoot\":true,\"vTPM\":true,\"vmInfrastructureType\":\"Cloud\",\"virtualProcessorCount\":null,\"memoryGB\":null,\"maximumMemoryGB\":null,\"minimumMemoryGB\":null,\"dynamicMemoryConfig\":false}"

Which, when you remove all of the ‘escaping’ needed for an ARM template becomes:

"vmTemplate": {
  "domain": "",
  "galleryImageOffer": "windows-11",
  "galleryImagePublisher": "microsoftwindowsdesktop",
  "galleryImageSKU": "win11-22h2-avd",
  "imageType": "Gallery",
  "customImageId": null,
  "namePrefix": "avdtesthost",
  "osDiskType": "StandardSSD_LRS",
  "vmSize": { "id": "Standard_D2as_v5", "cores": 2, "ram": 8 },
  "galleryItemId": "microsoftwindowsdesktop.windows-11win11-22h2-avd",
  "hibernate": false,
  "diskSizeGB": 128,
  "securityType": "TrustedLaunch",
  "secureBoot": true,
  "vTPM": true,
  "vmInfrastructureType": "Cloud",
  "virtualProcessorCount": null,
  "memoryGB": null,
  "maximumMemoryGB": null,
  "minimumMemoryGB": null,
  "dynamicMemoryConfig": false
}

I can’t find anywhere that documents this, even when referring to Add session hosts to a host pool - Azure Virtual Desktop | Microsoft Learn.

My approach would be, if unsure, temporarily add a host-pool via the portal with the configuration you desire and inspect the ARM template that is available via the ‘export template’ option to extract the JSON required for the vm_template attribute.

You can then place the JSON into your module directly in the resource, or into a local variable using heredoc-strings or read from a local file, or pass in via in input variable.

Hope that helps

Happy Terraforming

Hi

I did the exact same thing which you have mentioned before raising the question here. Please refer my code below.

resource “azurerm_virtual_desktop_host_pool” “example” {
location = var.rglocation
resource_group_name = var.rgname
name = var.hostpoolname
validate_environment = false
preferred_app_group_type = “Desktop”
type = “Pooled”
load_balancer_type = var.loadbalncealgorithm

vm_template = <<EOF
{
“domain”: “”,

"galleryImageOffer": "windows-10",
"createVMs": "2",
"vmAdministratorAccountUsername": "localadmin",
"vmAdministratorAccountPassword": "Passwordrfd@7788",
"existingVnetName": "avdvnet",
"existingSubnetName": "private",
"galleryImagePublisher": "microsoftwindowsdesktop",
"galleryImageSKU": "win10-21h2-avd-g2",
"imageType": "Gallery",
"customImageId": null,
"namePrefix": "Myvms",
"osDiskType": "StandardSSD_LRS",
"vmSize": {
  "id": "Standard_D2as_v5",
  "cores": 2,
  "rm": 8
},
"galleryItemId": "microsoftwindowsdesktop.windows-10win10-21h2-avd-g2",
"hibernate": false,
"diskSizeGB": 128,
"securityType": "TrustedLaunch",
"secureBoot": true,
"vTPM": true,
"vmInfrastructureType": "Cloud",
"virtualProcessorCount": null,
"memoryGB": null,
"maximumMemoryGB": null,
"minimumMemoryGB": null,
"dynamicMemoryConfig": false

}
EOF

maximum_sessions_allowed = var.sessionllimit

}

But this will run the terrraform but it does not provision any host pools. I don’t know what to do on this

Thank You

Apologies, I was distracted by you asking about the vm_template attribute, rather than picking up on your actual desired outcome:

You need to create and then add the hosts to the host pool using Terraform. Just creating the AVD ‘infrastructure’ (host pools, registrations, scaling rules, etc.) does not cause hosts to be provisioned - it just provides the infrastructure or ‘framework’ for the AVD connectivity, scaling, app groups/workspace associations etc. The underlying session hosts are still azurerm_windows_virtual_machine resources which you have to create and then associate with the AVD infrastructure (host pools)

The MS Learn site provides a pretty good run-through: Configure Azure Virtual Desktop Session Hosts using Terraform | Microsoft Learn Note that this explicitly states that you should have deployed the AVD Infrastructure via Configure Azure Virtual Desktop using Terraform - Azure | Microsoft Learn

Good Luck!

Happy Terraforming

Hi
I have saw this as well. This MS docs also referring host pool creation join Active Directory Domain and it uses bit older modules.

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_desktop_host_pool

I want to use this particular provider and create session host configuration. In the azure portal when creating AVD under virtual machine section we can specify whether to Active directory/Entra, Number of VMs, Virtual Networks, Disk and so on.

For this provider vm_template argument is the respective argument to achieve that but it does not mentioned how to specify the attributes.

I want to know how to specify the arguments for this.

Hope your understood the question I have as of right now.

Thank You

Outside of me labbing this up end-to-end, I am not able to provide any definitive answer or guidance as I do not presently have access to an AVD implementation.
From what I extracted from the portal there was no reference to attributes that seemed to refer to the number of VMs to create or anything specific (outside of the domain attribute) that defines the specifics of the domain type and joining - but that does not mean there is not one.
I have previously used azurerm_virtual_machine_extension to add relevent extensions to aadds join hosts, deployed as compute, which are then registered to the host pool. However this was some time ago, so it is likely that things have changed, but I do not recall having used the vm_template attribute as part of the azurerm_virtual_desktop_host_pool

You might consider reviewing the Import - Generating Configuration functionality. And use the functionality to generate (using a temporary config/state) the config that the terraform module is expecting for the specific resource and review the output that is created. This may provide you further information/insight as to what is expected by the provider in the configuration of this resource as it pertains to the vm_template attribute. This may include more than is shown in the portal when doing a template export of the resource.

When you find the solution for this particular use-case, I would be interested in what it is.

Good luck!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.