title | description | keywords | ms.topic | ms.date | ms.custom |
---|---|---|---|---|---|
Configure Azure Virtual Desktop Session Hosts using Terraform |
Learn how to use Terraform to configure session hosts and add them to a host pool. |
azure devops terraform avd virtual desktop session host |
how-to |
11/01/2022 |
devx-track-terraform |
This article shows you how to build Session Hosts and deploy them to an AVD Host Pool with Terraform. This article assumes you've already deployed the Azure Virtual Desktop Infrastructure.
Article tested with the following Terraform and Terraform provider versions:
Learn more about using Terraform in Azure
In this article, you learn how to:
[!div class="checklist"]
- Use Terraform to create NIC for each session host
- Use Terraform to create VM for session host
- Join VM to domain
- Register VM with Azure Virtual Desktop
- Use variables file
[!INCLUDE open-source-devops-prereqs-azure-subscription.md]
[!INCLUDE configure-terraform.md]
-
Create a directory in which to test the sample Terraform code and make it the current directory.
-
Create a file named
providers.tf
and insert the following code.[!code-terraform master]
Key points:
- Use
count
to indicate how many resources will be created - References resources that were created when the infrastructure was built - such as
azurerm_subnet.subnet.id
andazurerm_virtual_desktop_host_pool.hostpool.name
. If you changed the name of these resources from that section, you also need to update the references here.
- Use
-
Create a file named
main.tf
and insert the following code:[!code-terraform master]
-
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" {
type = string
default = "rg-avd-compute"
description = "Name of the Resource group in which to deploy session host"
}
variable "rdsh_count" {
description = "Number of AVD machines to deploy"
default = 2
}
variable "prefix" {
type = string
default = "avdtf"
description = "Prefix of the name of the AVD machine(s)"
}
variable "domain_name" {
type = string
default = "infra.local"
description = "Name of the domain to join"
}
variable "domain_user_upn" {
type = string
default = "domainjoineruser" # do not include domain name as this is appended
description = "Username for domain join (do not include domain name as this is appended)"
}
variable "domain_password" {
type = string
default = "ChangeMe123!"
description = "Password of the user to authenticate with the domain"
sensitive = true
}
variable "vm_size" {
description = "Size of the machine to deploy"
default = "Standard_DS2_v2"
}
variable "ou_path" {
default = ""
}
variable "local_admin_username" {
type = string
default = "localadm"
description = "local admin username"
}
variable "local_admin_password" {
type = string
default = "ChangeMe123!"
description = "local admin password"
sensitive = true
}
- Create a file named
output.tf
and insert the following code:
output "location" {
description = "The Azure region"
value = azurerm_resource_group.rg.location
}
output "session_host_count" {
description = "The number of VMs created"
value = var.rdsh_count
}
output "dnsservers" {
description = "Custom DNS configuration"
value = azurerm_virtual_network.vnet.dns_servers
}
output "vnetrange" {
description = "Address range for deployment vnet"
value = azurerm_virtual_network.vnet.address_space
}
-
Create a file named
terraform.tfvars
and insert the following code:[!code-terraform master]
[!INCLUDE terraform-init.md]
[!INCLUDE terraform-plan.md]
[!INCLUDE terraform-apply-plan.md]
- On the Azure portal, Select Azure Virtual Desktop.
- Select Host pools and then the Name of the pool created resource.
- Select Session hosts and then verify the session host is listed.
[!INCLUDE terraform-plan-destroy.md]
Troubleshoot common problems when using Terraform on Azure
[!div class="nextstepaction"] Learn more about using Terraform in Azure