KeyError: 'IDENTITY_ENDPOINT' error in Azure environment

Viewed 452

I had deployed my application in the Azure Linux environment.

I followed the official python example: https://docs.microsoft.com/en-us/azure/app-service/overview-managed-identity?tabs=python

import os
import requests

identity_endpoint = os.environ["IDENTITY_ENDPOINT"]
identity_header = os.environ["IDENTITY_HEADER"]

def get_bearer_token(resource_uri):
    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']

    return access_token

Why I am getting KeyError: 'IDENTITY_ENDPOINT' error? Any configuration I need to do extra?

1 Answers

It looks like you might not have completed a step from the link you provided. Those environment variables get added when you add a Managed Identity to the app in Azure.

The link provides a number of ways to add the identity but for simplicity now, just work with a system assigned identity.

So, for the App Service you made in the Azure Portal, under Identity, check you have that set to On:

Adding a system identity

To confirm that this has created the 2 environment variables we expect, again, in the Portal for your App Service, go to an SSH session (this is in place of the old Console option).

SSH

In the shell, type printenv to see all the environment variables set. Look for the 2 that your app is currently failing to find:

Identity env vars

If they're missing let me know and at least we'll be better placed to dig further.

Related