다음을 통해 공유


Terraform을 사용하여 Azure AI 스튜디오 허브 만들기

이 문서에서는 Terraform을 사용하여 Azure AI Studio 허브, 프로젝트 및 AI 서비스 연결을 만듭니다. 허브는 데이터 과학자와 개발자가 기계 학습 프로젝트에서 공동 작업할 수 있는 중심 장소입니다. 기계 학습 모델을 빌드, 학습 및 배포하는 공유 공동 작업 공간을 제공합니다. 허브는 Azure Machine Learning 및 기타 Azure 서비스와 통합되어 기계 학습 작업을 위한 포괄적인 솔루션입니다. 또한 허브를 사용하면 AI 배포를 관리하고 모니터링하여 예상대로 수행되도록 할 수 있습니다.

Terraform은 클라우드 인프라의 정의, 미리 보기 및 배포를 사용합니다. Terraform을 사용하는 경우 HCL 구문를 사용하여 구성 파일을 만듭니다. HCL 구문을 사용하면 클라우드 공급자(예: Azure) 그리고 클라우드 인프라를 구성하는 요소를 지정할 수 있습니다. 구성 파일을 만든 후 배포되기 전에 인프라 변경을 미리 볼 수 있는 실행 계획를 만듭니다. 변경 내용을 확인 한 후에는 실행 계획을 적용하여 인프라를 배포합니다.

  • 리소스 그룹 만들기
  • 스토리지 계정 설정
  • 키 자격 증명 모음 설정
  • AI 서비스 구성
  • Azure AI 허브 빌드
  • AI 프로젝트 개발
  • AI 서비스 연결 설정

필수 조건

