Add a new session host to an existing host pool the easy way with Terraform

In my last couple of blog posts about deploying an AVD host pool and session hosts I always deploy all the resources in one deployment. Till now Terraform couldn’t load the host pool in your main.tf file other then with an import command.

Recently I got a question from a community member if I had a way of making the deployment off a session host easier. We don’t want to delete the session hosts and the host pool and deploy it again.

Last friday the 17th of february Hashicorp released the newest azurerm provider, version 3.44.1 of the Azurerm provider. There is 1 big new feature in this provider for AVD. There is a data resource in there for azurerm_virtual_desktop_host_pool. In this blog post I’ll show you how to use this new data resource to deploy your session host(s) in an existing host pool.

To be able to use this new data resource you have to make sure that you are using the correct version of the Azurerm provider.

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "=3.44.1"
    }
  }
}

For this blog post I’m splitting up the deployment.

  • Part 1 the host pool(s)
  • Part 2 the session host(s)

Part 1 is to deploy the host pool. The code for this can be found on my Github.

Now that we have an existing host pool we need to be able to add the hosts.

The most important change to the azurerm provider for AVD is the fact that you can use a data resource for the host pool.

In Part 2 we are going to add the session hosts to the host pool. Since last week there is an easy way to reference an already deployed host pool. You can add a data resource in your Terraform code.

The code for the session host(s) can be found here.

This also means that you can deploy to multiple host pools in 1 deployment. You can add more than 1 data source for host pools.

data "azurerm_virtual_desktop_host_pool" "hp" {
  provider = azurerm.hub
  name = "hp-${var.spoke}-${var.prefix}-${var.solution}-we-01"
  resource_group_name = "rg-${var.spoke}-${var.prefix}-${var.solution}-backplane-01"
}

When running Terraform plan the existing host pool(s) will be loaded.

Registration Info Host Pool

The first resource where we need to reference the host pool is to create a new registration token. The host pool id in this resource is referencing the data resource from the host pool.

resource "azurerm_virtual_desktop_host_pool_registration_info" "avd_token" {
  provider        = azurerm.hub
  hostpool_id     = data.azurerm_virtual_desktop_host_pool.hp.id
  expiration_date = var.rfc3339
}

The second resource where we need to use the data resource is with the register sessionhost extension. Here we apply the same way or referencing the host pool. Only here we use the name of the host pool.

resource "azurerm_virtual_machine_extension" "registersessionhost" {
  name               = "registersessionhost"
  virtual_machine_id = azurerm_windows_virtual_machine.vm[count.index].id
  depends_on = [
    azurerm_windows_virtual_machine.vm
  ]
  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": "${data.azurerm_virtual_desktop_host_pool.hp.name}",
            "aadJoin": true
            
        }
    }
    SETTINGS
  protected_settings         = <<PROTECTED_SETTINGS
    {
      "properties" : {
            "registrationInfoToken" : "${azurerm_virtual_desktop_host_pool_registration_info.avd_token.token}"
        }
    }
    PROTECTED_SETTINGS

  lifecycle {
    ignore_changes = [settings, protected_settings]
  }
}

To conclude, the new data resource for host pool(s) make it very easy to first deploy the AVD backend and afterwards the AVD session host(s). It also makes it easier to for example do the patching from your AVD host pool(s).

I hope this blog post can help you deploy session host(s) in your host pool(s). If you have any questions you can always send me a massage.

3 thoughts on “Add a new session host to an existing host pool the easy way with Terraform

Leave a Reply

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