注释
本文中提供的信息特定于 基于中心的项目,不适用于 Foundry 项目。 有关详细信息,请参阅 项目类型。
在本文中,你将使用 Terraform 创建 Azure AI Foundry 中心、项目和 AI 服务连接。 中心是数据科学家和开发人员协作处理机器学习项目的中心位置。 它提供了一个共享协作空间,用于生成、训练和部署机器学习模型。 中心与 Azure 机器学习和其他 Azure 服务集成,使其成为机器学习任务的全面解决方案。 中心还允许管理和监视 AI 部署,确保它们按预期执行。
使用 Terraform 可以定义、预览和部署云基础结构。 使用 Terraform 时,请使用 HCL 语法来创建配置文件。 利用 HCL 语法,可指定 Azure 这样的云提供程序和构成云基础结构的元素。 创建配置文件后,请创建一个执行计划,利用该计划,可在部署基础结构更改之前先预览这些更改。 验证了更改后,请应用该执行计划以部署基础结构。
- 创建资源组
- 设置存储帐户
- 建立密钥保管库
- 配置 AI 服务
- 构建 Azure AI Foundry 中心
- 开发 Azure AI Foundry 项目
- 建立 AI 服务连接
先决条件
创建具有活动订阅的 Azure 帐户。 你可以免费创建一个帐户。
实现 Terraform 代码
注释
本文中的示例代码位于 Azure Terraform GitHub 存储库中。 你可以查看包含当前和以前 Terraform 版本的测试结果的日志文件。 可能需要更新模板中使用的资源提供程序版本才能使用最新的可用版本。
创建用于测试和运行示例 Terraform 代码的目录,并将其设为当前目录。
创建名为
providers.tf
的文件并插入下列代码。terraform { required_version = ">= 1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>4.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features { key_vault { recover_soft_deleted_key_vaults = false purge_soft_delete_on_destroy = false purge_soft_deleted_keys_on_destroy = false } resource_group { prevent_deletion_if_contains_resources = false } } }
创建名为
main.tf
的文件并插入下列代码。# Random pet to be used in resource group name resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } # Create a resource group resource "azurerm_resource_group" "example" { ___location = var.resource_group_location name = random_pet.rg_name.id } # Retrieve information about the current Azure client configuration data "azurerm_client_config" "current" {} # Generate random value for unique resource naming resource "random_string" "example" { length = 8 lower = true numeric = false special = false upper = false } # Create an Azure Key Vault resource resource "azurerm_key_vault" "example" { name = random_string.example.result # Name of the Key Vault ___location = azurerm_resource_group.example.___location # Location from the resource group resource_group_name = azurerm_resource_group.example.name # Resource group name tenant_id = data.azurerm_client_config.current.tenant_id # Azure tenant ID sku_name = "standard" # SKU tier for the Key Vault purge_protection_enabled = true # Enables purge protection to prevent accidental deletion } # Set an access policy for the Key Vault to allow certain operations resource "azurerm_key_vault_access_policy" "test" { key_vault_id = azurerm_key_vault.example.id # Key Vault reference tenant_id = data.azurerm_client_config.current.tenant_id # Tenant ID object_id = data.azurerm_client_config.current.object_id # Object ID of the principal key_permissions = [ # List of allowed key permissions "Create", "Get", "Delete", "Purge", "GetRotationPolicy", ] } # Create an Azure Storage Account resource "azurerm_storage_account" "example" { name = random_string.example.result # Storage account name ___location = azurerm_resource_group.example.___location # Location from the resource group resource_group_name = azurerm_resource_group.example.name # Resource group name account_tier = "Standard" # Performance tier account_replication_type = "LRS" # Locally-redundant storage replication } # Deploy Azure AI Services resource resource "azurerm_ai_services" "example" { name = "exampleaiservices" # AI Services resource name ___location = azurerm_resource_group.example.___location # Location from the resource group resource_group_name = azurerm_resource_group.example.name # Resource group name sku_name = "S0" # Pricing SKU tier custom_subdomain_name = "exampleaiservices" # Custom subdomain name } # Create Azure AI Foundry service resource "azurerm_ai_foundry" "example" { name = "exampleaihub" # AI Foundry service name ___location = azurerm_ai_services.example.___location # Location from the AI Services resource resource_group_name = azurerm_resource_group.example.name # Resource group name storage_account_id = azurerm_storage_account.example.id # Associated storage account key_vault_id = azurerm_key_vault.example.id # Associated Key Vault identity { type = "SystemAssigned" # Enable system-assigned managed identity } } # Create an AI Foundry Project within the AI Foundry service resource "azurerm_ai_foundry_project" "example" { name = "example" # Project name ___location = azurerm_ai_foundry.example.___location # Location from the AI Foundry service ai_services_hub_id = azurerm_ai_foundry.example.id # Associated AI Foundry service identity { type = "SystemAssigned" # Enable system-assigned managed identity } }
创建名为
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." }
创建名为
outputs.tf
的文件并插入下列代码。output "resource_group_name" { value = azurerm_resource_group.example.id } output "workspace_name" { value = azurerm_ai_foundry.example.name }
初始化 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
。
验证结果
获取 Azure 资源组名称。
resource_group_name=$(terraform output -raw resource_group_name)
获取工作区名称。
workspace_name=$(terraform output -raw workspace_name)
运行 az ml workspace show 以显示有关新工作区的信息。
az ml workspace show --resource-group $resource_group_name \ --name $workspace_name
清理资源
不再需要通过 Terraform 创建的资源时,请执行以下步骤:
运行 terraform plan 并指定
destroy
标志。terraform plan -destroy -out main.destroy.tfplan
要点:
terraform plan
命令将创建一个执行计划,但不会执行它。 相反,它会确定需要执行哪些操作,以创建配置文件中指定的配置。 此模式允许你在对实际资源进行任何更改之前验证执行计划是否符合预期。- 使用可选
-out
参数可以为计划指定输出文件。 使用-out
参数可以确保所查看的计划与所应用的计划完全一致。
运行 terraform apply 来应用执行计划。
terraform apply main.destroy.tfplan
Azure 上的 Terraform 故障排除
排查在 Azure 上使用 Terraform 时遇到的常见问题。