Use Terraform to create an Azure AI Studio hub

In this article, you use Terraform to create an Azure AI Studio hub, a project, and AI services connection. A hub is a central place for data scientists and developers to collaborate on machine learning projects. It provides a shared, collaborative space to build, train, and deploy machine learning models. The hub is integrated with Azure Machine Learning and other Azure services, making it a comprehensive solution for machine learning tasks. The hub also allows you to manage and monitor your AI deployments, ensuring they're performing as expected.

Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure.

  • Create a resource group
  • Set up a storage account
  • Establish a key vault
  • Configure AI services
  • Build an AI Studio hub
  • Develop an AI Studio project
  • Establish an AI services connection

Prerequisites

Implement the Terraform code

Note

The sample code for this article is located in the Azure Terraform GitHub repo. You can view the log file containing the test results from current and previous versions of Terraform.

See more articles and sample code showing how to use Terraform to manage Azure resources

  1. Create a directory in which to test and run the sample Terraform code and make it the current directory.

  2. Create a file named providers.tf and insert the following code.

    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. Create a file named main.tf and insert the following code.

    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. Create a file named variables.tf and insert the following code.

    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. Create a file named outputs.tf and insert the following code.

    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
    }
    

Initialize Terraform

Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.

terraform init -upgrade

Key points:

  • The -upgrade parameter upgrades the necessary provider plugins to the newest version that complies with the configuration's version constraints.

Create a Terraform execution plan

Run terraform plan to create an execution plan.

terraform plan -out main.tfplan

Key points:

  • The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
  • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.

Apply a Terraform execution plan

Run terraform apply to apply the execution plan to your cloud infrastructure.

terraform apply main.tfplan

Key points:

  • The example terraform apply command assumes you previously ran terraform plan -out main.tfplan.
  • If you specified a different filename for the -out parameter, use that same filename in the call to terraform apply.
  • If you didn't use the -out parameter, call terraform apply without any parameters.

Verify the results

  1. Get the Azure resource group name.

    resource_group_name=$(terraform output -raw resource_group_name)
    
  2. Get the workspace name.

    workspace_name=$(terraform output -raw workspace_name)
    
  3. Run az ml workspace show to display information about the new workspace.

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

Clean up resources

When you no longer need the resources created via Terraform, do the following steps:

  1. Run terraform plan and specify the destroy flag.

    terraform plan -destroy -out main.destroy.tfplan
    

    Key points:

    • The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
    • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.
  2. Run terraform apply to apply the execution plan.

    terraform apply main.destroy.tfplan
    

Troubleshoot Terraform on Azure

Troubleshoot common problems when using Terraform on Azure.

Next steps