Create a scaling plan for a personal host pool with Terraform

Hi Everyone,

Welcome to another blog about AVD in combination with Terraform. One of the most important aspects when working in the cloud is cost saving. This is no different with AVD. With AVD scaling, the IT admin can make sure that the session hosts are not running when they are not in use. This way money can be saved.

Of course the scaling for in this case personal host pools can be configured in the Azure portal, but a lot of companies will want to automate this.

Configuring the autoscaling for personal host pool is not yet available using the AzureRM provider from Terraform. However, it’s possible to use the AzApi provider.

Prerequisites

Before starting with the code to create the autoscaling schedule, the IT admin need to foresee some resources.

  • Host pool
  • Resource group

Change the variables in the code to fit your own need.

Host pool

Probably the host pool already exists, so the IT admin needs to import that in the config file. The following line of code can be used for that. In case the host pool doesn’t exist yes, feel free to use my code on my Github to create it.

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

Resource group

The second resource that the IT admin needs to import is the resource group where the scaling plan will be created.

data "azurerm_resource_group" "hp-rg" {
  provider = azurerm.hub ## in case you use multiple subscriptions
    name = "rg-${var.spoke}-${var.prefix}-${var.solution}-backplane-01"
}

Creating the scaling plan

As already mentioned, the AzureRM provider doesn’t give the option to create scaling plans for personal host pools yet. To be able to create them, you need to use the Azapi provider.

resource "azapi_resource" "weekdays_personal_schedule_root" {
  type      = "Microsoft.DesktopVirtualization/scalingPlans@2023-11-01-preview"
  name      = "scaling-p-prd-jvn-avd-01"
  location  = data.azurerm_virtual_desktop_host_pool.hp.location
  parent_id = data.azurerm_resource_group.hp-rg.id
   tags = {
    Environment = "avd"
    Costcenter = "IT"   
    Solution = "ScalingPlan" 
    HostPoolType = "Personal"
  }
  body = jsonencode({
    properties = {
      timeZone     = "W. Europe Standard Time",
      hostPoolType = "Personal",
      exclusionTag = "Maintenance",
      schedules    = [],
      hostPoolReferences = [
        {
          hostPoolArmPath    = data.azurerm_virtual_desktop_host_pool.hp.id,
          scalingPlanEnabled = true
        }
      ],
  } })
}

Creating the scaling schedule

Now that the scaling plan is created it’s time to create the schedule. In this example, the IT admin creates a schedule for weekdays and uses the Hibernate feature.

resource "azapi_resource" "weekdays_personal_schedule" {
  type  = "Microsoft.DesktopVirtualization/scalingPlans/personalSchedules@2023-11-01-preview"
  name  = "Weekdays"
  parent_id = azapi_resource.weekdays_personal_schedule_root.id
  body = jsonencode({
    properties = {
      daysOfWeek = [
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday"
      ]

      rampUpStartTime = {
        hour   = 7,
        minute = 0
      },
      rampUpAutoStartHosts            = "None",
      rampUpStartVMOnConnect          = "Enable",
      rampUpMinutesToWaitOnDisconnect = 45,
      rampUpActionOnDisconnect        = "Hibernate",
      rampUpMinutesToWaitOnLogoff     = 30,
      rampUpActionOnLogoff            = "Hibernate",

      peakStartTime = {
        hour   = 8,
        minute = 0
      },
      peakStartVMOnConnect          = "Enable",
      peakMinutesToWaitOnDisconnect = 60,
      peakActionOnDisconnect        = "Hibernate",
      peakMinutesToWaitOnLogoff     = 60,
      peakActionOnLogoff            = "Hibernate",

      rampDownStartTime = {
        hour   = 16,
        minute = 30
      },
      rampDownStartVMOnConnect          = "Enable",
      rampDownMinutesToWaitOnDisconnect = 45,
      rampDownActionOnDisconnect        = "Hibernate",
      rampDownMinutesToWaitOnLogoff     = 30,
      rampDownActionOnLogoff            = "Hibernate",

      offPeakStartTime = {
        hour   = 17,
        minute = 30
      },
      offPeakStartVMOnConnect          = "Enable",
      offPeakMinutesToWaitOnDisconnect = 20,
      offPeakActionOnDisconnect        = "Hibernate",
      offPeakMinutesToWaitOnLogoff     = 15,
      offPeakActionOnLogoff            = "Hibernate",
    }
  })
}

As you can see it’s easy to create a scaling plan and schedule for personal host pools with Terraform. The code for this blog post can be found on my Github

Feel free to leave a comment in case you have any questions.

1 thought on “Create a scaling plan for a personal host pool with Terraform

Leave a Reply

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