Lesson 2 of 3 Infrastructure as Code for Azure Machine Learning

Deploying Workspaces with Bicep and the Azure CLI

This lesson shows how a workspace and its dependent resources are deployed with Bicep, how role assignments are made idempotent, and how workspace contents are added with the Azure CLI.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe secure settings for a workspace's dependent resources.
  • Identify workspace properties that cannot change after creation.
  • Explain why role assignment names use guid().
  • Sequence Bicep and az ml steps in a deployment.

Dependent resources in Bicep

A production-quality template creates the dependencies with secure settings rather than accepting defaults: a storage account with public blob access disabled and TLS 1.2, a key vault that uses Azure RBAC, a workspace-based Application Insights resource with a Log Analytics workspace, and a container registry with the admin user disabled.

resource st 'Microsoft.Storage/storageAccounts@2023-05-01' = {
  name: 'st${uniqueString(resourceGroup().id)}'
  location: location
  sku: { name: 'Standard_LRS' }
  kind: 'StorageV2'
  properties: {
    allowBlobPublicAccess: false
    minimumTlsVersion: 'TLS1_2'
  }
}

resource law 'Microsoft.OperationalInsights/workspaces@2023-09-01' = {
  name: 'log-${prefix}'
  location: location
}

resource appi 'Microsoft.Insights/components@2020-02-02' = {
  name: 'appi-${prefix}'
  location: location
  kind: 'web'
  properties: { Application_Type: 'web', WorkspaceResourceId: law.id }
}

resource acr 'Microsoft.ContainerRegistry/registries@2023-07-01' = {
  name: 'acr${uniqueString(resourceGroup().id)}'
  location: location
  sku: { name: 'Premium' }
  properties: { adminUserEnabled: false }
}

The container registry uses the Premium tier here because private endpoints, needed in the next topic, require it. A development environment that does not use private networking could use Basic or Standard.

The workspace resource

The workspace resource references its dependencies by resource ID and sets its identity, network and authentication options. The storage account and key vault cannot be changed after the workspace is created, which is one more reason to define them in the template from the start.

resource mlw 'Microsoft.MachineLearningServices/workspaces@2025-06-01' = {
  name: 'mlw-${prefix}'
  location: location
  identity: { type: 'SystemAssigned' }
  sku: { name: 'Basic', tier: 'Basic' }
  properties: {
    friendlyName: 'Churn ${env}'
    storageAccount: st.id
    keyVault: kv.id
    applicationInsights: appi.id
    containerRegistry: acr.id
    publicNetworkAccess: 'Disabled'
    managedNetwork: { isolationMode: 'AllowOnlyApprovedOutbound' }
  }
}

output workspaceName string = mlw.name

Role assignments in Bicep

Access is part of the infrastructure. A role assignment resource takes a deterministic name, usually a GUID derived from the scope, principal and role, so that redeploying does not create duplicates. Built-in roles are referenced by their role definition IDs.

param dataScientistsGroupId string
var dataScientistRole = 'f6c7c914-8db3-469d-8ca1-694a8f32e121' // AzureML Data Scientist

resource dsAccess 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
  name: guid(mlw.id, dataScientistsGroupId, dataScientistRole)
  scope: mlw
  properties: {
    principalId: dataScientistsGroupId
    principalType: 'Group'
    roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', dataScientistRole)
  }
}

The identity that deploys the template needs permission to create role assignments, such as Owner, User Access Administrator or Role Based Access Control Administrator on the scope. Pipelines often get that right only on the resource groups they manage.

Workspace contents with the Azure CLI

Once the workspace exists, the CLI creates its contents from YAML files kept in the same repository. A deployment script runs the commands in order, reading names from the Bicep outputs.

WS=$(az deployment group show -g rg-ml-dev -n main --query properties.outputs.workspaceName.value -o tsv)
az ml compute create --file compute/cpu-cluster.yml -g rg-ml-dev -w $WS
az ml datastore create --file datastores/sales_lake.yml -g rg-ml-dev -w $WS
az ml environment create --file environments/train-env.yml -g rg-ml-dev -w $WS

create commands for compute and datastores update the asset if it already exists with compatible settings, so the script can be rerun, while environments and components need new versions when their content changes. Keeping the YAML in Git means the workspace configuration is reviewed and versioned like the Bicep files.

Worked example

A template that fails on the second environment

A Bicep template deploys a workspace and assigns the data scientists group AzureML Data Scientist. It works in development. In test, the deployment fails with an error that a role assignment already exists with a different name. The template names the role assignment with guid(resourceGroup().id).

The name is derived only from the resource group, so it does not change when the principal or role changes, and a previous manual assignment of the same role to the same principal at that scope conflicts with it. Derive the name from the scope, principal and role, as in guid(mlw.id, dataScientistsGroupId, dataScientistRole), and remove the manual assignment so that the template owns it. Redeployments are then idempotent in every environment.

Generating a random GUID with newGuid() would create a new name on each deployment and fail or duplicate assignments. Deleting and recreating the workspace would not change the naming problem. Removing the role assignment from the template would leave access unmanaged.

Check yourself

Why are role assignment names in Bicep generated with guid(scope, principalId, roleDefinitionId)?

Show answer

Role assignment names must be GUIDs and unique per scope. A deterministic GUID gives the same name on every deployment, so redeploying updates the existing assignment instead of failing on a duplicate.

Exam Tips

Remember for the exam

  • Create dependencies with secure settings: no public blob access, RBAC key vault, no ACR admin user.
  • A workspace's storage account and key vault cannot be changed after creation.
  • Name role assignments with guid(scope, principal, role) so redeployments are idempotent.
  • Run az ml commands for compute, datastores and environments after the Bicep deployment.