Deploy AVD session hosts in an imported host pool using Terraform

Hello and welcome to another blogpost in my Azure Virtual Desktop and Terraform series. In this blogpost I’m gonna deploy 2 AVD session host in an already existing host pool.

Steps to be done:

  • Already have an existing host pool in Azure
  • modify the file for the host pool and import it
  • configure the tf file for deployment

Already have an existing hostpool in Azure

In my previous blogposts I explained how you can deploy an AVD host pool with Terraform. The code is also on my github.

How to import the already existing host pool

Because I created the host pool separately I need a way to tell Terraform it’s there. We can use the import command. We first thing we have to do is to put a resource for the host pool in our file.

resource "azurerm_virtual_desktop_host_pool" "hp" {}

The next step is to import the hostpool with the following command: Replace <subid> with your subscription id

terraform import azurerm_virtual_desktop_host_pool.hp /subscriptions/<subid>/resourceGroups/rg-prod-jvn-avd-backplane-01/providers/Microsoft.DesktopVirtualization/hostPools/hp-prod-jvn-avd-weu-01

After a couple of seconds you will get the below result.

With Terraform you can use the “plan” command to see what resources will be deployed. In this case the plan will give us errors like:

This is because the resource in the file is empty. We want the details from the imported host pool in there and this is possible with the terraform “show ” command.

Now that we have this, copy paste it in the resource in the file and run the terraform plan command again and it will work.

Registration of session hosts

Recently the way you create the registration token for AVD has changed. There is a complete configuration item to configure now. The code for the session hosts is also on my Github. You only need to change the empty variables in the file to suit your needs.

resource "azurerm_virtual_desktop_host_pool_registration_info" "avd_token" {
  hostpool_id =
  expiration_date = var.rfc3339

Make sure the hostpool_id is the one from the imported host pool. The variables for the expiration date is configured like this:

variable "rfc3339" {
  default = "2022-05-30T12:43:13Z"
  description = "token expiration"

For the registration to work you need to configure the extension. Make sure the hostPoolName matches the resource in the file and you point to the token

resource "azurerm_virtual_machine_extension" "registersessionhost" {
  name                 = "registersessionhost"
  virtual_machine_id   = azurerm_virtual_machine.vm[count.index].id
  depends_on = [
  publisher            = "Microsoft.Powershell"
  count                = "${var.vm_count}"
  type = "DSC"
  type_handler_version = "2.73"
  auto_upgrade_minor_version = true
  settings = <<SETTINGS
        "ModulesUrl": "${var.artifactslocation}",
        "ConfigurationFunction" : "Configuration.ps1\\AddSessionHost",
        "Properties": {
            "hostPoolName": ""
        protected_settings = <<PROTECTED_SETTINGS
      "properties" : {
            "registrationInfoToken" : "${azurerm_virtual_desktop_host_pool_registration_info.avd_token.token}"


    lifecycle {
        ignore_changes = [settings, protected_settings ]

After the apply is finished you can see the 2 hosts have been added to the host pool that we imported in this config.

If you have any questions about this feel free to contact me through my Twitter handle or my Linkedin.

Leave a Reply

Your email address will not be published. Required fields are marked *