Search code examples
azure-sdk-python

No environment configuration found. DefaultAzureCredential()


I am trying to use this python sample to authenticate a client with an Azure Service

# pip install azure-identity
from azure.identity import DefaultAzureCredential
# pip install azure-mgmt-compute
from azure.mgmt.compute import ComputeManagementClient
# pip install azure-mgmt-network
from azure.mgmt.network import NetworkManagementClient
# pip install azure-mgmt-resource
from azure.mgmt.resource import ResourceManagementClient

    SUBSCRIPTION_ID = creds_obj['SUBSCRIPTION_ID']

    # Create client
    # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
    resource_client = ResourceManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )
    network_client = NetworkManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )
    compute_client = ComputeManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )

I keep getting No environment configuration found. The code sample is directly from the microsoft github: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/resources/azure-mgmt-resource/azure/mgmt/resource/resources/_resource_management_client.py. Ideally I would like to manage this configuration using environment variables or a config file. Is there any way to do this?


Solution

  • When using Azure Identity client library for Python, DefaultAzureCredential attempts to authenticate via the following mechanisms in this order, stopping when one succeeds:

    enter image description here

    You could set Environment Variables to fix it.

    enter image description here

    from azure.identity import DefaultAzureCredential
    
    credential=DefaultAzureCredential()
    

    Or set the properties in config and use ClientSecretCredential to create credential.

    from azure.identity import ClientSecretCredential
    
    subscription_id = creds_obj["AZURE_SUBSCRIPTION_ID"]
    tenant_id = creds_obj["AZURE_TENANT_ID"]
    client_id = creds_obj["AZURE_CLIENT_ID"]
    client_secret = creds_obj["AZURE_CLIENT_SECRET"]
    
    credential = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)