使用 Terraform 配置 Azure 虚拟网络设置

文章使用以下 Terraform 和 Terraform 提供程序版本进行测试:

使用 Terraform 可以定义、预览和部署云基础结构。 使用 Terraform 时,请使用 HCL 语法来创建配置文件。 利用 HCL 语法,可指定 Azure 这样的云提供程序和构成云基础结构的元素。 创建配置文件后,请创建一个执行计划,利用该计划,可在部署基础结构更改之前先预览这些更改。 验证了更改后,请应用该执行计划以部署基础结构。

本文概述了如何使用 Terraform 配置 Azure 虚拟桌面的网络设置。

在本文中,学习如何:

  • 使用 Terraform 创建虚拟网络
  • 使用 Terraform 创建子网
  • 使用 Terraform 创建 NSG
  • 将 Azure 虚拟桌面 vnet 与中心 vnet 对等互连

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 {}
    }
    
  3. 创建名为 main.tf 的文件并插入下列代码:

    resource "azurerm_virtual_network" "vnet" {
      name                = "${var.prefix}-VNet"
      address_space       = var.vnet_range
      dns_servers         = var.dns_servers
      location            = var.deploy_location
      resource_group_name = var.rg_name
      depends_on          = [azurerm_resource_group.rg]
    }
    
    resource "azurerm_subnet" "subnet" {
      name                 = "default"
      resource_group_name  = var.rg_name
      virtual_network_name = azurerm_virtual_network.vnet.name
      address_prefixes     = var.subnet_range
      depends_on           = [azurerm_resource_group.rg]
    }
    
    resource "azurerm_network_security_group" "nsg" {
      name                = "${var.prefix}-NSG"
      location            = var.deploy_location
      resource_group_name = var.rg_name
      security_rule {
        name                       = "HTTPS"
        priority                   = 1001
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "443"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
      }
      depends_on = [azurerm_resource_group.rg]
    }
    
    resource "azurerm_subnet_network_security_group_association" "nsg_assoc" {
      subnet_id                 = azurerm_subnet.subnet.id
      network_security_group_id = azurerm_network_security_group.nsg.id
    }
    
    data "azurerm_virtual_network" "ad_vnet_data" {
      name                = var.ad_vnet
      resource_group_name = var.ad_rg
    }
    
    resource "azurerm_virtual_network_peering" "peer1" {
      name                      = "peer_avdspoke_ad"
      resource_group_name       = var.rg_name
      virtual_network_name      = azurerm_virtual_network.vnet.name
      remote_virtual_network_id = data.azurerm_virtual_network.ad_vnet_data.id
    }
    resource "azurerm_virtual_network_peering" "peer2" {
      name                      = "peer_ad_avdspoke"
      resource_group_name       = var.ad_rg
      virtual_network_name      = var.ad_vnet
      remote_virtual_network_id = azurerm_virtual_network.vnet.id
    }
    
  4. 创建名为 variables.tf 的文件并插入下列代码:

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 "rg_shared_name" {
  type        = string
  default     = "rg-shared-resources"
  description = "Name of the Resource group in which to deploy shared resources"
}

variable "deploy_location" {
  type        = string
  default     = "eastus"
  description = "The Azure Region in which all resources in this example should be created."
}

variable "ad_vnet" {
  type        = string
  default     = "infra-network"
  description = "Name of domain controller vnet"
}

variable "dns_servers" {
  type        = list(string)
  default     = ["10.0.1.4", "168.63.129.16"]
  description = "Custom DNS configuration"
}

variable "vnet_range" {
  type        = list(string)
  default     = ["10.2.0.0/16"]
  description = "Address range for deployment VNet"
}
variable "subnet_range" {
  type        = list(string)
  default     = ["10.2.0.0/24"]
  description = "Address range for session host subnet"
}

variable "prefix" {
  type        = string
  default     = "avdtf"
  description = "Prefix of the name of the AVD machine(s)"
}
  1. 创建名为 output.tf 的文件并插入下列代码:
output "location" {
  description = "The Azure region"
  value       = azurerm_resource_group.rg.location
}

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
}

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 门户上,选择 Azure 虚拟桌面
  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 时遇到的常见问题

后续步骤