次の方法で共有


Terraform を使用して Azure Files を構成する

次の Terraform と Terraform プロバイダーのバージョンでテストされた記事:

Terraform を使用すると、クラウド インフラストラクチャの定義、プレビュー、デプロイが可能になります。 Terraform を使用して、 HCL 構文を使用して構成ファイルを作成します。 HCL 構文を使用すると、クラウド プロバイダー (Azure など) とクラウド インフラストラクチャを構成する要素を指定できます。 構成ファイルを作成したら、インフラストラクチャの変更をデプロイする前にプレビューできる 実行プラン を作成します。 変更を確認したら、実行プランを適用してインフラストラクチャをデプロイします。

Azure には、FSLogix プロファイル コンテナーの格納に使用できる複数のストレージ ソリューションが用意されています。 この記事では、Terraform を使用した Azure Virtual Desktop FSLogix ユーザー プロファイル コンテナー用の Azure Files ストレージ ソリューションの構成について説明します

この記事では、次の方法について説明します。

  • Terraform を使用して Azure File Storage アカウントを管理する
  • Terraform を使用してファイル共有を構成する
  • Terraform を使用して Azure File Storage の RBAC アクセス許可を構成する

1. 環境を構成する

  • Azure サブスクリプション: Azure サブスクリプションをお持ちでない場合は、開始する前に 無料アカウント を作成してください。
  • Terraform の構成: まだ構成していない場合は、次のいずれかのオプションを使用して Terraform を構成します。

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 を使用するときの一般的な問題のトラブルシューティング

次のステップ