Terraform を使用して Azure Virtual Desktop セッション ホストを構成する

この記事では、セッション ホストをビルドし、Terraform を使用して AVD ホスト プールにデプロイする方法について説明します。 この記事では、Azure Virtual Desktop インフラストラクチャを既にデプロイしていることを前提としています。

次の Terraform と Terraform プロバイダーのバージョンでテストされた記事:

Azure での Terraform の使用について詳細を参照

この記事では、次のことについて説明します。

  • Terraform を使用してセッション ホストごとに NIC を作成する
  • Terraform を使用してセッション ホスト用の VM を作成する
  • VM をドメインに参加させる
  • Azure Virtual Desktop で VM を登録する
  • 変数ファイルを使用する

1. 環境を構成する

  • Azure サブスクリプション:Azure サブスクリプションをお持ちでない場合は、開始する前に無料アカウントを作成してください。

2. Terraform コードを実装する

  1. サンプルの Terraform コードをテストするディレクトリを作成し、それを現在のディレクトリにします。

  2. providers.tf という名前のファイルを作成し、次のコードを挿入します。

    terraform {
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~>2.0"
        }
        azuread = {
          source = "hashicorp/azuread"
        }
      }
    }
    
    provider "azurerm" {
      features {}
    }
    

    重要なポイント:

    • 作成されるリソースの数を示すために count を使用する
    • インフラストラクチャのビルド時に作成された azurerm_subnet.subnet.idazurerm_virtual_desktop_host_pool.hostpool.name などのリソースを参照します。 そのセクションからこれらのリソースの名前を変更した場合は、ここでも参照を更新する必要があります。
  3. main.tf という名前のファイルを作成し、次のコードを挿入します。

    locals {
      registration_token = azurerm_virtual_desktop_host_pool_registration_info.registrationinfo.token
    }
    
    resource "random_string" "AVD_local_password" {
      count            = var.rdsh_count
      length           = 16
      special          = true
      min_special      = 2
      override_special = "*!@#?"
    }
    
    resource "azurerm_resource_group" "rg" {
      name     = var.rg
      location = var.resource_group_location
    }
    
    resource "azurerm_network_interface" "avd_vm_nic" {
      count               = var.rdsh_count
      name                = "${var.prefix}-${count.index + 1}-nic"
      resource_group_name = azurerm_resource_group.rg.name
      location            = azurerm_resource_group.rg.location
    
      ip_configuration {
        name                          = "nic${count.index + 1}_config"
        subnet_id                     = azurerm_subnet.subnet.id
        private_ip_address_allocation = "dynamic"
      }
    
      depends_on = [
        azurerm_resource_group.rg
      ]
    }
    
    resource "azurerm_windows_virtual_machine" "avd_vm" {
      count                 = var.rdsh_count
      name                  = "${var.prefix}-${count.index + 1}"
      resource_group_name   = azurerm_resource_group.rg.name
      location              = azurerm_resource_group.rg.location
      size                  = var.vm_size
      network_interface_ids = ["${azurerm_network_interface.avd_vm_nic.*.id[count.index]}"]
      provision_vm_agent    = true
      admin_username        = var.local_admin_username
      admin_password        = var.local_admin_password
    
      os_disk {
        name                 = "${lower(var.prefix)}-${count.index + 1}"
        caching              = "ReadWrite"
        storage_account_type = "Standard_LRS"
      }
    
      source_image_reference {
        publisher = "MicrosoftWindowsDesktop"
        offer     = "Windows-10"
        sku       = "20h2-evd"
        version   = "latest"
      }
    
      depends_on = [
        azurerm_resource_group.rg,
        azurerm_network_interface.avd_vm_nic
      ]
    }
    
    resource "azurerm_virtual_machine_extension" "domain_join" {
      count                      = var.rdsh_count
      name                       = "${var.prefix}-${count.index + 1}-domainJoin"
      virtual_machine_id         = azurerm_windows_virtual_machine.avd_vm.*.id[count.index]
      publisher                  = "Microsoft.Compute"
      type                       = "JsonADDomainExtension"
      type_handler_version       = "1.3"
      auto_upgrade_minor_version = true
    
      settings = <<SETTINGS
        {
          "Name": "${var.domain_name}",
          "OUPath": "${var.ou_path}",
          "User": "${var.domain_user_upn}@${var.domain_name}",
          "Restart": "true",
          "Options": "3"
        }
    SETTINGS
    
      protected_settings = <<PROTECTED_SETTINGS
        {
          "Password": "${var.domain_password}"
        }
    PROTECTED_SETTINGS
    
      lifecycle {
        ignore_changes = [settings, protected_settings]
      }
    
      depends_on = [
        azurerm_virtual_network_peering.peer1,
        azurerm_virtual_network_peering.peer2
      ]
    }
    
    resource "azurerm_virtual_machine_extension" "vmext_dsc" {
      count                      = var.rdsh_count
      name                       = "${var.prefix}${count.index + 1}-avd_dsc"
      virtual_machine_id         = azurerm_windows_virtual_machine.avd_vm.*.id[count.index]
      publisher                  = "Microsoft.Powershell"
      type                       = "DSC"
      type_handler_version       = "2.73"
      auto_upgrade_minor_version = true
    
      settings = <<-SETTINGS
        {
          "modulesUrl": "https://wvdportalstorageblob.blob.core.windows.net/galleryartifacts/Configuration_09-08-2022.zip",
          "configurationFunction": "Configuration.ps1\\AddSessionHost",
          "properties": {
            "HostPoolName":"${azurerm_virtual_desktop_host_pool.hostpool.name}"
          }
        }
    SETTINGS
    
      protected_settings = <<PROTECTED_SETTINGS
      {
        "properties": {
          "registrationInfoToken": "${local.registration_token}"
        }
      }
    PROTECTED_SETTINGS
    
      depends_on = [
        azurerm_virtual_machine_extension.domain_join,
        azurerm_virtual_desktop_host_pool.hostpool
      ]
    }
    
  4. variables.tf という名前のファイルを作成し、次のコードを挿入します。

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
}
  1. output.tf という名前のファイルを作成し、次のコードを挿入します。
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
}
  1. terraform.tfvars という名前のファイルを作成し、次のコードを挿入します。

    # Customized the sample values below for your environment and either rename to terraform.tfvars or env.auto.tfvars
    
    deploy_location      = "west europe"
    rg_name              = "avd-resources-rg"
    prefix               = "avdtf"
    local_admin_username = "localadm"
    local_admin_password = "ChangeMe123$"
    vnet_range           = ["10.1.0.0/16"]
    subnet_range         = ["10.1.0.0/24"]
    dns_servers          = ["10.0.1.4", "168.63.129.16"]
    aad_group_name       = "AVDUsers"
    domain_name          = "infra.local"
    domain_user_upn      = "admin"     # do not include domain name as this is appended
    domain_password      = "ChangeMe123!"
    ad_vnet              = "infra-network"
    ad_rg                = "infra-rg"
    avd_users = [
      "avduser01@infra.local",
      "avduser01@infra.local"
    ]
    

