pythonazure-machine-learning-serviceazure-sdk-python

How to assign a Azure ML compute instance to a user using python SDK


How to I assign a compute instance to a user using python SDK?

Right now I'm connecting to my workspace via serviceprincipal authentication using the following code sniped with the python sdk

from azureml.core import Workspace
from azureml.core.authentication import ServicePrincipalAuthentication

svc_pr = ServicePrincipalAuthentication(
    tenant_id="tenant_id_from_my_account",
    service_principal_id="service_principal_id_of_my_app",
    service_principal_password='password_of_my_service_principal')


ws = Workspace(
    subscription_id="my_subscription_id",
    resource_group="my_resource_group",
    workspace_name="my_workspacename",
    auth=svc_pr)

To create a compute instance I'm using this code snipet

from azureml.core.compute import ComputeTarget, ComputeInstance
from azureml.core.compute_target import ComputeTargetException

compute_name = "light-medium-gabriel-2nd"

# Verify that instance does not exist already
try:
    instance = ComputeInstance(workspace=ws, name=compute_name)
    print('Found existing instance, use it.')
except ComputeTargetException:
    compute_config = ComputeInstance.provisioning_configuration(
        vm_size='STANDARD_E4S_V3',
        ssh_public_access=False,
        tags = {'projeto' : 'Data Science','Ambiente':'Homologação'},
    )
    instance = ComputeInstance.create(ws, compute_name, compute_config)
    instance.wait_for_completion(show_output=True)

But I can't access the compute instance. Since I'm using the service principal autentication it's like I'm creating the compute instance assigned to the service principal and not to my user?


Solution

  • I tried in my environment and below results:

    You can be able to create compute instance in AzureML workspace with user by using Defaultazurecredential method.

    You can follow this MS-DOCS to create compute instance.

    Code:

    from azure.identity import DefaultAzureCredential
    from azure.ai.ml.entities import ComputeInstance
    import datetime
    from azure.ai.ml import MLClient
    
    subscription_id = "<sub id>"
    resource_group = "<resource_grp>"
    workspace_name = "wrkspace_name"
    credential=DefaultAzureCredential()
    ml_client = MLClient(subscription_id,resource_group,workspace_name,credential)
    ci_basic_name = "v-vsettu1" + datetime.datetime.now().strftime("%Y%m%d%H%M")
    ci_basic = ComputeInstance(name=ci_basic_name,size="STANDARD_DS3_v2")
    ml_client.begin_create_or_update(ci_basic).result()
    

    Console:

    enter image description here

    Also, you can use Cli commands(v2) to create Compute instance.

    Command:

    az ml compute create -f instance1.yml
    

    yaml

    $schema: https://azuremlschemas.azureedge.net/latest/computeInstance.schema.json 
    name: v-vsettu1
    type: computeinstance
    size: STANDARD_DS3_v2
    

    Portal:

    enter image description here