共用方式為


使用 Terraform 設定 Azure 檔案記憶體

使用下列 Terraform 和 Terraform 提供者版本進行測試的文章:

Terraform 可啟用雲端基礎結構的定義、預覽和部署。 使用 Terraform,您可以使用 HCL 語法建立組態檔。 HCL 語法可讓您指定雲端提供者,例如 Azure,以及構成雲端基礎結構的專案。 建立組態檔之後,您會建立一個 執行計劃 ,讓您在部署基礎結構變更之前先預覽這些變更。 驗證變更之後,您會套用執行計劃來部署基礎結構。

Azure 提供多個記憶體解決方案,可讓您用來儲存 FSLogix 配置檔容器。 本文涵蓋如何使用 Terraform 來設定 Azure 虛擬桌面 FSLogix 使用者設定檔容器的 Azure 檔案儲存解決方案。

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

  • 使用 Terraform 於 Azure 檔案儲存帳戶
  • 使用 Terraform 來設定檔案共用
  • 使用 Terraform 在 Azure 檔案記憶體上設定 RBAC 許可權

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 的檔案,並插入下列程序代碼:

    ## Create a Resource Group for Storage
    resource "azurerm_resource_group" "rg_storage" {
      location = var.deploy_location
      name     = var.rg_stor
    }
    
    # generate a random string (consisting of four characters)
    # https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string
    resource "random_string" "random" {
      length  = 4
      upper   = false
      special = false
    }
    
    ## Azure Storage Accounts requires a globally unique names
    ## https://docs.microsoft.com/en-us/azure/storage/common/storage-account-overview
    ## Create a File Storage Account 
    resource "azurerm_storage_account" "storage" {
      name                     = "stor${random_string.random.id}"
      resource_group_name      = azurerm_resource_group.rg_storage.name
      location                 = azurerm_resource_group.rg_storage.location
      account_tier             = "Premium"
      account_replication_type = "LRS"
      account_kind             = "FileStorage"
    }
    
    resource "azurerm_storage_share" "FSShare" {
      name                 = "fslogix"
      storage_account_name = azurerm_storage_account.storage.name
      depends_on           = [azurerm_storage_account.storage]
    }
    
    ## Azure built-in roles
    ## https://docs.microsoft.com/en-us/azure/role-based-access-control/built-in-roles
    data "azurerm_role_definition" "storage_role" {
      name = "Storage File Data SMB Share Contributor"
    }
    
    resource "azurerm_role_assignment" "af_role" {
      scope              = azurerm_storage_account.storage.id
      role_definition_id = data.azurerm_role_definition.storage_role.id
      principal_id       = azuread_group.aad_group.id
    }
    
  4. 建立名為 variables.tf 的檔案,並插入下列程序代碼:

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

variable "rg_stor" {
  type        = string
  default     = "rg-avd-storage"
  description = "Name of the Resource group in which to deploy storage"
}

variable "avd_users" {
  description = "AVD users"
  default = [
    "avduser01@contoso.net",
    "avduser02@contoso.net"
  ]
}

variable "aad_group_name" {
  type        = string
  default     = "AVDUsers"
  description = "Azure Active Directory Group for AVD users"
}
  1. 建立名為 output.tf 的檔案,並插入下列程序代碼:
output "location" {
  description = "The Azure region"
  value       = azurerm_resource_group.rg_storage.location
}

output "storage_account" {
  description = "Storage account for Profiles"
  value       = azurerm_storage_account.storage.name
}

output "storage_account_share" {
  description = "Name of the Azure File Share created for FSLogix"
  value       = azurerm_storage_share.FSShare.name
}

output "AVD_user_groupname" {
  description = "Azure Active Directory Group for AVD users"
  value       = azuread_group.aad_group.display_name
}

3. 初始化 Terraform

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

terraform init -upgrade

要點:

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

4.建立 Terraform 執行計劃

執行 terraform 計劃 以建立執行計劃。

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.清除資源

當您不再需要透過 Terraform 建立的資源時,請執行下列步驟:

  1. 執行 terraform 計劃 並指定 destroy 旗標。

    terraform plan -destroy -out main.destroy.tfplan
    

    要點:

    • terraform plan 命令會建立執行計劃,但不會執行它。 然而,它會決定哪些動作是必要的,以建立您組態檔中所指定的設定。 此模式可讓您在對實際資源進行任何變更之前,先確認執行計劃是否符合您的預期。
    • 選擇性 -out 參數可讓您指定計劃的輸出檔。 使用 -out 參數可確保您查看的計劃確切地被套用。
  2. 執行 terraform apply 以執行計劃。

    terraform apply main.destroy.tfplan
    

排除 Azure 上 Terraform 的故障

針對在 Azure 上使用 Terraform 時的常見問題進行疑難解答

後續步驟