3. Terraform を初期化する

terraform init を実行して、Terraform のデプロイを初期化します。 このコマンドによって、Azure リソースを管理するために必要な Azure プロバイダーがダウンロードされます。

terraform init -upgrade

重要なポイント:

  • -upgrade パラメーターは、必要なプロバイダー プラグインを、構成のバージョン制約に準拠する最新バージョンにアップグレードします。

4. Terraform 実行プランを作成する

terraform plan を実行して、実行プランを作成します。

terraform plan -out main.tfplan

重要なポイント:

  • terraform plan コマンドは、実行プランを作成しますが、実行はしません。 代わりに、構成ファイルに指定された構成を作成するために必要なアクションを決定します。 このパターンを使用すると、実際のリソースに変更を加える前に、実行プランが自分の想定と一致しているかどうかを確認できます。
  • 省略可能な -out パラメーターを使用すると、プランの出力ファイルを指定できます。 -out パラメーターを使用すると、レビューしたプランが適用内容とまったく同じであることが確実になります。

5. Terraform 実行プランを適用する

terraform apply を実行して、クラウド インフラストラクチャに実行プランを適用します。

terraform apply main.tfplan

重要なポイント:

  • terraform apply コマンドの例は、以前に terraform plan -out main.tfplan が実行されたことを前提としています。
  • -out パラメーターに別のファイル名を指定した場合は、terraform apply の呼び出しで同じファイル名を使用します。
  • -out パラメーターを使用しなかった場合は、パラメーターを指定せずに terraform apply を呼び出します。

6. 結果を確認する

  1. Azure portal で [Azure Virtual Desktop] を選択します。
  2. [ホスト プール] を選択し、[作成したプールの名前] リソースを選択します。
  3. [セッション ホスト] を選択し、セッション ホストが一覧表示されていることを確認します。

7.リソースをクリーンアップする

Terraform を使用して作成したリソースが不要になった場合は、次の手順を実行します。

  1. terraform plan を実行して、destroy フラグを指定します。

    terraform plan -destroy -out main.destroy.tfplan
    

    重要なポイント:

    • terraform plan コマンドは、実行プランを作成しますが、実行はしません。 代わりに、構成ファイルに指定された構成を作成するために必要なアクションを決定します。 このパターンを使用すると、実際のリソースに変更を加える前に、実行プランが自分の想定と一致しているかどうかを確認できます。
    • 省略可能な -out パラメーターを使用すると、プランの出力ファイルを指定できます。 -out パラメーターを使用すると、レビューしたプランが適用内容とまったく同じであることが確実になります。
  2. terraform apply を実行して、実行プランを適用します。

    terraform apply main.destroy.tfplan
    

Azure での Terraform のトラブルシューティング

Azure で Terraform を使用する場合の一般的な問題のトラブルシューティング

次のステップ