次の方法で共有


開始タスクで 1 つの Azure Batch アカウントと 2 つのプールをデプロイする - Terraform

このクイック スタートでは、Terraform を使用して、1 つの Azure Batch アカウント、1 つの Azure Storage アカウント、2 つの Batch プールを作成します。 Batch は、大量のデータの処理を並列化して多数のコンピューターに分散させるクラウドベースのジョブ スケジューリング サービスです。 通常、3D グラフィックスのレンダリング、大規模なデータセットの分析、ビデオの処理などのタスクに使用されます。 この場合、作成されるリソースには、Batch アカウント (分散処理タスクのための中央整理エンティティ)、処理するデータを保持するための Azure Storage アカウント、タスクを実行する仮想マシンのグループである 2 つの Batch プールが含まれます。

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

  • Terraform の必要なバージョンと必要なプロバイダーを指定します。
  • 追加機能を使用せずに Azure プロバイダーを定義します。
  • リソース グループの場所と名前プレフィックスの変数を定義します。
  • Azure リソース グループのランダムな名前を生成します。
  • 指定した場所に生成された名前を持つリソース グループを作成します。
  • ストレージ アカウント名のランダムな文字列を生成します。
  • 作成されたリソース グループに生成された名前を持つストレージ アカウントを作成します。
  • Batch アカウント名のランダムな文字列を生成します。
  • 作成されたリソース グループに生成された名前を持つ Batch アカウントを作成し、作成されたストレージ アカウントにリンクします。
  • Batch プールのランダムな名前を生成します。
  • 作成されたリソース グループに固定スケーリングの Batch プールを作成し、作成された Batch アカウントにリンクします。
  • 作成されたリソース グループに自動スケーリングの Batch プールを作成し、作成された Batch アカウントにリンクします。
  • 作成されたリソース グループ、ストレージ アカウント、Batch アカウント、両方の Batch プールの名前を出力します。

前提条件

Terraform コードを実装する

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

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

    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
    }
    
    resource "random_string" "storage_account_name" {
      length  = 8
      lower   = true
      numeric = false
      special = false
      upper   = false
    }
    
    resource "azurerm_storage_account" "example" {
      name                     = random_string.storage_account_name.result
      resource_group_name      = azurerm_resource_group.rg.name
      ___location                 = azurerm_resource_group.rg.___location
      account_tier             = "Standard"
      account_replication_type = "LRS"
    }
    
    resource "random_string" "batch_account_name" {
      length  = 8
      lower   = true
      numeric = false
      special = false
      upper   = false
    }
    
    resource "azurerm_batch_account" "example" {
      name                                = random_string.batch_account_name.result
      resource_group_name                 = azurerm_resource_group.rg.name
      ___location                            = azurerm_resource_group.rg.___location
      storage_account_id                  = azurerm_storage_account.example.id
      storage_account_authentication_mode = "StorageKeys"
    }
    
    resource "random_pet" "azurerm_batch_pool_name" {
      prefix = "pool"
    }
    
    resource "azurerm_batch_pool" "fixed" {
      name                = "${random_pet.azurerm_batch_pool_name.id}-fixed-pool"
      resource_group_name = azurerm_resource_group.rg.name
      account_name        = azurerm_batch_account.example.name
      display_name        = "Fixed Scale Pool"
      vm_size             = "Standard_D4_v3"
      node_agent_sku_id   = "batch.node.ubuntu 22.04"
    
      fixed_scale {
        target_dedicated_nodes = 2
        resize_timeout         = "PT15M"
      }
    
      storage_image_reference {
        publisher = "Canonical"
        offer     = "0001-com-ubuntu-server-jammy"
        sku       = "22_04-lts"
        version   = "latest"
      }
    
      start_task {
        command_line       = "echo 'Hello World from $env'"
        task_retry_maximum = 1
        wait_for_success   = true
    
        common_environment_properties = {
          env = "TEST"
        }
    
        user_identity {
          auto_user {
            elevation_level = "NonAdmin"
            scope           = "Task"
          }
        }
      }
    
      metadata = {
        "tagName" = "Example tag"
      }
    }
    
    resource "azurerm_batch_pool" "autopool" {
      name                = "${random_pet.azurerm_batch_pool_name.id}-autoscale-pool"
      resource_group_name = azurerm_resource_group.rg.name
      account_name        = azurerm_batch_account.example.name
      display_name        = "Auto Scale Pool"
      vm_size             = "Standard_D4_v3"
      node_agent_sku_id   = "batch.node.ubuntu 22.04"
    
      auto_scale {
        evaluation_interval = "PT15M"
    
        formula = <<EOF
          startingNumberOfVMs = 1;
          maxNumberofVMs = 25;
          pendingTaskSamplePercent = $PendingTasks.GetSamplePercent(180 * TimeInterval_Second);
          pendingTaskSamples = pendingTaskSamplePercent < 70 ? startingNumberOfVMs : avg($PendingTasks.GetSample(180 * TimeInterval_Second));
          $TargetDedicatedNodes=min(maxNumberofVMs, pendingTaskSamples);
    EOF
      }
    
      storage_image_reference {
        publisher = "Canonical"
        offer     = "0001-com-ubuntu-server-jammy"
        sku       = "22_04-lts"
        version   = "latest"
      }
    }
    
  3. outputs.tf という名前のファイルを作成し、次のコードを挿入します。

    output "resource_group_name" {
      value = azurerm_resource_group.rg.name
    }
    
    output "storage_account_name" {
      value = azurerm_storage_account.example.name
    }
    
    output "batch_account_name" {
      value = azurerm_batch_account.example.name
    }
    
    output "batch_pool_fixed_name" {
      value = azurerm_batch_pool.fixed.name
    }
    
    output "batch_pool_autopool_name" {
      value = azurerm_batch_pool.autopool.name
    }
    
  4. 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 {}
    }
    
  5. variables.tf という名前のファイルを作成し、次のコードを挿入します。

    variable "resource_group_location" {
      type        = string
      default     = "eastus"
      description = "Location of the resource group."
    }
    
    variable "resource_group_name_prefix" {
      type        = string
      default     = "rg"
      description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
    }
    

Terraform を初期化する

terraform init 実行して、Terraform デプロイを初期化します。 このコマンドによって、Azure リソースを管理するために必要な Azure プロバイダーがダウンロードされます。

terraform init -upgrade

重要なポイント:

  • -upgrade パラメーターは、必要なプロバイダー プラグインを、構成のバージョン制約に準拠する最新バージョンにアップグレードします。

Terraform 実行プランを作成する

terraform プランを実行して実行プランを作成します。

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

結果を確認する

az batch account show を実行して Batch アカウントを表示します。

az batch account show --name <batch_account_name> --resource-group <resource_group_name>

上記のコマンドで、<batch_account_name> を作成する Batch アカウントの名前に置き換え、<resource_group_name> を作成するリソース グループの名前に置き換えます。

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

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

次のステップ