注意
Azure AI 搜尋服務 可透過 Azure 入口網站、REST API 及 Azure SDK 取得。 它同時也是 Foundry IQ 的基礎,這是一個管理式知識層,能將企業內容轉化為可重複使用、權限感知的知識庫,供 Microsoft Foundry 入口網站中的代理使用。
本文說明如何使用 Terraform 建立 Azure AI 搜尋服務 服務。
Terraform 可啟用雲端基礎結構的定義、預覽和部署。 使用 Terraform,您可以使用 HCL 語法建立組態檔。 HCL 語法可讓您指定雲端提供者 (例如 Azure) 和構成雲端基礎結構的元素。 建立組態檔之後,您會建立一個 執行計劃 ,讓您在部署基礎結構變更之前先預覽這些變更。 驗證變更之後,您可以套用執行計畫來部署基礎結構。
在本文中,您將學會如何:
- 用 random_pet 為Azure資源群名稱隨機建立一個寵物名
- 透過使用 azurerm_resource_group 建立 Azure 資源群組
- 用 random_string 建立隨機字串
- 透過使用 azurerm_search_service 建立Azure AI 搜尋服務服務
必要條件
實作 Terraform 程式碼
注意
欲了解更多資訊,請參閱 articles 及示範程式碼,說明如何使用 Terraform 管理Azure資源。
建立一個目錄來測試並執行範例 Terraform 程式碼。 將它設為目前目錄。
建立名為
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 }建立名為
outputs.tf的檔案,並插入下列程式碼:output "resource_group_name" { value = azurerm_resource_group.rg.name } output "azurerm_search_service_name" { value = azurerm_search_service.search.name }建立名為
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 {} }建立名為
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,不需要使用參數。
驗證結果
取得你建立 Azure AI 搜尋服務 服務的 Azure 資源名稱。
resource_group_name=$(terraform output -raw resource_group_name)取得 Azure AI 搜尋服務名稱。
azurerm_search_service_name=$(terraform output -raw azurerm_search_service_name)執行 az search service show 以顯示您在本文中建立的 Azure AI 搜尋服務。
az search service show --name $azurerm_search_service_name \ --resource-group $resource_group_name
清除資源
當您不再需要透過 Terraform 建立的資源時,請執行下列步驟:
執行 terraform 計劃 並指定
destroy旗標。terraform plan -destroy -out main.destroy.tfplan要點:
-
terraform plan命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。 - 選用的
-out參數可讓您指定計畫的輸出檔。 使用-out參數可確保您所檢閱的方案就是所套用的方案。
-
執行 terraform apply 以套用執行計劃。
terraform apply main.destroy.tfplan
對 Azure 上的 Terraform 進行疑難排解
針對在 Azure 上使用 Terraform 時的常見問題進行疑難解答