How to authenticate Azure Batch from Python Function App using Managed Identity?

Abh1234123 20 Reputation points
2025-04-01T09:12:26.2266667+00:00

I'm trying to authenticate with Azure Batch from a Python Azure Function App using Managed Identity via DefaultAzureCredential.

What I’ve Tried:

I'm using the official azure-batch SDK and passed DefaultAzureCredential() to BatchServiceClient, like so:

python
CopyEdit
from azure.batch import BatchServiceClient
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()
batch_client = BatchServiceClient(credential, batch_url="https://<my-batch-account>.<region>.batch.azure.com")


However, I get this error.


AttributeError: 'DefaultAzureCredential' object has no attribute 'signed_session'

📍 Stack trace:

File ".../azure_functions_worker/dispatcher.py", line ...
  ...
File ".../msrest/service_client.py", line 336, in send
  pipeline_response = self.config.pipeline.run(request, **kwargs)
AttributeError: 'DefaultAzureCredential' object has no attribute 'signed_session'

Alternative approach I tried:

credential = DefaultAzureCredential()
# legacy token
token = credential.get_token("https://batch.core.windows.net/.default")

token_auth = BasicTokenAuthentication({"access_token": token.token})
client = BatchServiceClient(
    credentials=token_auth,
    batch_url="https://<your-batch-account>.<region>.batch.azure.com"
)

But this doesn't work too.

Azure Batch
Azure Batch
An Azure service that provides cloud-scale job scheduling and compute management.
368 questions
{count} votes

Accepted answer
  1. Arko 2,130 Reputation points Microsoft External Staff
    2025-04-08T17:52:22+00:00

    If you're trying to call Azure Batch APIs from a Python-based Azure Function App using Managed Identity, you’ll need a Storage Account, an Azure Batch Account, Linux Consumption/EPM Function App (Python 3.10+) and a System-assigned Managed Identity

    Example-

    
    # Set storage name and create storage account
    
    $storageName = "arkstorage$((Get-Random -Maximum 99999))"
    
    az storage account create `
    
      --name $storageName `
    
      --resource-group arkorg `
    
      --location centralindia `
    
      --sku Standard_LRS
    
    # Create Azure Batch account linked to the storage account
    
    az batch account create `
    
      --name arkbatchaccount `
    
      --resource-group arkorg `
    
      --location centralindia `
    
      --storage-account $storageName
    
    # Create Premium Elastic App Plan (for Linux Functions)
    
    az functionapp plan create `
    
      --resource-group arkorg `
    
      --name arkfuncplan `
    
      --location centralindia `
    
      --number-of-workers 1 `
    
      --sku EP1 `
    
      --is-linux
    
    # Create Function App with System-assigned Managed Identity
    
    az functionapp create `
    
      --resource-group arkorg `
    
      --plan arkfuncplan `
    
      --name arkbatchfuncapp `
    
      --runtime python `
    
      --runtime-version 3.10 `
    
      --functions-version 4 `
    
      --os-type Linux `
    
      --storage-account $storageName `
    
      --assign-identity
    
    

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    Assign Permissions to the Function App

    
    # Get the principal ID of the function's managed identity
    
    $principalId = az functionapp identity show `
    
      --resource-group arkorg `
    
      --name arkbatchfuncapp `
    
      --query principalId `
    
      -o tsv
    
    # Get the Batch account scope
    
    $batchScope = az batch account show `
    
      --name arkbatchaccount `
    
      --resource-group arkorg `
    
      --query id `
    
      -o tsv
    
    # Assign "Contributor" role (minimum needed for most Batch operations)
    
    az role assignment create `
    
      --assignee $principalId `
    
      --role "Contributor" `
    
      --scope $batchScope
    
    

    Create Function App Project with Batch SDK

    
    func init arkbatchfunc --worker-runtime python
    
    cd arkbatchfunc
    
    func new --template "HTTP trigger" --name BatchTrigger
    
    

    Update requirements.txt

    
    azure-functions
    
    azure-identity
    
    azure-batch
    
    msrest
    
    

    Install pip install -r requirements.txt

    Write Python Function Code to Authenticate via Managed Identity

    Inside BatchTrigger/function_app.py (or init.py)

    
    import logging
    
    import azure.functions as func
    
    from azure.identity import DefaultAzureCredential
    
    from azure.batch import BatchServiceClient
    
    from azure.batch.models import BatchErrorException
    
    def main(req: func.HttpRequest) -> func.HttpResponse:
    
        try:
    
            credential = DefaultAzureCredential()
    
            batch_url = "https://arkbatchaccount.centralindia.batch.azure.com"
    
            batch_client = BatchServiceClient(
    
                credential=credential,
    
                batch_url=batch_url
    
            )
    
            # Example: List pools
    
            pool_list = list(batch_client.pool.list())
    
            pool_ids = [p.id for p in pool_list]
    
            return func.HttpResponse(f"Connected! Pools: {pool_ids}", status_code=200)
    
        except BatchErrorException as be:
    
            logging.error(f"Batch error: {be}")
    
            return func.HttpResponse("Batch error occurred", status_code=500)
    
        except Exception as e:
    
            logging.error(f"General error: {e}")
    
            return func.HttpResponse("Unhandled error occurred", status_code=500)
    
    

    Deploy to Azure and Get Function URL

    
    func azure functionapp publish arkbatchfuncapp
    
    az functionapp function show `
    
      --resource-group arkorg `
    
      --name arkbatchfuncapp `
    
      --function-name BatchTrigger `
    
      --query "invokeUrlTemplate" `
    
      -o tsv
    
    

    Get function key

    
    az functionapp function keys list `
    
      --resource-group arkorg `
    
      --name arkbatchfuncapp `
    
      --function-name BatchTrigger `
    
      --query "default" `
    
      -o tsv
    
    

    enter image description here

    Test it via your url in my case-

    
    https://arkbatchfuncapp.azurewebsites.net/api/batch?code=<your-function-key>
    
    

    This setup uses Azure Managed Identity and Azure Identity SDK (no hardcoded credentials) to securely authenticate your Function App with Azure Batch

    enter image description here


0 additional answers

Sort by: Most helpful

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.