クイック スタート: Terraform を使用して Azure AI 検索 Serviceをデプロイする

注

Azure AI 検索は、Azure ポータル、REST API、およびAzure SDKから使用できます。 また、Foundry IQ は、エンタープライズ コンテンツを、Microsoft Foundry ポータルのエージェントの再利用可能なアクセス許可に対応したナレッジ ベースに変換するマネージド ナレッジ レイヤーです。

この記事では、Terraform を使用して Terraform を使用してAzure AI 検索 サービスを作成する方法について説明します。

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

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

  • random_petを使用して、Azure リソース グループ名のランダムなペット名を作成します
  • azurerm_resource_groupを使用して Azure リソース グループを作成する
  • random_stringを使用してランダムな文字列を作成する
  • azurerm_search_serviceを使用してAzure AI 検索 サービスを作成する

前提条件

Terraform コードを実装する

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

  2. 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" "azurerm_search_service_name" {
      length  = 25
      upper   = false
      numeric = false
      special = false
    }
    
    resource "azurerm_search_service" "search" {
      name                = random_string.azurerm_search_service_name.result
      resource_group_name = azurerm_resource_group.rg.name
      location            = azurerm_resource_group.rg.location
      sku                 = var.sku
      replica_count       = var.replica_count
      partition_count     = var.partition_count
    }
    
  3. outputs.tf という名前のファイルを作成し、次のコードを挿入します。

    output "resource_group_name" {
      value = azurerm_resource_group.rg.name
    }
    
    output "azurerm_search_service_name" {
      value = azurerm_search_service.search.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
      description = "Location for all resources."
      default     = "eastus"
    }
    
    variable "resource_group_name_prefix" {
      type        = string
      description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
      default     = "rg"
    }
    
    variable "sku" {
      description = "The pricing tier of the search service you want to create (for example, basic or standard)."
      default     = "standard"
      type        = string
      validation {
        condition     = contains(["free", "basic", "standard", "standard2", "standard3", "storage_optimized_l1", "storage_optimized_l2"], var.sku)
        error_message = "The sku must be one of the following values: free, basic, standard, standard2, standard3, storage_optimized_l1, storage_optimized_l2."
      }
    }
    
    variable "replica_count" {
      type        = number
      description = "Replicas distribute search workloads across the service. You need at least two replicas to support high availability of query workloads (not applicable to the free tier)."
      default     = 1
      validation {
        condition     = var.replica_count >= 1 && var.replica_count <= 12
        error_message = "The replica_count must be between 1 and 12."
      }
    }
    
    variable "partition_count" {
      type        = number
      description = "Partitions allow for scaling of document count as well as faster indexing by sharding your index over multiple search units."
      default     = 1
      validation {
        condition     = contains([1, 2, 3, 4, 6, 12], var.partition_count)
        error_message = "The partition_count must be one of the following values: 1, 2, 3, 4, 6, 12."
      }
    }
    

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

結果を確認する

  1. Azure AI 検索 サービスを作成したAzure リソース名を取得します。

    resource_group_name=$(terraform output -raw resource_group_name)
    
  2. Azure AI 検索 サービス名を取得する。

    azurerm_search_service_name=$(terraform output -raw azurerm_search_service_name)
    
  3. az search service show を実行して、この記事で作成した Azure AI 検索 サービスを表示します。

    az search service show --name $azurerm_search_service_name \
                           --resource-group $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 を使用するときの一般的な問題のトラブルシューティング

次のステップ