使用 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 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.清理资源

不再需要通过 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 时遇到的常见问题

后续步骤