Terraform を使用して Azure Virtual Desktop を構成する、を試してみた

## 背景と目的

今のところパブリックプレビューですが、Azure Virtual Desktop のホストプールが東日本で作成できるようになりました。自分が検証用に使用しているサブスクリプションに、やっと東日本の選択肢が展開されたようなので試してみたいと思います。今回は、公式ドキュメントにある Terraform を使用したサンプルで試してみました。

## 前提条件

コマンドの実施環境は、Mac + Azure CLI + Terraform です。

```bash
$ sw_vers
ProductName:    macOS
ProductVersion: 12.4
BuildVersion:   21F79

$ az version
{
  "azure-cli": "2.37.0",
  "azure-cli-core": "2.37.0",
  "azure-cli-telemetry": "1.0.6",
  "extensions": {}
}

$ terraform --version
Terraform v1.2.2
on darwin_amd64
```

## Terraform コードを作成

https://docs.microsoft.com/ja-jp/azure/developer/terraform/configure-azure-virtual-desktop

こちらのドキュメントを参考に、Terraform コードを作成していきます。

variables.tf は自分の環境に合わせて適宜変更してください。

```bash:bash
$ cat <<EOF > providers.tf
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>2.0"
    }
    azuread = {
      source = "hashicorp/azuread"
    }
  }
}

provider "azurerm" {
  features {}
}
EOF

$ cat <<"EOF" > main.tf
# 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
}
EOF

$ cat <<EOF > variables.tf
variable "resource_group_location" {
  type        = string
  default     = "japaneast"
  description = "Location of the resource group."
}

variable "rg_name" {
  type        = string
  default     = "devavd-rg"
  description = "Name of the Resource group in which to deploy service objects"
}

variable "workspace" {
  type        = string
  default     = "AVD TF Workspace"
  description = "Name of the Azure Virtual Desktop workspace"
}

variable "hostpool" {
  type        = string
  default     = "AVD-TF-HP"
  description = "Name of the Azure Virtual Desktop host pool"
}

variable "rfc3339" {
  type        = string
  default     = "2022-06-25T09:00:00Z"
  description = "Registration token expiration"
}

variable "prefix" {
  type        = string
  default     = "devavd"
  description = "Prefix of the name of the AVD machine(s)"
}
EOF
```

## Terraform を実行し Azure リソースを作成

リソースグループ、ホストプール、アプリケーショングループ、ワークスペースが作成されます。

```bash:bash
$ terraform init

$ terraform plan

$ terraform apply -auto-approve
```

下記のスクショは実行後の Azure リソースです。

![avd-terraform.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/675885/81379df8-8cde-1285-01c3-b43671723ea2.png)

## 検証で作成した Azure リソースを削除

```bash:bash
$ terraform plan -destroy

$ terraform destroy -auto-approve
```

## 参考

https://techcommunity.microsoft.com/t5/azure-virtual-desktop/announcing-the-public-preview-of-the-azure-virtual-desktop/m-p/3417497

https://docs.microsoft.com/ja-jp/azure/virtual-desktop/data-locations

https://qiita.com/mnrst/items/63c6b9b77d58a557493d

https://qiita.com/mnrst/items/0527a9a3f44a6a910cb3