共用方式為


快速入門:使用 Terraform 使用 Azure 虛擬網絡 Manager 建立網格網路拓撲

使用 Terraform 為所有虛擬網路布建連線,以開始使用 Azure 虛擬網絡 Manager。

在本快速入門中,您會部署三個虛擬網路,並使用 Azure 虛擬網絡 Manager 來建立網狀網路拓撲。 然後,確認已套用連線設定。 您可以從具有訂用帳戶範圍或管理群組範圍的部署中選擇。 深入了解 網路管理員範圍

Terraform 可讓您定義、預覽和部署雲端基礎結構。 使用 Terraform 時,您可以使用 HCL 語法來建立設定檔。 HCL 語法可讓您指定雲端提供者 (例如 Azure) 和構成雲端基礎結構的元素。 建立設定檔之後,您可以建立執行計畫,讓您先預覽基礎結構變更,之後再部署。 驗證變更之後,您可以套用執行計畫來部署基礎結構。

在本文中,您將學會如何:

必要條件

實作 Terraform 程式碼

此程式代碼範例會在訂用帳戶範圍實作 Azure 虛擬網絡 Manager。

  1. 建立目錄,然後在目錄中測試並執行範例 Terraform 程式碼,且設為目前的目錄。

  2. 建立名為 providers.tf 的檔案,並插入下列程式碼:

    terraform {
      required_version = ">=1.0"
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = ">= 3.56.0"
        }
        random = {
          source  = "hashicorp/random"
          version = "~>3.0"
        }
      }
    }
    provider "azurerm" {
      features {}
    }
    
  3. 建立名為 main.tf 的檔案,並插入下列程式碼:

    
    # Create the Resource Group
    
    resource "random_pet" "rg_name" {
      prefix = var.resource_group_name_prefix
    }
    
    resource "azurerm_resource_group" "rg" {
      location = var.resource_group_location
      name     = random_pet.rg_name.id
    }
    
    # Create three virtual networks
    resource "random_string" "prefix" {
      length = 4
      special = false
      upper = false
    }
    
    resource "random_pet" "virtual_network_name" {
      prefix = "vnet-${random_string.prefix.result}"
    }
    resource "azurerm_virtual_network" "vnet" {
      count = 3
    
      name                = "${random_pet.virtual_network_name.id}-0${count.index}"
      resource_group_name = azurerm_resource_group.rg.name
      location            = azurerm_resource_group.rg.location
      address_space       = ["10.${count.index}.0.0/16"]
    }
    
    # Add a subnet to each virtual network
    
    resource "azurerm_subnet" "subnet_vnet" {
      count = 3
    
      name                 = "default"
      virtual_network_name = azurerm_virtual_network.vnet[count.index].name
      resource_group_name  = azurerm_resource_group.rg.name
      address_prefixes     = ["10.${count.index}.0.0/24"]
    }
    
    # Create a Virtual Network Manager instance
    
    data "azurerm_subscription" "current" {
    }
    
    resource "azurerm_network_manager" "network_manager_instance" {
      name                = "network-manager"
      location            = azurerm_resource_group.rg.location
      resource_group_name = azurerm_resource_group.rg.name
      scope_accesses      = ["Connectivity"]
      description         = "example network manager"
      scope {
        subscription_ids = [data.azurerm_subscription.current.id]
      }
    }
    
    # Create a network group
    
    resource "azurerm_network_manager_network_group" "network_group" {
      name               = "network-group"
      network_manager_id = azurerm_network_manager.network_manager_instance.id
    }
    
    # Add three virtual networks to a network group as dynamic members with Azure Policy
    
    resource "random_pet" "network_group_policy_name" {
      prefix = "network-group-policy"
    }
    
    resource "azurerm_policy_definition" "network_group_policy" {
      name         = "${random_pet.network_group_policy_name.id}"
      policy_type  = "Custom"
      mode         = "Microsoft.Network.Data"
      display_name = "Policy Definition for Network Group"
    
      metadata = <<METADATA
        {
          "category": "Azure Virtual Network Manager"
        }
      METADATA
    
      policy_rule = <<POLICY_RULE
        {
          "if": {
            "allOf": [
              {
                  "field": "type",
                  "equals": "Microsoft.Network/virtualNetworks"
              },
              {
                "allOf": [
                  {
                  "field": "Name",
                  "contains": "${random_pet.virtual_network_name.id}"
                  }
                ]
              }
            ]
          },
          "then": {
            "effect": "addToNetworkGroup",
            "details": {
              "networkGroupId": "${azurerm_network_manager_network_group.network_group.id}"
            }
          }
        }
      POLICY_RULE
    }
    
    resource "azurerm_subscription_policy_assignment" "azure_policy_assignment" {
      name                 = "${random_pet.network_group_policy_name.id}-policy-assignment"
      policy_definition_id = azurerm_policy_definition.network_group_policy.id
      subscription_id      = data.azurerm_subscription.current.id
    }
    
    # Create a connectivity configuration
    
    resource "azurerm_network_manager_connectivity_configuration" "connectivity_config" {
      name                  = "connectivity-config"
      network_manager_id    = azurerm_network_manager.network_manager_instance.id
      connectivity_topology = "Mesh"
      applies_to_group {
        group_connectivity = "None"
        network_group_id   = azurerm_network_manager_network_group.network_group.id
      }
    }
    
    
    # Commit deployment
    
    resource "azurerm_network_manager_deployment" "commit_deployment" {
      network_manager_id = azurerm_network_manager.network_manager_instance.id
      location           = azurerm_resource_group.rg.location
      scope_access       = "Connectivity"
      configuration_ids  = [azurerm_network_manager_connectivity_configuration.connectivity_config.id]
    }
    
  4. 建立名為 variables.tf 的檔案,並插入下列程式碼:

    variable "resource_group_location" {
      type        = string
      default     = "eastus"
      description = "Location of the resource group."
    }
    
    variable "resource_group_name_prefix" {
      type        = string
      description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
      default     = "rg"
    }
    
  5. 建立名為 outputs.tf 的檔案,並插入下列程式碼:

    output "resource_group_name" {
      value = azurerm_resource_group.rg.name
    }
    
    output "virtual_network_names" {
      value = azurerm_virtual_network.vnet[*].name
    }
    

