SQL Azure Connection Using managed identity in Azure Function (python)

madhavan thasu ramachandran 21 Reputation points
2021-01-27T05:04:50.683+00:00

Hello All,

I have an Azure function Python 3.6, Consumption Plan and SQL Azure. Enabled system identity for the function.. Created User for this function in SQL Azure.

Now need help on implementing the connectivity using managed identity.

Based on the link here -> https://learn.microsoft.com/en-us/azure/app-service/overview-managed-identity?tabs=python

I am not getting how to set the MSI_Endpoint and MSI_Header for my function.

Any help appreciated.

Regards
Madhavan.TR

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,257 questions
{count} votes

Accepted answer
  1. JayaC-MSFT 5,526 Reputation points
    2021-01-29T10:50:11.317+00:00

    Hello @madhavan thasu ramachandran , I see the document you are referring mentions this : https://learn.microsoft.com/en-us/azure/app-service/overview-managed-identity?tabs=dotnet#using-the-rest-protocol
    Could you please confirm if this is the case?

    ---------------------------------------------

    UPDATE:

    Code to start with :

    identity_endpoint = os.environ["IDENTITY_ENDPOINT"]  
    identity_header = os.environ["IDENTITY_HEADER"]  
    resource_uri="https://database.windows.net/"  
    token_auth_uri = f"{identity_endpoint}?resource={resource_uri}&api-version=2019-08-01"  
    head_msi = {'X-IDENTITY-HEADER':identity_header}  
    resp = requests.get(token_auth_uri, headers=head_msi)  
    access_token = resp.json()['access_token']  
    accessToken = bytes(access_token, 'utf-8');  
    

    Please refer to : https://github.com/AzureAD/azure-activedirectory-library-for-python/wiki/Connect-to-Azure-SQL-Database

    https://learn.microsoft.com/en-us/azure/app-service/overview-managed-identity?tabs=python#code-examples


0 additional answers

Sort by: Most helpful