Azure Function(Python Runtime): Local development store environment variable

Yash 86 Reputation points
2022-04-09T03:44:19.86+00:00

I'm creating an Azure Function with Python 3.9 runtime. The function is a timer triggered function that will read credentials from Azure Key Vault, and call the rest api accordingly. In the azure documentation, it is shown the best approach will be to store the key vault url in an environment variable. I have tried to store the key vault url value in local.settings.json, and tried to access it using os.environ() dictionary, but I'm not able to access the url.

Can anyone please help me to understand, what can be the best approach to store environment variable when I'm developing azure function locally(using Visual Studio Code) using Python runtime, so that I don't have to change the code to access the value in azure function(in azure cloud) as well as when I'm developing locally?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,908 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. VenkateshDodda-MSFT 24,946 Reputation points Microsoft Employee Moderator
    2022-04-13T05:28:31.69+00:00

    @Yash Thank you for reaching out. I have tested the scenario in my local environment by creating the Timmer trigger function, stored the key Vault name as environment variable in the local.settings.json file. As documented here I have used os.environ() to get the key vault name and was able to retrieve the secrets.

    init.py:

    import datetime  
    import logging  
    import os  
    from azure.keyvault.secrets import SecretClient  
    from azure.identity import DefaultAzureCredential  
      
    import azure.functions as func  
      
    def main(mytimer: func.TimerRequest) -> None:  
        utc_timestamp = datetime.datetime.utcnow().replace(  
        tzinfo=datetime.timezone.utc).isoformat()  
      
        keyVaultName = os.environ["KEY_VAULT_NAME"]  
        KVUri = fhttps://{keyVaultName}.vault.azure.net  
        credential = DefaultAzureCredential()  
        client = SecretClient(vault_url=KVUri, credential=credential)  
        secret=client.get_secret("keytest")  
        logging.info(secret.value)  
        logging.info('Python timer trigger function ran at %s', utc_timestamp)  
    

    local.setting.json:

    {  
      "IsEncrypted": false,  
      "Values": {  
        "AzureWebJobsStorage": "<<yourstorageconnectionstring>>",  
        "FUNCTIONS_WORKER_RUNTIME": "python",  
        "KEY_VAULT_NAME":"<<yourkeyvalutname>>"  
      }  
    }  
    

    requirements.txt

    azure-functions  
    azure-identity  
    azure-keyvault  
    azure-keyvault-secrets  
    

    Output:

    eb1fppM.png

    Can anyone please help me to understand, what can be the best approach to store environment variable when I'm developing azure function locally(using Visual Studio Code) using Python runtime, so that I don't have to change the code to access the value in azure function(in azure cloud) as well as when I'm developing locally?

    It is always recommended to store the environment variables in local.settings.json file during your local development but when deploying to function make sure your local.settings.json settings are reflected/uploaded in the configuration blade of your function app.

    Feel free to get back to me if you need any assistance.

    0 comments No comments

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.