實作 Terraform 程式碼

此程式代碼範例會在管理群組範圍實作 Azure 虛擬網絡 Manager。

  1. 建立目錄,然後在目錄中測試並執行範例 Terraform 程式碼,且設為目前的目錄。

  2. 建立名為 providers.tf 的檔案,並插入下列程式碼:

    terraform {
      required_version = ">=1.0"
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~> 3.56.0, < 4.0"
        }
        random = {
          source  = "hashicorp/random"
          version = "~>3.0"
        }
      }
    }
    provider "azurerm" {
      features {}
    }
    
  3. 建立名為 main.tf 的檔案,並插入下列程式碼:

    # Create the Resource Group
    
    resource "random_pet" "rg_name" {
      prefix = var.resource_group_name_prefix
    }
    
    resource "azurerm_resource_group" "rg" {
      location = var.resource_group_location
      name     = random_pet.rg_name.id
    }
    
    # Create three virtual networks
    resource "random_string" "prefix" {
      length  = 4
      special = false
      upper   = false
    }
    
    resource "random_pet" "virtual_network_name" {
      prefix = "vnet-${random_string.prefix.result}"
    }
    resource "azurerm_virtual_network" "vnet" {
      count = 3
    
      name                = "${random_pet.virtual_network_name.id}-0${count.index}"
      resource_group_name = azurerm_resource_group.rg.name
      location            = azurerm_resource_group.rg.location
      address_space       = ["10.${count.index}.0.0/16"]
    }
    
    # Add a subnet to each virtual network
    
    resource "azurerm_subnet" "subnet_vnet" {
      count = 3
    
      name                 = "default"
      virtual_network_name = azurerm_virtual_network.vnet[count.index].name
      resource_group_name  = azurerm_resource_group.rg.name
      address_prefixes     = ["10.${count.index}.0.0/24"]
    }
    
    data "azurerm_subscription" "current" {
    }
    
    # Create a Management Group
    
    resource "random_pet" "management_group_name" {
      prefix = "AVNM-management-group"
    }
    
    resource "azurerm_management_group" "mg" {
      display_name = random_pet.management_group_name.id
    
      subscription_ids = [
        data.azurerm_subscription.current.subscription_id,
      ]
    }
    
    data "azurerm_client_config" "this" {}
    
    resource "azurerm_role_assignment" "management_group_owner" {
      principal_id         = coalesce(var.msi_id, data.azurerm_client_config.this.object_id)
      scope                = azurerm_management_group.mg.id
      role_definition_name = "Contributor"
    }
    
    # register Microsoft.Network to the Management Group
    
    resource "null_resource" "register_rp_to_mg" {
      provisioner "local-exec" {
        command = "az provider register --namespace Microsoft.Network -m ${azurerm_management_group.mg.name}"
      }
      depends_on = [azurerm_role_assignment.management_group_owner]
    }
    
    resource "time_sleep" "wait_5_seconds" {
      create_duration = "5s"
      depends_on      = [null_resource.register_rp_to_mg]
    }
    
    # Create a Virtual Network Manager instance
    
    resource "azurerm_network_manager" "network_manager_instance" {
      name                = "network-manager"
      location            = azurerm_resource_group.rg.location
      resource_group_name = azurerm_resource_group.rg.name
      scope_accesses      = ["Connectivity"]
      description         = "example network manager"
      scope {
        management_group_ids = [azurerm_management_group.mg.id]
      }
      depends_on = [time_sleep.wait_5_seconds]
    }
    
    # Create a network group
    
    resource "azurerm_network_manager_network_group" "network_group" {
      name               = "network-group"
      network_manager_id = azurerm_network_manager.network_manager_instance.id
    }
    
    # Add three virtual networks to a network group as dynamic members with Azure Policy
    
    resource "random_pet" "network_group_policy_name" {
      prefix = "network-group-policy"
    }
    
    resource "azurerm_policy_definition" "network_group_policy" {
      name         = random_pet.network_group_policy_name.id
      policy_type  = "Custom"
      mode         = "Microsoft.Network.Data"
      display_name = "Policy Definition for Network Group"
    
      metadata = <<METADATA
        {
          "category": "Azure Virtual Network Manager"
        }
      METADATA
    
      policy_rule = <<POLICY_RULE
        {
          "if": {
            "allOf": [
              {
                  "field": "type",
                  "equals": "Microsoft.Network/virtualNetworks"
              },
              {
                "allOf": [
                  {
                  "field": "Name",
                  "contains": "${random_pet.virtual_network_name.id}"
                  }
                ]
              }
            ]
          },
          "then": {
            "effect": "addToNetworkGroup",
            "details": {
              "networkGroupId": "${azurerm_network_manager_network_group.network_group.id}"
            }
          }
        }
      POLICY_RULE
    }
    
    resource "azurerm_subscription_policy_assignment" "azure_policy_assignment" {
      name                 = "${random_pet.network_group_policy_name.id}-policy-assignment"
      policy_definition_id = azurerm_policy_definition.network_group_policy.id
      subscription_id      = data.azurerm_subscription.current.id
    }
    
    # Create a connectivity configuration
    
    resource "azurerm_network_manager_connectivity_configuration" "connectivity_config" {
      name                  = "connectivity-config"
      network_manager_id    = azurerm_network_manager.network_manager_instance.id
      connectivity_topology = "Mesh"
      applies_to_group {
        group_connectivity = "None"
        network_group_id   = azurerm_network_manager_network_group.network_group.id
      }
    }
    
    
    # Commit deployment
    
    resource "azurerm_network_manager_deployment" "commit_deployment" {
      network_manager_id = azurerm_network_manager.network_manager_instance.id
      location           = azurerm_resource_group.rg.location
      scope_access       = "Connectivity"
      configuration_ids  = [azurerm_network_manager_connectivity_configuration.connectivity_config.id]
    }
    
  4. 建立名為 variables.tf 的檔案,並插入下列程式碼:

    variable "resource_group_location" {
      type        = string
      default     = "eastus"
      description = "Location of the resource group."
    }
    
    variable "resource_group_name_prefix" {
      type        = string
      description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
      default     = "rg"
    }
    
    variable "msi_id" {
      type    = string
      description = "(Optional) Manage identity id that be used as authentication method. Defaults to `null`."
      default = null
    }
    
  5. 建立名為 outputs.tf 的檔案,並插入下列程式碼:

    output "resource_group_name" {
      value = azurerm_resource_group.rg.name
    }
    
    output "virtual_network_names" {
      value = azurerm_virtual_network.vnet[*].name
    }
    

