クイックスタート: Terraform を使用して Azure で Windows VM クラスターを作成する

適用対象: ✔️ Windows VM

この記事では、Terraform を使用して Azure で (3 つの Windows VM インスタンスを含む) Windows VM クラスターを作成する方法を示します。

前提条件

Terraform コードを実装する

注意

この記事のサンプル コードは、Azure Terraform GitHub リポジトリにあります。 Terraform の現在および以前のバージョンのテスト結果を含むログ ファイルを表示できます。

Terraform を使用して Azure リソースを管理する方法を示すその他の記事とサンプル コードを参照してください

  1. サンプルの Terraform コードをテストするディレクトリを作成し、それを現在のディレクトリにします。

  2. providers.tf という名前のファイルを作成し、次のコードを挿入します。

    terraform {
      required_version = ">=1.0"
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~>3.0"
        }
        random = {
          source  = "hashicorp/random"
          version = "~>3.0"
        }
      }
    }
    provider "azurerm" {
      features {}
    }
    
  3. main.tf という名前のファイルを作成し、次のコードを挿入します。

    resource "random_pet" "rg_name" {
      prefix = var.resource_group_name_prefix
    }
    
    resource "azurerm_resource_group" "rg" {
      name     = random_pet.rg_name.id
      location = var.resource_group_location
    }
    
    resource "random_string" "windows_server_vm_hostname" {
      length  = 8
      lower   = true
      upper   = false
      special = false
    }
    
    resource "random_pet" "windows_server_public_ip_dns" {
      prefix = "dns"
    }
    
    resource "random_password" "password" {
      length  = 16
      special = true
      lower   = true
      upper   = true
      numeric = true
    }
    
    # The following module is a Terraform Verified Module. 
    # For more information about Verified Modules, see 
    # https://github.com/azure/terraform-azure-modules/
    module "windows_server" {
      count                         = 3 # Define 3 Windows Server VMs
      source                        = "Azure/compute/azurerm"
      resource_group_name           = azurerm_resource_group.rg.name
      vnet_subnet_id                = module.network.vnet_subnets[0]
      is_windows_image              = true
      vm_hostname                   = "vm-${random_string.windows_server_vm_hostname.result}-${count.index}"
      delete_os_disk_on_termination = true
      admin_password                = random_password.password.result
      vm_os_simple                  = "WindowsServer"
      public_ip_dns                 = ["${random_pet.windows_server_public_ip_dns.id}-${count.index}"]
    }
    
    # The following module is a Terraform Verified Module. 
    # For more information about Verified Modules, see 
    # https://github.com/azure/terraform-azure-modules/
    module "network" {
      source              = "Azure/network/azurerm"
      resource_group_name = azurerm_resource_group.rg.name
      version             = "5.2.0"
      subnet_prefixes     = ["10.0.1.0/24"]
      subnet_names        = ["subnet1"]
      use_for_each        = true
    }
    
  4. variables.tf という名前のファイルを作成し、次のコードを挿入します。

    variable "resource_group_location" {
      type        = string
      default     = "eastus"
      description = "Location for all resources."
    }
    
    variable "resource_group_name_prefix" {
      type        = string
      default     = "rg"
      description = "Prefix of the resource group name that's combined with a random value so name is unique in your Azure subscription."
    }
    
  5. outputs.tf という名前のファイルを作成し、次のコードを挿入します。

    output "resource_group_name" {
      value = azurerm_resource_group.rg.name
    }
    
    output "windows_vm_public_names" {
      value = module.windows_server[*].public_ip_dns_name
    }
    
    output "vm_public_ip_addresses" {
      value = module.windows_server[*].public_ip_address
    }
    
    output "vm_private_ip_addresses" {
      value = module.windows_server[*].network_interface_private_ip
    }
    
    output "vm_hostnames" {
      value = module.windows_server[*].vm_names
    }
    

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 を呼び出します。

Terraform での仮想マシン作成プロセス中のコスト情報は、Azure portal と同じようには表示されません。 仮想マシンのコストのしくみの詳細を知りたい場合は、コスト最適化の概要ページを参照してください。

結果を確認する

  1. Azure リソース グループ名を取得します。

    resource_group_name=$(terraform output -raw resource_group_name)
    
  2. JMESPath クエリを使用して az vm list を実行し、リソース グループに作成された仮想マシンの名前を表示します。

    az vm list \
      --resource-group $resource_group_name \
      --query "[].{\"VM Name\":name}" -o table
    

リソースをクリーンアップする

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

次のステップ