我試著使用Terraform配置Azure Virtual Desktop

背景和目的

目前只处于公开预览阶段,但 Azure 虚拟桌面的主机池可以在东日本进行创建了。我在我的验证订阅中终于看到了东日本的选项,所以我想要尝试一下。这次,我使用了官方文档中提供的使用 Terraform 的示例进行尝试。

这里只需要给出一个中文的选项,所以我的回答是:前提。

执行环境为Mac + Azure CLI + Terraform。

$ 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 代码

 

根据这份文档作为参考,我们将开始编写Terraform代码。

请根据您的环境适当地更改variables.tf。

$ 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资源。

将创建资源组、主机池、应用程序组和工作区。

$ terraform init

$ terraform plan

$ terraform apply -auto-approve

以下的截图是执行后的 Azure 资源。

avd-terraform.png

删除由验证创建的 Azure 资源。

$ terraform plan -destroy

$ terraform destroy -auto-approve

请自行参考。

 

广告
将在 10 秒后关闭
bannerAds