Azure job requires interactive authentication every time

SydneyD 1 Reputation point
2022-03-16T19:11:10.313+00:00

I'm running a neural network on azure and it's working except it requires me to sign in with a device code for every job that runs. This is an issue for when I want to run long studies with multiple jobs and can't authenticate every job.

I've tried to use the azure CLI authentication with the following code:

cli_auth = AzureCliAuthentication()
ws = Workspace(subscription_id="id",
               resource_group="rsgp",
               workspace_name="ws", auth=cli_auth)

When I do that, I recieve the error: "Could not retrieve user token. Please run 'az login'"
This happens when I login using az login before a run as well.

I've also tried using the default credential before calling the workspace instead of using CLI, using this code:

credential = DefaultAzureCredential()


client = ResourceManagementClient(
    credential=credential,
    subscription_id="id"
)

This is functional, but I still need to log in manually for every job.

Any help would be appreciated! Thanks!

Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
3,336 questions
{count} votes

1 answer

Sort by: Most helpful
  1. romungi-MSFT 48,906 Reputation points Microsoft Employee Moderator
    2022-03-17T07:29:42.533+00:00

    @SydneyD I think you should be using service principal authentication for automated processes. Detailed steps to create a service principal are documented in this notebook along with other available options.

    To summarize, you set a service principal with the required access role and then use the same while defining your workspace.

    import os  
    from azureml.core.authentication import ServicePrincipalAuthentication  
      
    svc_pr_password = os.environ.get("AZUREML_PASSWORD")  
      
    svc_pr = ServicePrincipalAuthentication(  
        tenant_id="my-tenant-id",  
        service_principal_id="my-application-id",  
        service_principal_password=svc_pr_password)  
      
      
    ws = Workspace(  
        subscription_id="my-subscription-id",  
        resource_group="my-ml-rg",  
        workspace_name="my-ml-workspace",  
        auth=svc_pr  
        )  
      
    print("Found workspace {} at location {}".format(ws.name, ws.location))  
    

    Service principal password can be set as environment variable if it is for local testing or can be retried using Azure keyvault or a secret variable in Azure DevOps.
    Hope this helps!!

    If an answer is helpful, please click on 130616-image.png or upvote 130671-image.png which might help other community members reading this thread.

    0 comments No comments

Your answer

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