初始化 Terraform

執行 terraform init 來初始化 Terraform 部署。 此命令會下載管理 Azure 資源所需的 Azure 提供者。

terraform init -upgrade

重點︰

  • -upgrade 參數會將必要的提供者外掛程式升級至符合設定版本條件約束的最新版本。

建立 Terraform 執行計畫

執行 terraform plan 以建立執行計畫。

terraform plan -out main.tfplan

重點︰

  • terraform plan 命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。
  • 選用的 -out 參數可讓您指定計畫的輸出檔。 使用 -out 參數可確保您所檢閱的方案就是所套用的方案。

套用 Terraform 執行計畫

執行 terraform apply 將執行計畫套用至您的雲端基礎結構。

terraform apply main.tfplan

重點︰

  • 範例 terraform apply 命令假設您之前已執行過 terraform plan -out main.tfplan
  • 如果您為 -out 參數指定了不同的檔案名稱,請在呼叫 terraform apply 時使用該檔案名稱。
  • 若您未使用 -out 參數,請呼叫 terraform apply,不需要使用參數。

驗證結果

  1. 取得 Azure 資源群組名稱。

    resource_group_name=$(terraform output -raw resource_group_name)
    
  2. 取得虛擬網路名稱。

    terraform output virtual_network_names
    
  3. 針對在上一個步驟中列印的每個虛擬網路名稱,執行 az network manager list-effective-connectivity-config 來列印有效 (applied) 組態。 將 <virtual_network_name> 佔位元取代為 vnet 名稱。

    az network manager list-effective-connectivity-config \
      --resource-group $resource_group_name \
      --vnet-name <virtual_network_name>
    

清除資源

當您不再需要透過 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 時的常見問題進行疑難排解

下一步