Terraform 코드 구현

  1. 샘플 Terraform 코드를 테스트 및 실행할 디렉터리를 만들고 현재 디렉터리로 만듭니다.

  2. providers.tf라는 파일을 만들고 다음 코드를 삽입합니다.

    terraform {
      required_version = ">= 1.0"
    
      required_providers { 
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~>3.0"
        }
        azapi = {
          source  = "azure/azapi"
        }
        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
        }
      }
    }
    
    provider "azapi" {
    }
    
  3. main.tf라는 파일을 만들고 다음 코드를 삽입합니다.

    resource "random_pet" "rg_name" { 
      prefix = var.resource_group_name_prefix
    }
    
    // RESOURCE GROUP
    resource "azurerm_resource_group" "rg" {
      location = var.resource_group_location
      name     = random_pet.rg_name.id
    }
    
    data "azurerm_client_config" "current" {
    }
    
    // STORAGE ACCOUNT
    resource "azurerm_storage_account" "default" {
      name                            = "${var.prefix}storage${random_string.suffix.result}"
      location                        = azurerm_resource_group.rg.location
      resource_group_name             = azurerm_resource_group.rg.name
      account_tier                    = "Standard"
      account_replication_type        = "GRS"
      allow_nested_items_to_be_public = false
    }
    
    // KEY VAULT
    resource "azurerm_key_vault" "default" {
      name                     = "${var.prefix}keyvault${random_string.suffix.result}"
      location                 = azurerm_resource_group.rg.location
      resource_group_name      = azurerm_resource_group.rg.name
      tenant_id                = data.azurerm_client_config.current.tenant_id
      sku_name                 = "standard"
      purge_protection_enabled = false
    }
    
    // AzAPI AIServices
    resource "azapi_resource" "AIServicesResource"{
      type = "Microsoft.CognitiveServices/accounts@2023-10-01-preview"
      name = "AIServicesResource${random_string.suffix.result}"
      location = azurerm_resource_group.rg.location
      parent_id = azurerm_resource_group.rg.id
    
      identity {
        type = "SystemAssigned"
      }
    
      body = jsonencode({
        name = "AIServicesResource${random_string.suffix.result}"
        properties = {
          //restore = true
          customSubDomainName = "${random_string.suffix.result}domain"
            apiProperties = {
                statisticsEnabled = false
            }
        }
        kind = "AIServices"
        sku = {
            name = var.sku
        }
        })
    
      response_export_values = ["*"]
    }
    
    // Azure AI Hub
    resource "azapi_resource" "hub" {
      type = "Microsoft.MachineLearningServices/workspaces@2024-04-01-preview"
      name = "${random_pet.rg_name.id}-aih"
      location = azurerm_resource_group.rg.location
      parent_id = azurerm_resource_group.rg.id
    
      identity {
        type = "SystemAssigned"
      }
    
      body = jsonencode({
        properties = {
          description = "This is my Azure AI hub"
          friendlyName = "My Hub"
          storageAccount = azurerm_storage_account.default.id
          keyVault = azurerm_key_vault.default.id
    
          /* Optional: To enable these field, the corresponding dependent resources need to be uncommented.
          applicationInsight = azurerm_application_insights.default.id
          containerRegistry = azurerm_container_registry.default.id
          */
    
          /*Optional: To enable Customer Managed Keys, the corresponding 
          encryption = {
            status = var.encryption_status
            keyVaultProperties = {
                keyVaultArmId = azurerm_key_vault.default.id
                keyIdentifier = var.cmk_keyvault_key_uri
            }
          }
          */
          
        }
        kind = "hub"
      })
    }
    
    // Azure AI Project
    resource "azapi_resource" "project" {
      type = "Microsoft.MachineLearningServices/workspaces@2024-04-01-preview"
      name = "my-ai-project${random_string.suffix.result}"
      location = azurerm_resource_group.rg.location
      parent_id = azurerm_resource_group.rg.id
    
      identity {
        type = "SystemAssigned"
      }
    
      body = jsonencode({
        properties = {
          description = "This is my Azure AI PROJECT"
          friendlyName = "My Project"
          hubResourceId = azapi_resource.hub.id
        }
        kind = "project"
      })
    }
    
    // AzAPI AI Services Connection
    resource "azapi_resource" "AIServicesConnection" {
      type = "Microsoft.MachineLearningServices/workspaces/connections@2024-04-01-preview"
      name = "Default_AIServices${random_string.suffix.result}"
      parent_id = azapi_resource.hub.id
    
      body = jsonencode({
          properties = {
            category = "AIServices",
            target = jsondecode(azapi_resource.AIServicesResource.output).properties.endpoint,
            authType = "AAD",
            isSharedToAll = true,
            metadata = {
              ApiType = "Azure",
              ResourceId = azapi_resource.AIServicesResource.id
            }
          }
        })
      response_export_values = ["*"]
    }
    
    /* The following resources are OPTIONAL.
    // APPLICATION INSIGHTS
    resource "azurerm_application_insights" "default" {
      name                = "${var.prefix}appinsights${random_string.suffix.result}"
      location            = azurerm_resource_group.rg.location
      resource_group_name = azurerm_resource_group.rg.name
      application_type    = "web"
    }
    
    // CONTAINER REGISTRY
    resource "azurerm_container_registry" "default" {
      name                     = "${var.prefix}contreg${random_string.suffix.result}"
      resource_group_name      = azurerm_resource_group.rg.name
      location                 = azurerm_resource_group.rg.location
      sku                      = "premium"
      admin_enabled            = true
    }
    */
    
  4. 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."
    }
    
    variable "prefix" {
        type = string
        description="This variable is used to name the hub, project, and dependent resources."
        default = "ai"
    }
    
    variable "sku" {
        type        = string
        description = "The sku name of the Azure Analysis Services server to create. Choose from: B1, B2, D1, S0, S1, S2, S3, S4, S8, S9. Some skus are region specific. See https://docs.microsoft.com/en-us/azure/analysis-services/analysis-services-overview#availability-by-region"
        default     = "S0"
    }
    
    resource "random_string" "suffix" {  
      length           = 4  
      special          = false  
      upper            = false  
    } 
    
    /*Optional: For Customer Managed Keys, uncomment this part AND the corresponding section in main.tf
    variable "cmk_keyvault_key_uri" {
        description = "Key vault uri to access the encryption key."
    }
    
    variable "encryption_status" {
        description = "Indicates whether or not the encryption is enabled for the workspace."
        default = "Enabled"
    }
    */
    
  5. outputs.tf라는 파일을 만들고 다음 코드를 삽입합니다.

    output "resource_group_name" {
      value = azurerm_resource_group.rg.id
    }
    
    output "workspace_name" {
        value = azapi_resource.project.id
    }
    
    output "endpoint" {
      value = jsondecode(azapi_resource.AIServicesResource.output).properties.endpoint
    }
    

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를 호출합니다.

결과 확인

  1. Azure 리소스 그룹 이름을 가져옵니다.

    resource_group_name=$(terraform output -raw resource_group_name)
    
  2. 작업 영역 이름을 가져옵니다.

    workspace_name=$(terraform output -raw workspace_name)
    
  3. az ml workspace show를 실행하여 새 작업 영역에 대한 정보를 표시합니다.

    az ml workspace show --resource-group $resource_group_name \
                         --name $workspace_name
    

리소스 정리

Terraform을 통해 리소스를 만들 필요가 더 이상 없으면 다음 단계를 수행합니다.

  1. terraform 플랜을 실행하고 destroy 플래그를 지정합니다.

    terraform plan -destroy -out main.destroy.tfplan
    

    주요 정보:

    • terraform plan 명령은 실행 계획을 만들지만 실행하지는 않습니다. 대신 구성 파일에 지정된 구성을 만드는 데 필요한 작업을 결정합니다. 이 패턴을 사용하면 실제 리소스를 변경하기 전에 실행 계획이 예상과 일치하는지 확인할 수 있습니다.
    • 선택 사항인 -out 매개 변수를 사용하여 계획의 출력 파일을 지정할 수 있습니다. -out 매개 변수를 사용하면 검토한 계획이 정확하게 적용됩니다.
  2. terraform apply를 실행하여 실행 계획을 적용합니다.

    terraform apply main.destroy.tfplan
    

Azure의 Terraform 문제 해결

Azure에서 Terraform을 사용할 때 발생하는 일반적인 문제를 해결합니다.

다음 단계