Configure Azure Virtual Desktop with Terraform

Article tested with the following Terraform and Terraform provider versions:

Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure.

This article provides an overview of how to use Terraform to deploy an ARM Azure Virtual Desktop environment, not AVD Classic.

There are several pre-requisites requirements for Azure Virtual Desktop

New to Azure Virtual Desktop? Start with What is Azure Virtual Desktop?

It is assumed that an appropriate platform foundation is already setup which may or may not be the Enterprise Scale Landing Zone platform foundation.

In this article, you learn how to:

  • Use Terraform to create an Azure Virtual Desktop workspace
  • Use Terraform to create an Azure Virtual Desktop host pool
  • Use Terraform to create an Azure Desktop Application Group
  • Associate a Workspace and a Desktop Application Group

1. Configure your environment

  • Azure subscription: If you don't have an Azure subscription, create a free account before you begin.

2. Implement the Terraform code

  1. Create a directory in which to test the sample Terraform code and make it the current directory.

  2. Create a file named providers.tf and insert the following code:

    terraform {
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~>2.0"
        }
        azuread = {
          source = "hashicorp/azuread"
        }
      }
    }
    
    provider "azurerm" {
      features {}
    }
    
  3. Create a file named main.tf and insert the following code:

    # Resource group name is output when execution plan is applied.
    resource "azurerm_resource_group" "sh" {
      name     = var.rg_name
      location = var.resource_group_location
    }
    
    # Create AVD workspace
    resource "azurerm_virtual_desktop_workspace" "workspace" {
      name                = var.workspace
      resource_group_name = azurerm_resource_group.sh.name
      location            = azurerm_resource_group.sh.location
      friendly_name       = "${var.prefix} Workspace"
      description         = "${var.prefix} Workspace"
    }
    
    # Create AVD host pool
    resource "azurerm_virtual_desktop_host_pool" "hostpool" {
      resource_group_name      = azurerm_resource_group.sh.name
      location                 = azurerm_resource_group.sh.location
      name                     = var.hostpool
      friendly_name            = var.hostpool
      validate_environment     = true
      custom_rdp_properties    = "audiocapturemode:i:1;audiomode:i:0;"
      description              = "${var.prefix} Terraform HostPool"
      type                     = "Pooled"
      maximum_sessions_allowed = 16
      load_balancer_type       = "DepthFirst" #[BreadthFirst DepthFirst]
    }
    
    resource "azurerm_virtual_desktop_host_pool_registration_info" "registrationinfo" {
      hostpool_id     = azurerm_virtual_desktop_host_pool.hostpool.id
      expiration_date = var.rfc3339
    }
    
    # Create AVD DAG
    resource "azurerm_virtual_desktop_application_group" "dag" {
      resource_group_name = azurerm_resource_group.sh.name
      host_pool_id        = azurerm_virtual_desktop_host_pool.hostpool.id
      location            = azurerm_resource_group.sh.location
      type                = "Desktop"
      name                = "${var.prefix}-dag"
      friendly_name       = "Desktop AppGroup"
      description         = "AVD application group"
      depends_on          = [azurerm_virtual_desktop_host_pool.hostpool, azurerm_virtual_desktop_workspace.workspace]
    }
    
    # Associate Workspace and DAG
    resource "azurerm_virtual_desktop_workspace_application_group_association" "ws-dag" {
      application_group_id = azurerm_virtual_desktop_application_group.dag.id
      workspace_id         = azurerm_virtual_desktop_workspace.workspace.id
    }
    
  4. Create a file named variables.tf and insert the following code:

    variable "resource_group_location" {
    default     = "eastus"
    description = "Location of the resource group."
    }
    
    variable "rg_name" {
    type        = string
    default     = "rg-avd-resources"
    description = "Name of the Resource group in which to deploy service objects"
    }
    
    variable "workspace" {
    type        = string
    description = "Name of the Azure Virtual Desktop workspace"
    default     = "AVD TF Workspace"
    }
    
    variable "hostpool" {
    type        = string
    description = "Name of the Azure Virtual Desktop host pool"
    default     = "AVD-TF-HP"
    }
    
    variable "rfc3339" {
    type        = string
    default     = "2022-03-30T12:43:13Z"
    description = "Registration token expiration"
    }
    
    variable "prefix" {
    type        = string
    default     = "avdtf"
    description = "Prefix of the name of the AVD machine(s)"
    }
    
  5. Create a file named output.tf and insert the following code:

    output "azure_virtual_desktop_compute_resource_group" {
      description = "Name of the Resource group in which to deploy session host"
      value       = azurerm_resource_group.sh.name
    }
    
    output "azure_virtual_desktop_host_pool" {
      description = "Name of the Azure Virtual Desktop host pool"
      value       = azurerm_virtual_desktop_host_pool.hostpool.name
    }
    
    output "azurerm_virtual_desktop_application_group" {
      description = "Name of the Azure Virtual Desktop DAG"
      value       = azurerm_virtual_desktop_application_group.dag.name
    }
    
    output "azurerm_virtual_desktop_workspace" {
      description = "Name of the Azure Virtual Desktop workspace"
      value       = azurerm_virtual_desktop_workspace.workspace.name
    }
    
    output "location" {
      description = "The Azure region"
      value       = azurerm_resource_group.sh.location
    }
    
    output "AVD_user_groupname" {
      description = "Azure Active Directory Group for AVD users"
      value       = azuread_group.aad_group.display_name
    }
    

3. Initialize Terraform

Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.

terraform init -upgrade

Key points:

  • The -upgrade parameter upgrades the necessary provider plugins to the newest version that complies with the configuration's version constraints.

4. Create a Terraform execution plan

Run terraform plan to create an execution plan.

terraform plan -out main.tfplan

Key points:

  • The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
  • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.

5. Apply a Terraform execution plan

Run terraform apply to apply the execution plan to your cloud infrastructure.

terraform apply main.tfplan

Key points:

  • The example terraform apply command assumes you previously ran terraform plan -out main.tfplan.
  • If you specified a different filename for the -out parameter, use that same filename in the call to terraform apply.
  • If you didn't use the -out parameter, call terraform apply without any parameters.

6. Verify the results

  1. On the Azure portal, Select Azure Virtual Desktop.
  2. Select Host pools and then the Name of the pool created resource.
  3. Select Session hosts and then verify the session host is listed.

7. Clean up resources

When you no longer need the resources created via Terraform, do the following steps:

  1. Run terraform plan and specify the destroy flag.

    terraform plan -destroy -out main.destroy.tfplan
    

    Key points:

    • The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
    • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.
  2. Run terraform apply to apply the execution plan.

    terraform apply main.destroy.tfplan
    

Troubleshoot Terraform on Azure

Troubleshoot common problems when using Terraform on Azure

Next steps