What's the recommended way to use a user-defined Managed Identity ?

FLOER, Clément 0 Reputation points
2025-11-18T20:33:31.2033333+00:00

Hi,

I'm currently using the AzureML Python SDK v1 (Switching to v2 is not an option that I consider for the moment).

I'm trying to give read and write access to an AzureML job so it can access an Azure Table storage. The compute cluster is attached to a single user-assigned identity that has all the rights it needs. There is no system-assigned identity attached to this cluster.

I am actually able to get my code working using the ManagedIdentity credential by passing its client ID as an environment variable when I submit the job. Then I can instantiate the ManagedIdentityCredential within the Azure VM, like the following:


  client_id = os.getenv("CUSTOM_MANAGED_IDENTITY_CLIENT_ID")
  credential = ManagedIdentityCredential(client_id=client_id)
  table_service_client = TableServiceClient(
            endpoint=f"https://{storage_name}.table.core.windows.net",
            credential=credential,
   )

I'd like to find a better approach that uses the ManagedIdentity and that doesn't rely on environment variables, as I find them prone to errors.

Is there a way to dynamically retrieve the client_ids of the managed identities attached to the cluster within the running job? What is the recommended approach to use the ManagedIdentity within an Azure ML job?

Thanks for your help,

Azure Machine Learning
{count} votes

1 answer

Sort by: Most helpful
  1. Aryan Parashar 3,380 Reputation points Microsoft External Staff Moderator
    2025-11-19T06:45:38.1733333+00:00

    Hi FLOER, Clément,

    Yes, you can absolutely use a userAssigned ManagedIdentity without relying on environment variables. I know this can be a bit confusing at first, so I hope the example below helps make things clearer. Here’s the code to retrieve all the user-assigned identities attached to a resource group:

    from azure.identity import DefaultAzureCredential
    from azure.mgmt.msi import ManagedServiceIdentityClient
    
    subscription_id = "<subscription_id>"  # Replace with your subscription ID
    credential = DefaultAzureCredential()
    client = ManagedServiceIdentityClient(credential,subscription_id )
    resource_group = "<resource_group_name>"  # Replace with your resource group name
    for identity in client.user_assigned_identities.list_by_resource_group(resource_group):
        print(identity.id)
    

    These are the dependencies: → pip install azure-mgmt-msi azure-identity

    From here, you can integrate the identities into your code by filtering them based on whatever identifier fits your requirements.

    Feel free to accept this as an answer if it helps.
    Thank you for reaching out to The Microsoft Q&A Portal.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.