注
この記事の情報は ハブ ベースのプロジェクトに固有であり、 Foundry プロジェクトには適用されません。 「 自分が持っているプロジェクトの種類を確認する方法 」と 「ハブ ベースのプロジェクトを作成する」を参照してください。
この記事では、Terraform を使用して 、Azure AI Foundry ハブ、プロジェクト、および AI サービス接続を作成します。 ハブは、データ サイエンティストと開発者が機械学習プロジェクトで共同作業を行うための中心的な場所です。 機械学習モデルを構築、トレーニング、デプロイするための共有の共同作業スペースが提供されます。 ハブは Azure Machine Learning やその他の Azure サービスと統合されており、機械学習タスクの包括的なソリューションとなります。 また、ハブを使用すると、AI デプロイを管理および監視し、期待どおりに実行されるようにすることができます。
Terraform を使用すると、クラウド インフラストラクチャの定義、プレビュー、デプロイが可能になります。 Terraform を使用して、 HCL 構文を使用して構成ファイルを作成します。 HCL 構文を使用すると、クラウド プロバイダー (Azure など) とクラウド インフラストラクチャを構成する要素を指定できます。 構成ファイルを作成したら、インフラストラクチャの変更をデプロイする前にプレビューできる 実行プラン を作成します。 変更を確認したら、実行プランを適用してインフラストラクチャをデプロイします。
- リソース グループを作成する
- ストレージ アカウントを設定する
- キー コンテナーを確立する
- AI サービスを構成する
- Azure AI Foundry ハブを構築する
- Azure AI Foundry プロジェクトを開発する
- AI サービス接続を確立する
[前提条件]
アクティブなサブスクリプションがある Azure アカウントを作成します。 アカウントは無料で作成できます。
Terraform コードを実装する
注
この記事のサンプル コードは、 Azure Terraform GitHub リポジトリにあります。 Terraform の現在および以前のバージョンからのテスト結果を含むログ ファイルを表示できます。 使用可能な最新バージョンを使用するには、テンプレートで使用されているリソース プロバイダーのバージョンを更新する必要がある場合があります。
Terraform を使用して Azure リソースを管理する方法を示すその他の記事とサンプル コードを参照してください
サンプルの 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 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 リソース グループ名を取得します。
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 プランを実行し、
destroy
フラグを指定します。terraform plan -destroy -out main.destroy.tfplan
重要なポイント:
-
terraform plan
コマンドは実行プランを作成しますが、実行はしません。 代わりに、それは設定ファイルで指定された設定を作成するために必要な手順を決定します。 このパターンを使用すると、実際のリソースに変更を加える前に、実行プランが期待と一致するかどうかを確認できます。 - 任意の
-out
パラメーターを使用すると、プランの出力ファイルを指定することができます。-out
パラメーターを使用すると、レビューしたプランがそのまま適用されることが保証されます。
-
terraform apply を実行して実行プランを適用します。
terraform apply main.destroy.tfplan
Azure での Terraform のトラブルシューティング
Azure で Terraform を使用する場合の一般的な問題のトラブルシューティングを行います。