使用 Terraform 來管理 Azure Machine Learning 工作區

在本文中,您將了解如何使用 Terraform 設定檔來建立 Azure Machine Learning 工作區。 Terraform (部分機器翻譯) 的範本型設定檔讓您能夠以可重複且可預測的方式定義、建立和設定 Azure 資源。 Terraform 會追蹤資源狀態,而且可以清除和終結資源。

Terraform 設定檔是定義了部署所需資源的文件。 Terraform 設定也可以指定部署變數,以用來在套用設定時提供輸入值。

必要條件

  • 具有免費或付費版 Azure Machine Learning 的 Azure 訂用帳戶。 如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶
  • 根據快速入門:安裝和設定 Terraform (部分機器翻譯) 中的指示安裝及設定好 Terraform。

限制

  • 建立新的工作區時,您可以自動建立工作區所需的服務,或使用現有的服務。 若要使用有別於工作區的不同 Azure 訂閱的現有服務,您必須在包含這些服務的訂閱中註冊 Azure Machine Learning 命名空間。 例如,若要在訂用帳戶 A (使用訂用帳戶 B 的儲存體帳戶) 中建立工作區,則必須先在訂用帳戶 B 中註冊 Azure Machine Learning 命名空間,然後工作區才能使用該儲存體帳戶。

    Azure Machine Learning 的資源提供者為 Microsoft.MachineLearningServices。 如需查看是否已註冊或正在註冊它的相關資訊,請參閱 Azure 資源提供者和類型

    重要

    此資訊僅適用建立工作區期間所提供的資源:Azure 儲存體帳戶、Azure Container Registry、Azure Key Vault 和 Application Insights。

  • 下列限制適用於在工作區建立期間所建立的 Application Insights 執行個體:

    提示

    當您建立工作區時,會建立 Azure Application Insights 執行個體。 建立叢集後,您可以視需要刪除 Application Insights 執行個體。 刪除 Application Insights 執行個體會限制從工作區收集的資訊,且可能會使問題的疑難排解變得較為困難。 如果您刪除工作區所建立的 Application Insights 執行個體,重新建立的唯一方式是刪除並重新建立工作區

    如需關於使用 Application Insights 執行個體的詳細資訊,請參閱監視和收集 Machine Learning Web 服務端點的資料

建立工作區

建立名為 main.tf 且具有下列程式碼的檔案。

data "azurerm_client_config" "current" {}

resource "azurerm_resource_group" "default" {
  name     = "${random_pet.prefix.id}-rg"
  location = var.location
}

resource "random_pet" "prefix" {
  prefix = var.prefix
  length = 2
}

resource "random_integer" "suffix" {
  min = 10000000
  max = 99999999
}

在名為 providers.tf 且具有下列程式碼的檔案中宣告 Azure 提供者。

terraform {
  required_version = ">= 1.0"

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">= 3.0, < 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
    }
  }
}

設定工作區

若要建立 Azure Machine Learning 工作區,請使用下列其中一個 Terraform 設定。 Azure Machine Learning 工作區需要各種其他服務來作為相依性。 範本會指定這些相關聯的資源 (部分機器翻譯)。 視您的需求而定,您可以選擇使用範本來建立具有公用或私人網路連線能力的資源。

注意

Azure 中的某些資源需要全域唯一的名稱。 在部署資源之前,請務必將 name 變數設定為唯一值。

下列設定會建立具有公用網路連線能力的工作區。

在名為 variables.tf 的檔案中定義下列變數。

variable "environment" {
  type        = string
  description = "Name of the environment"
  default     = "dev"
}

variable "location" {
  type        = string
  description = "Location of the resources"
  default     = "eastus"
}

variable "prefix" {
  type        = string
  description = "Prefix of the resource name"
  default     = "ml"
}

在名為 workspace.tf 的檔案中定義下列工作區設定:

# Dependent resources for Azure Machine Learning
resource "azurerm_application_insights" "default" {
  name                = "${random_pet.prefix.id}-appi"
  location            = azurerm_resource_group.default.location
  resource_group_name = azurerm_resource_group.default.name
  application_type    = "web"
}

resource "azurerm_key_vault" "default" {
  name                     = "${var.prefix}${var.environment}${random_integer.suffix.result}kv"
  location                 = azurerm_resource_group.default.location
  resource_group_name      = azurerm_resource_group.default.name
  tenant_id                = data.azurerm_client_config.current.tenant_id
  sku_name                 = "premium"
  purge_protection_enabled = false
}

resource "azurerm_storage_account" "default" {
  name                            = "${var.prefix}${var.environment}${random_integer.suffix.result}st"
  location                        = azurerm_resource_group.default.location
  resource_group_name             = azurerm_resource_group.default.name
  account_tier                    = "Standard"
  account_replication_type        = "GRS"
  allow_nested_items_to_be_public = false
}

resource "azurerm_container_registry" "default" {
  name                = "${var.prefix}${var.environment}${random_integer.suffix.result}cr"
  location            = azurerm_resource_group.default.location
  resource_group_name = azurerm_resource_group.default.name
  sku                 = "Premium"
  admin_enabled       = true
}

# Machine Learning workspace
resource "azurerm_machine_learning_workspace" "default" {
  name                          = "${random_pet.prefix.id}-mlw"
  location                      = azurerm_resource_group.default.location
  resource_group_name           = azurerm_resource_group.default.name
  application_insights_id       = azurerm_application_insights.default.id
  key_vault_id                  = azurerm_key_vault.default.id
  storage_account_id            = azurerm_storage_account.default.id
  container_registry_id         = azurerm_container_registry.default.id
  public_network_access_enabled = true

  identity {
    type = "SystemAssigned"
  }
}

建立並套用方案

若要建立工作區,請執行下列程式碼:

terraform init

terraform plan \
        # -var <any of the variables set in variables.tf> \
          -out demo.tfplan

terraform apply "demo.tfplan"

針對資源提供者錯誤進行疑難排解

建立 Azure Machine Learning 工作區或工作區所使用的資源時,您可能會收到類似下列訊息的錯誤:

  • No registered resource provider found for location {location}
  • The subscription is not registered to use namespace {resource-provider-namespace}

大部分資源提供者都會自動註冊,但並非全部。 如果您收到此訊息,則需要註冊先前提及的提供者。

下表包含 Azure Machine Learning 所需的資源提供者清單:

資源提供者 需要的原因
Microsoft.MachineLearningServices 建立 Azure Machine Learning 工作區。
Microsoft.Storage Azure 儲存體帳戶用來做為工作區的預設儲存體。
Microsoft.ContainerRegistry 工作區會使用 Azure Container Registry 來建置 Docker 映像。
Microsoft.KeyVault 工作區會使用 Azure Key Vault 來儲存秘密。
Microsoft.Notebooks Azure Machine Learning 計算執行個體上的整合式筆記本。
Microsoft.ContainerService 若您打算將已定型的模型部署至 Azure Kubernetes Service。

若您打算搭配 Azure Machine Learning 使用客戶自控金鑰,則必須註冊下列服務提供者:

資源提供者 需要的原因
Microsoft.DocumentDB 記錄工作區中繼資料的 Azure CosmosDB 執行個體。
Microsoft.Search Azure 搜尋可為工作區提供索引編製功能。

如果您打算搭配 Azure Machine Learning 使用受控虛擬網路,則必須註冊 Microsoft.Network 資源提供者。 建立受控虛擬網路的私人端點時,工作區會使用此資源提供者。

如需有關註冊資源提供者的詳細資訊,請參閱解決資源提供者註冊的錯誤