本文介绍如何使用 Terraform 创建 Azure AI 搜索服务。
使用 Terraform 可以定义、预览和部署云基础结构。 使用 Terraform 时,请使用 HCL 语法来创建配置文件。 HCL 语法允许你指定云提供商(如 Azure)和构成云基础结构的元素。 创建配置文件后,请创建一个执行计划,利用该计划,可在部署基础结构更改之前先预览这些更改。 验证更改后,应用执行计划来部署基础结构。
在这篇文章中,你将学会如何:
- 使用 random_pet 为 Azure 资源组名称创建随机宠物名称
- 使用 azurerm_resource_group 创建 Azure 资源组
- 使用random_string创建随机字符串
- 使用 azurerm_search_service 创建 Azure AI 搜索服务
先决条件
实现 Terraform 代码
创建用于测试和运行示例 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 = "chinaeast" } 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 plan 以创建执行计划。
terraform plan -out main.tfplan
应用 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 plan 并指定
destroy标志。terraform plan -destroy -out main.destroy.tfplan运行 terraform apply 来应用执行计划。
terraform apply main.destroy.tfplan
Azure 上的 Terraform 故障排除
排查在 Azure 上使用 Terraform 时遇到的常见问题