[アーティクル] 06/05/2023
9 人の共同作成者
フィードバック
この記事の内容
この記事では、Terraform の構成ファイルを使用して、Azure Machine Learning ワークスペースを作成して管理する方法について説明します。 Terraform のテンプレート ベースの構成ファイルを使用すると、Azure リソースの定義、作成、構成を反復的および計画的に行うことができます。 Terraform によって、リソースの状態が追跡され、リソースをクリーンアップおよび破棄できます。
Terraform の構成は、デプロイに必要なリソースが定義されているドキュメントです。 デプロイ変数を指定することもできます。 変数は、構成の使用時に入力値を指定するために使用されます。
前提条件
制限事項
新しいワークスペースを作成する場合は、ワークスペースに必要なサービスを自動的に作成するか、既存のサービスを使用することができます。 ワークスペースとは異なる Azure サブスクリプション の既存のサービス を使用する場合は、それらのサービスを含むサブスクリプションに Azure Machine Learning 名前空間を登録する必要があります。 たとえば、サブスクリプション B のストレージ アカウントを使用するサブスクリプション A でワークスペースを作成する場合、ワークスペースでストレージ アカウントを使用するには、Azure Machine Learning 名前空間がサブスクリプション B に登録されている必要があります。
Azure Machine Learning のリソース プロバイダーは、Microsoft.MachineLearningServices です。 登録されているかどうかを確認する方法や、登録方法については、記事「Azure リソース プロバイダーと種類 」を参照してください。
重要
これは、ワークスペースの作成時に提供されるリソースである、Azure Storage アカウント、Azure Container Register、Azure Key Vault、Application Insights にのみ適用されます。
ヒント
Azure Application Insights インスタンスは、ワークスペースの作成時に作成されます。 必要に応じて、クラスターの作成後に Application Insights インスタンスを削除できます。 それを削除すると、ワークスペースから収集される情報が限定され、問題のトラブルシューティングが困難になる可能性があります。 ワークスペースによって作成された Application Insights インスタンスを削除した場合、ワークスペースを削除して再作成することなく、それを再作成することはできません。
この Application Insights インスタンスの使用方法の詳細については、「Machine Learning Web サービス エンドポイントからのデータの監視と収集 」を参照してください。
Azure プロバイダーを宣言する
Azure プロバイダーを宣言する Terraform 構成ファイルを作成します。
main.tf
という名前で新しいファイルを作成します。 Azure Cloud Shell で作業している場合は、bash を使用します。
code main.tf
以下のコードをエディターに貼り付けます。
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
}
ファイルを保存し (<Ctrl>S キー)、エディターを終了します (<Ctrl>Q キー)。
ワークスペースをデプロイする
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"
}
}
次の構成を使用すると、Azure Private Link エンドポイントを使用して分離されたネットワーク環境にワークスペースが作成されます。 プライベート DNS ゾーン が含まれているので、仮想ネットワーク内でドメイン名を解決できます。
Azure の一部のリソースには、グローバルに一意の名前が必要です。 次のテンプレートを使用してリソースをデプロイする前に、resourceprefix
変数に一意の値を設定します。
Azure Container Registry と Azure Machine Learning の両方にプライベート リンク エンドポイントを使用すると、環境 イメージのビルドに Azure Container Registry タスクを使用できません。 代わりに、Azure Machine Learning コンピューティング クラスターを使用してイメージをビルドできます。 使用するクラスター名を構成するには、image_build_compute_name 引数を設定します。 public_network_access_enabled 引数を使用すると、プライベート リンク エンドポイントを持つワークスペースへのパブリック アクセスを許可する ように構成できます。
variables.tf :
variable "name" {
type = string
description = "Name of the deployment"
default = "examplehost"
}
variable "environment" {
type = string
description = "Name of the environment"
default = "dev"
}
variable "location" {
type = string
description = "Location of the resources"
default = "East US"
}
variable "vnet_address_space" {
type = list(string)
description = "Address space of the virtual network"
default = ["10.0.0.0/16"]
}
variable "training_subnet_address_space" {
type = list(string)
description = "Address space of the training subnet"
default = ["10.0.1.0/24"]
}
variable "aks_subnet_address_space" {
type = list(string)
description = "Address space of the aks subnet"
default = ["10.0.2.0/23"]
}
variable "ml_subnet_address_space" {
type = list(string)
description = "Address space of the ML workspace subnet"
default = ["10.0.0.0/24"]
}
variable "dsvm_subnet_address_space" {
type = list(string)
description = "Address space of the DSVM subnet"
default = ["10.0.4.0/24"]
}
variable "bastion_subnet_address_space" {
type = list(string)
description = "Address space of the bastion subnet"
default = ["10.0.5.0/24"]
}
variable "image_build_compute_name" {
type = string
description = "Name of the compute cluster to be created and set to build docker images"
default = "image-builder"
}
# DSVM Variables
variable "dsvm_name" {
type = string
description = "Name of the Data Science VM"
default = "vmdsvm01"
}
variable "dsvm_admin_username" {
type = string
description = "Admin username of the Data Science VM"
default = "azureadmin"
}
variable "dsvm_host_password" {
type = string
description = "Password for the admin username of the Data Science VM"
default = "ChangeMe123!"
sensitive = true
}
workspace.tf :
# Dependent resources for Azure Machine Learning
resource "azurerm_application_insights" "default" {
name = "appi-${var.name}-${var.environment}"
location = azurerm_resource_group.default.location
resource_group_name = azurerm_resource_group.default.name
application_type = "web"
}
resource "random_string" "kv_prefix" {
length = 4
upper = false
special = false
numeric = false
}
resource "azurerm_key_vault" "default" {
name = "kv-${random_string.kv_prefix.result}-${var.environment}"
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 = true
network_acls {
default_action = "Deny"
bypass = "AzureServices"
}
}
resource "random_string" "sa_prefix" {
length = 4
upper = false
special = false
numeric = false
}
resource "azurerm_storage_account" "default" {
name = "st${random_string.sa_prefix.result}${var.environment}"
location = azurerm_resource_group.default.location
resource_group_name = azurerm_resource_group.default.name
account_tier = "Standard"
account_replication_type = "GRS"
network_rules {
default_action = "Deny"
bypass = ["AzureServices"]
}
}
resource "azurerm_container_registry" "default" {
name = "cr${var.name}${var.environment}"
location = azurerm_resource_group.default.location
resource_group_name = azurerm_resource_group.default.name
sku = "Premium"
admin_enabled = true
network_rule_set {
default_action = "Deny"
}
public_network_access_enabled = false
}
# Machine Learning workspace
resource "azurerm_machine_learning_workspace" "default" {
name = "mlw-${var.name}-${var.environment}"
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
identity {
type = "SystemAssigned"
}
# Args of use when using an Azure Private Link configuration
public_network_access_enabled = false
image_build_compute_name = var.image_build_compute_name
depends_on = [
azurerm_private_endpoint.kv_ple,
azurerm_private_endpoint.st_ple_blob,
azurerm_private_endpoint.storage_ple_file,
azurerm_private_endpoint.cr_ple,
azurerm_subnet.snet-training
]
}
# Private endpoints
resource "azurerm_private_endpoint" "kv_ple" {
name = "ple-${var.name}-${var.environment}-kv"
location = azurerm_resource_group.default.location
resource_group_name = azurerm_resource_group.default.name
subnet_id = azurerm_subnet.snet-workspace.id
private_dns_zone_group {
name = "private-dns-zone-group"
private_dns_zone_ids = [azurerm_private_dns_zone.dnsvault.id]
}
private_service_connection {
name = "psc-${var.name}-kv"
private_connection_resource_id = azurerm_key_vault.default.id
subresource_names = ["vault"]
is_manual_connection = false
}
}
resource "azurerm_private_endpoint" "st_ple_blob" {
name = "ple-${var.name}-${var.environment}-st-blob"
location = azurerm_resource_group.default.location
resource_group_name = azurerm_resource_group.default.name
subnet_id = azurerm_subnet.snet-workspace.id
private_dns_zone_group {
name = "private-dns-zone-group"
private_dns_zone_ids = [azurerm_private_dns_zone.dnsstorageblob.id]
}
private_service_connection {
name = "psc-${var.name}-st"
private_connection_resource_id = azurerm_storage_account.default.id
subresource_names = ["blob"]
is_manual_connection = false
}
}
resource "azurerm_private_endpoint" "storage_ple_file" {
name = "ple-${var.name}-${var.environment}-st-file"
location = azurerm_resource_group.default.location
resource_group_name = azurerm_resource_group.default.name
subnet_id = azurerm_subnet.snet-workspace.id
private_dns_zone_group {
name = "private-dns-zone-group"
private_dns_zone_ids = [azurerm_private_dns_zone.dnsstoragefile.id]
}
private_service_connection {
name = "psc-${var.name}-st"
private_connection_resource_id = azurerm_storage_account.default.id
subresource_names = ["file"]
is_manual_connection = false
}
}
resource "azurerm_private_endpoint" "cr_ple" {
name = "ple-${var.name}-${var.environment}-cr"
location = azurerm_resource_group.default.location
resource_group_name = azurerm_resource_group.default.name
subnet_id = azurerm_subnet.snet-workspace.id
private_dns_zone_group {
name = "private-dns-zone-group"
private_dns_zone_ids = [azurerm_private_dns_zone.dnscontainerregistry.id]
}
private_service_connection {
name = "psc-${var.name}-cr"
private_connection_resource_id = azurerm_container_registry.default.id
subresource_names = ["registry"]
is_manual_connection = false
}
}
resource "azurerm_private_endpoint" "mlw_ple" {
name = "ple-${var.name}-${var.environment}-mlw"
location = azurerm_resource_group.default.location
resource_group_name = azurerm_resource_group.default.name
subnet_id = azurerm_subnet.snet-workspace.id
private_dns_zone_group {
name = "private-dns-zone-group"
private_dns_zone_ids = [azurerm_private_dns_zone.dnsazureml.id, azurerm_private_dns_zone.dnsnotebooks.id]
}
private_service_connection {
name = "psc-${var.name}-mlw"
private_connection_resource_id = azurerm_machine_learning_workspace.default.id
subresource_names = ["amlworkspace"]
is_manual_connection = false
}
}
# Compute cluster for image building required since the workspace is behind a vnet.
# For more details, see https://docs.microsoft.com/en-us/azure/machine-learning/tutorial-create-secure-workspace#configure-image-builds.
resource "azurerm_machine_learning_compute_cluster" "image-builder" {
name = var.image_build_compute_name
location = azurerm_resource_group.default.location
vm_priority = "LowPriority"
vm_size = "Standard_DS2_v2"
machine_learning_workspace_id = azurerm_machine_learning_workspace.default.id
subnet_resource_id = azurerm_subnet.snet-training.id
scale_settings {
min_node_count = 0
max_node_count = 3
scale_down_nodes_after_idle_duration = "PT15M" # 15 minutes
}
identity {
type = "SystemAssigned"
}
}
network.tf :
# Virtual Network
resource "azurerm_virtual_network" "default" {
name = "vnet-${var.name}-${var.environment}"
address_space = var.vnet_address_space
location = azurerm_resource_group.default.location
resource_group_name = azurerm_resource_group.default.name
}
resource "azurerm_subnet" "snet-training" {
name = "snet-training"
resource_group_name = azurerm_resource_group.default.name
virtual_network_name = azurerm_virtual_network.default.name
address_prefixes = var.training_subnet_address_space
enforce_private_link_endpoint_network_policies = true
}
resource "azurerm_subnet" "snet-aks" {
name = "snet-aks"
resource_group_name = azurerm_resource_group.default.name
virtual_network_name = azurerm_virtual_network.default.name
address_prefixes = var.aks_subnet_address_space
enforce_private_link_endpoint_network_policies = true
}
resource "azurerm_subnet" "snet-workspace" {
name = "snet-workspace"
resource_group_name = azurerm_resource_group.default.name
virtual_network_name = azurerm_virtual_network.default.name
address_prefixes = var.ml_subnet_address_space
enforce_private_link_endpoint_network_policies = true
}
# ...
# For full reference, see: https://github.com/Azure/terraform/blob/master/quickstart/201-machine-learning-moderately-secure/network.tf
プライベート リンク エンドポイント ワークスペースに接続するには、複数のオプションがあります。 これらのオプションの詳細については、「ワークスペースに安全に接続する 」を参照してください。
トラブルシューティング
リソース プロバイダーのエラー
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 Storage アカウントは、ワークスペースの既定のストレージとして使用されます。
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 Search では、ワークスペースのインデックス作成機能を提供します。
Azure Machine Learning でマネージド仮想ネットワークを使用する場合は、Microsoft.Network リソース プロバイダーを登録する必要があります。 このリソース プロバイダーは、マネージド仮想ネットワークのプライベート エンドポイントを作成するときにワークスペースによって使用されます。
リソースプロバイダーの登録については、「リソースプロバイダー登録エラーの解決 」を参照してください。
次の手順