azureterraformazure-machine-learning-serviceazure-aiazapi

How to get Environment (codeid, environmentid) details of Microsoft.MachineLearningServices deployment model?


I am trying to deploy a online endpoint and deployment in Azure AI foundry of Microsoft.MachineLearningServices. I am doing this using AZAPI as below. How to get Environment (codeid, environmentid) details of Microsoft.MachineLearningServices deployment model ?

resource "azapi_resource" "online_endpoint" {
  type      = "Microsoft.MachineLearningServices/workspaces/onlineEndpoints@2025-01-01-preview"
  parent_id = azapi_resource.project.id
  name      = "my-first-endpoint"
  location  = var.location

  body = {
    identity = {
      type = "SystemAssigned"
    }
    properties = {
      authMode = "AADToken"
    }
  }

  schema_validation_enabled = false
  response_export_values    = ["*"]
}

resource "azapi_resource" "online_endpoint_deployment" {
  type      = "Microsoft.MachineLearningServices/workspaces/onlineEndpoints/deployments@2025-01-01-preview"
  parent_id = azapi_resource.online_endpoint.id
  name      = "blue"
  location  = local.location

  body = {
        sku = {
      capacity = 1
      name     = "Standard_DS3_v2"
    }
    properties = {
      endpointComputeType = "Managed"
      scaleSettings = {
        scaleType = "Default"
      }
      model = "azureml://registries/azureml-cohere/models/cohere-command-a/versions/1"
      codeConfiguration = {
        codeId         = **How to get this value ?**
        scoringScript  = "score.py"
      }
      environmentId = **How to get this value ?**
    }
  }

  schema_validation_enabled = false
  response_export_values    = ["*"]
} 

Solution

  • Getting Environment (codeid, environmentid) details of Microsoft.MachineLearningServices deployment model

    The missing keys info mentioned CodeID and EnvironmentID mentioned in the configuration has specific importance.

    As per the Microsoft Doc which was mentioned below

    CodeId: It is the ARM ID of the code asset that contains the scoring script and any associated files needed for model inference.

    Structure:

    /subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.MachineLearningServices/workspaces/{workspaceName}/codes/{codeName}/versions/{version}
    

    use the command

    az resource list --resource-type Microsoft.MachineLearningServices/workspaces/codes --resource-group <resourceGroup> --namespace Microsoft.MachineLearningServices --query "[?contains(id, '/workspaces/<workspaceName>/')].{Name:name, Id:id}" --output table
    

    Refer:

    https://learn.microsoft.com/en-us/cli/azure/resource?view=azure-cli-latest#az-resource-list

    Environmentid: It's basically ARM resource ID or AssetId of the environment specification for the endpoint deployment. Which mentioned the runtime context for your deployment, including dependencies like Python packages or Docker images.

    Structure:

    /subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.MachineLearningServices/workspaces/{workspaceName}/environments/{environmentName}/versions/{version}
    

    We can fetch this info using the command

    az ml environment list --resource-group my-resource-group --workspace-name my-workspace
    

    Refer: https://learn.microsoft.com/en-us/cli/azure/ml/environment?view=azure-cli-latest#az-ml-environment-list

    Once the two ID as mentioned fetched and noted, define them in locals and used in the configuration you mentioned

    Deployment:

    locals {
      workspace_id   = "/subscriptions/${var.subscription_id}/resourceGroups/${var.resource_group}/providers/Microsoft.MachineLearningServices/workspaces/${var.workspace_name}"
      code_id        = "${local.workspace_id}/codes/my-code/versions/1"
      environment_id = "${local.workspace_id}/environments/my-env/versions/1"
    }
    
    resource "azapi_resource" "online_endpoint_deployment" {
      type      = "Microsoft.MachineLearningServices/workspaces/onlineEndpoints/deployments@2025-01-01-preview"
      parent_id = azapi_resource.online_endpoint.id
      name      = var.deployment_name
      location  = var.location
    
      body = {
        sku = {
          name     = var.sku_name
          capacity = var.sku_capacity
        }
        properties = {
          endpointComputeType = "Managed"
          scaleSettings = {
            scaleType = "Default"
          }
          model = var.model_id
          codeConfiguration = {
            codeId        = local.code_id
            scoringScript = var.scoring_script
          }
          environmentId = local.environment_id
        }
      }
    
      schema_validation_enabled = false
      response_export_values    = ["*"]
    }
    

    Refer: Microsoft.MachineLearningServices/workspaces/onlineEndpoints/deployments - Bicep, ARM template & Terraform AzAPI reference | Microsoft Learn

    https://learn.microsoft.com/en-us/azure/machine-learning/how-to-configure-cli?view=azureml-api-2&tabs=private