dalle2024-08-1609.08.12-arealisticimagedepictinganalternativeofficesettingwithaslightlydifferentdesksetup.thelaptoponthedeskstilldisplaysterraformcoder.jpg

How to create a personal Azure Virtual Desktop scaling plan with Terraform using AzAPI

Leestijd: min.

In a recent project, I worked on setting up personal Azure Virtual Desktop (AVD) Session Hosts for a client. To optimize costs, I aimed to implement a scaling plan for personal host pools, which can significantly reduce Azure VM compute expenses. While Azure provides a straightforward guide on creating and assigning scaling plans through the portal here, implementing this via Terraform proved to be more challenging, particularly because Terraform’s azurerm_virtual_desktop_scaling_plan resource currently supports only pooled host pools at this time of writing.

Since the entire environment for this client is managed with Terraform, I had to find a way to work around this limitation. By leveraging the azapi_resource through the Terraform AzAPI provider, I was able to manage a scaling plan for personal host pools via Terraform. In this blog post, I'll walk you through the steps I took to configure a personal host pool scaling plan using Terraform and the azapi_resource.

Step 1: Add the AzAPI provider to your terraform configuration

The first step is to add the azapi provider to your Terraform configuration. This provider allows Terraform to manage resources via Azure API, which is essential for creating a scaling plan for personal host pools.

Here’s how you can specify the providers in your Terraform configuration block:

terraform { required_providers { azurerm = { source = "hashicorp/azurerm" } azapi = { source = "Azure/azapi" } }
azapi_personal_sp_1.jpg

After adding this configuration, run `terraform init` to install the azapi provider.

Step 2: Configure and assign scaler permissions

Create and assign the correct permissions for the Azure Virtual Desktop scaler plan as follows. Assign it to the resource groups where the Session Hosts VMs are created:

resource "random_uuid" "ScalePlan" { } resource "azurerm_role_definition" "DEV-ScalerRole" { name = "SP-ROLE-AVD-W11-DEV-Personal" scope = data.azurerm_resource_group.DEV-AVDSH.id description = "AVD AutoScale Role" permissions { actions = [ "Microsoft.Insights/eventtypes/values/read", "Microsoft.Compute/virtualMachines/deallocate/action", "Microsoft.Compute/virtualMachines/restart/action", "Microsoft.Compute/virtualMachines/powerOff/action", "Microsoft.Compute/virtualMachines/start/action", "Microsoft.Compute/virtualMachines/read", "Microsoft.DesktopVirtualization/hostpools/read", "Microsoft.DesktopVirtualization/hostpools/write", "Microsoft.DesktopVirtualization/hostpools/sessionhosts/read", "Microsoft.DesktopVirtualization/hostpools/sessionhosts/write", "Microsoft.DesktopVirtualization/hostpools/sessionhosts/usersessions/delete", "Microsoft.DesktopVirtualization/hostpools/sessionhosts/usersessions/read", "Microsoft.DesktopVirtualization/hostpools/sessionhosts/usersessions/sendMessage/action", "Microsoft.DesktopVirtualization/hostpools/sessionhosts/usersessions/read" ] not_actions = [] } assignable_scopes = [ data.azurerm_resource_group.DEV-AVDSH.id, ] } resource "azurerm_role_assignment" "WVDRole" { name = random_uuid.ScalePlan.result scope = data.azurerm_resource_group.DEV-AVDSH.id role_definition_id = azurerm_role_definition.DEV-ScalerRole.role_definition_resource_id principal_id = "1ed8b7ac-e72f-4578-bfc0-aa7017b02586" #Windows Virtual Desktop App Principal ID skip_service_principal_aad_check = true

Step 3: Define the personal scaling plan

Since Terraform does not yet support Personal Desktop Scaling Plans natively, you'll need to use the azapi_resource to define the scaling plan. Below is an example configuration:

resource "azapi_resource" "PersonalScalingPlan" { type = "Microsoft.DesktopVirtualization/scalingPlans@2023-11-01-preview" name = "VDSCALING-AVD-W11-DEV-Personal" location = "westeurope" parent_id = data.azurerm_resource_group.DEV-AVDSH.id body = jsonencode({ properties = { description = "VDSCALING-AVD-W11-DEV—Personal" exclusionTag = "ExcludeFromScaling" friendlyName = "VDSCALING-AVD-W11-DEV-Personal" hostPoolReferences = [ { hostPoolArmPath = azurerm_virtual_desktop_host_pool.DEV-AVDHP.id scalingPlanEnabled = true } ] hostPoolType = "Personal" timeZone = "W. Europe Standard Time" } }) depends_on = [azurerm_virtual_desktop_host_pool.DEV-AVDHP] }

Step 4: Set up the personal scaling plan schedule

Next, you need to define the schedule for the personal scaling plan. Here's an example configuration:

resource "azapi_resource" "ScalingPlanPersonalSchedule" { type = "Microsoft.DesktopVirtualization/scalingPlans/personalSchedules@2023-11-01-preview" name = "Alldays" parent_id = azapi_resource.PersonalScalingPlan.id body = jsonencode({ properties = { daysOfWeek = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ] offPeakActionOnDisconnect = "Deallocate" offPeakActionOnLogoff = "Deallocate" offPeakMinutesToWaitOnDisconnect = 120 offPeakMinutesToWaitOnLogoff = 60 offPeakStartTime = { hour = 22 minute = 0 } offPeakStartVMOnConnect = "Enable" peakActionOnDisconnect = "Deallocate" peakActionOnLogoff = "Deallocate" peakMinutesToWaitOnDisconnect = 120 peakMinutesToWaitOnLogoff = 60 peakStartTime = { hour = 9 minute = 0 } peakStartVMOnConnect = "Enable" rampDownActionOnDisconnect = "Deallocate" rampDownActionOnLogoff = "Deallocate" rampDownMinutesToWaitOnDisconnect = 120 rampDownMinutesToWaitOnLogoff = 60 rampDownStartTime = { hour = 19 minute = 0 } rampDownStartVMOnConnect = "Enable" rampUpActionOnDisconnect = "Deallocate" rampUpActionOnLogoff = "Deallocate" rampUpAutoStartHosts = "None" rampUpMinutesToWaitOnDisconnect = 120 rampUpMinutesToWaitOnLogoff = 60 rampUpStartTime = { hour = 6 minute = 0 } rampUpStartVMOnConnect = "Enable" } }) depends_on = [azapi_resource.PersonalScalingPlan] }
azapi_personal_sp_2.jpg

Conclusion

Configuring a scaling plan for personal host pools in Azure Virtual Desktop using Terraform can be a bit complex, especially since the azurerm provider does not directly support this feature yet. However, by using the azapi_resource along with the guidance above, you can implement an effective scaling plan that helps save on compute costs. This approach bridges the gap between Terraform’s current limitations and Azure’s capabilities, providing a scalable and cost-effective solution.

Meer nieuws
Interesse? Wij horen graag van u

Neem contact met ons op voor meer informatie of om een vrijblijvende afspraak te maken.

Direct kennismaken