To create consistent resource tagging in Azure ML workspace

66 Views Asked by At

I am currently trying to create consistent components tagging in Azure ML workspace. Instead of using the component name created by default, which can be hard to understand at times, I intend to use kebab-case for naming the components.

(Link: https://www.freecodecamp.org/news/snake-case-vs-camel-case-vs-pascal-case-vs-kebab-case-whats-the-difference/)

I can create consistent naming through the UI (i.e. by clicking through multiple buttons each time). I would like to automate the tagging process using bash script, and I have written a simple bash script for this purpose.

#! /usr/bin/sh

# Define and label the resources
RESOURCE_GROUP="rg-test"
REGION="westeurope"
WORKSPACE_NAME="mlw-test"

# Create the resource group
echo "Create the resource group"
az group create --name $RESOURCE_GROUP --location $REGION

# Create Azure ML workspace
# az ml workspace create --name $WORKSPACE_NAME

While this bash script would work to create a functional workspace, but the name of other components created along with this workspace such as container-registry, application-insights are randomly assigned. Therefore, I studied the documentation a bit more to check how to configure the labels for these additional components, and I came across this docs to define the components naming.

Link: https://learn.microsoft.com/de-de/cli/azure/ml/workspace?view=azure-cli-latest

I modified the initial bash script to extend the naming to other components, and found out that creating a new name for each listed component does not seem to be possible, unless these components have been already created.

az ml workspace create --resourceGroupName $RESOURCE_GROUP --workspaceName $WORKSPACE_NAME --location $REGION --application-insights appi-test --container-registry crtest --key-vault kv-test --storage-account sttest

I also learned from the YAML syntax that creating a new name doesn't seem to be possible, unless the resources exist already:

The fully qualified resource ID of an existing Azure container registry to use as the default container registry for the workspace (https://learn.microsoft.com/en-us/azure/machine-learning/reference-yaml-workspace?view=azureml-api-2)

So, my question is that how can we create consistent naming for new components from the terminal without using Azure default random naming?

Thank you!

1

There are 1 best solutions below

1
On BEST ANSWER

You can create consistent naming for new components by first creating the required resources (Application Insights, Container Registry, Key Vault, and Storage Account) with your desired names, and then referencing them when creating the Azure ML workspace.

Here's a modified version of your bash script that creates each resource with a consistent naming pattern and then creates the Azure ML workspace:

#!/usr/bin/sh

# Define and label the resources (Make sure to give correct and unique names to avoid any error)
RESOURCE_GROUP="rg-test"
REGION="westeurope"
WORKSPACE_NAME="mlw-test"
APP_INSIGHTS_NAME="appi-test"
CONTAINER_REGISTRY_NAME="crtest"
KEY_VAULT_NAME="kv-test"
STORAGE_ACCOUNT_NAME="sttest"

# Create the resource group
echo "Create the resource group"
az group create --name $RESOURCE_GROUP --location $REGION

# Create Application Insights
echo "Create Application Insights"
az resource create --resource-group $RESOURCE_GROUP --resource-type "Microsoft.Insights/components" --name $APP_INSIGHTS_NAME --location $REGION --properties '{"Application_Type":"web"}'

# Create Container Registry
echo "Create Container Registry"
az acr create --resource-group $RESOURCE_GROUP --name $CONTAINER_REGISTRY_NAME --sku Basic --location $REGION

# Create Key Vault
echo "Create Key Vault"
az keyvault create --resource-group $RESOURCE_GROUP --name $KEY_VAULT_NAME --location $REGION

# Create Storage Account
echo "Create Storage Account"
az storage account create --resource-group $RESOURCE_GROUP --name $STORAGE_ACCOUNT_NAME --location $REGION --sku Standard_LRS --kind StorageV2 --access-tier Hot

# Get resource IDs
APP_INSIGHTS_ID=$(az resource show --ids $(az resource list --resource-group $RESOURCE_GROUP --resource-type Microsoft.Insights/components --query "[?name=='$APP_INSIGHTS_NAME'].id" --output tsv) --query 'id' -o tsv)
CONTAINER_REGISTRY_ID=$(az acr show --name $CONTAINER_REGISTRY_NAME --query 'id' -o tsv)
KEY_VAULT_ID=$(az keyvault show --name $KEY_VAULT_NAME --query 'id' -o tsv)
STORAGE_ACCOUNT_ID=$(az storage account show --name $STORAGE_ACCOUNT_NAME --query 'id' -o tsv)

# Create Azure ML workspace
echo "Create Azure ML workspace"
az ml workspace create --resource-group $RESOURCE_GROUP --name $WORKSPACE_NAME --location $REGION --application-insights $APP_INSIGHTS_ID --container-registry $CONTAINER_REGISTRY_ID --key-vault $KEY_VAULT_ID --storage-account $STORAGE_ACCOUNT_ID

With the above command, I was able to create resources with consistent names. enter image description here