How do I add managed identity to azure batch pool via python?

Brian Bertrand 21 Reputation points
2021-10-27T21:12:48.227+00:00

I'm currently creating batch pools using azure.batch.models.PoolAddParameter()

I'm wondering how to assign a managed identity to the pool using this class. I found azure.batch.models.BatchPoolIdentity(), but cannot figure out how to assign it to the pool.

Any help would be greatly appreciated.

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

Accepted answer
  1. prmanhas-MSFT 17,901 Reputation points Microsoft Employee
    2021-11-05T07:53:55.99+00:00

    @Brian Bertrand Apologies for the delay in response and all the inconvenience caused because of the issue.

    To assign managed identities to pools you need to use the management client instead of the normal batch service client, this is so that requests are routed through ARM which supported managed identity. Here is some untested sample code in Python but should work with minimal tweaks:

    from azure.identity import DefaultAzureCredential
    import azure.mgmt.batch
    import azure.mgmt.batch.models

    client = azure.mgmt.batch.BatchManagement(credential=DefaultAzureCredential(), subscription_id="test")

    sample_identity = azure.mgmt.batch.models.BatchPoolIdentity(
    type=azure.mgmt.batch.models.PoolIdentityType.USER_ASSIGNED,
    user_assigned_identities={
    "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/identientityName": None
    }
    )
    params = azure.mgmt.batch.models.Pool(identity=sample_identity)

    client.pool.create("resource", "account", "poolName", params)

    Hope it helps!!!

    Please "Accept as Answer" if it helped so it can help others in community looking for help on similar topics.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Neharika Singh 16 Reputation points
    2022-09-16T20:10:57.433+00:00

    @Brian Bertrand @Neharika Singh
    I have used above code to add managed identity however I am unable to create pool..It gives me Authorization error 403. I have added contributor, owner and reader role to managed identity since facing error while creating pool. I cannot create pool from port as well when I select managed Identity as option it doesnt list the managed identity
    sharing screenshot of same.241977-batch-permission1.png

    241967-batch-permission2.png

    0 comments No comments