jabberwocky
jabberwocky

Reputation: 1055

Azure Functions, make local.settings.json values take precedence over System environment variables

In Azure Functions you can pass dummy environment variables through local.settings.json, which you set as application settings (ideally with key vault) when you deploy.

However when you run locally, i.e. func host start --function=MyFunction it always takes your locally defined environment variables over those in the settings file.

I want to access the variable in local.settings.json instead of the value in my environment variables.

I can't find one, and it's annoying if you want to test without having to manually change your environment variables.

Links: Python docs on env vars.

Local.settings.json docs

Existing question on how to store environment variables

Upvotes: 4

Views: 5290

Answers (1)

George Chen
George Chen

Reputation: 14324

For now this is not supported if you are using python function. You could get a prompt that the key is skipped.

enter image description here

So if you want to use same setting in the local you could read the json file the get the value.

Below is my code to access the testkey in the local.settings.json.

    ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
    with open(ROOT_DIR+'\\local.settings.json') as f:
        data = json.load(f)
        testkey=data['Values']['testkey']
        logging.info(testkey)

enter image description here

Upvotes: 4

Related Questions