CodexBloom - Programming Q&A Platform

How to Configure Azure Key Vault with Managed Identity for Python Azure Functions?

πŸ‘€ Views: 48 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-09
azure azure-functions key-vault Python

This might be a silly question, but I'm trying to access secrets stored in Azure Key Vault from my Azure Function written in Python, but I'm running into issues with the managed identity setup... I have enabled the system-assigned managed identity on my Azure Function, and I've granted it `get` access to the secrets in the Key Vault. However, I'm getting the following behavior when I try to retrieve a secret: ``` azure.core.exceptions.HttpResponseError: (403) Forbidden: You do not have permission to perform this operation. ``` Here’s a snippet of my code: ```python import os from azure.identity import ManagedIdentityCredential from azure.keyvault.secrets import SecretClient key_vault_name = os.environ['KEY_VAULT_NAME'] vault_url = f'https://{key_vault_name}.vault.azure.net/' credential = ManagedIdentityCredential() secret_client = SecretClient(vault_url=vault_url, credential=credential) try: secret = secret_client.get_secret('MySecret') print(f'Secret value: {secret.value}') except Exception as e: print(f'behavior: {str(e)}') ``` I’ve verified that the environment variable `KEY_VAULT_NAME` is set correctly in the Azure Function settings. Additionally, I have granted the managed identity access to the Key Vault in the Azure portal. After doing all this, I still face the permission scenario. Is there something I am missing in the configuration or the code? Any help would be greatly appreciated! I'm developing on Debian with Python. Thanks for taking the time to read this!