Mike Baron
Mike Baron

Reputation: 848

Azure Function Environment Variables

I'm trying to create a super basic Azure Function, but am having trouble with environment variables. Following various tutorials online,

        var config = new ConfigurationBuilder()
             .SetBasePath(context.FunctionAppDirectory)
             .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
             .AddEnvironmentVariables()
             .Build();

        log.Info(config["AzureWebJobsStorage"]);

My local.settings.json looks like this:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "<language worker>",
    "AzureWebJobsStorage": "abc123",
    "AzureWebJobsDashboard": "abc123",
    "MyBindingConnection": "abc123"
  },
  "Host": {
    "LocalHttpPort": 7071,
    "CORS": "*"
  }
}

When I run this locally, that

log.Info(config["AzureWebJobsStorage"]); 

line returns nothing... but when I deploy to Azure I see "abc123" in the console.

If, however, I alter that line to

log.Info(config["Values:AzureWebJobsStorage"]);

Then when I run locally, I see "abc123" but when I deploy to Azure I see nothing.

Is there something I'm missing to be able to reach the environment variables the same way locally vs deployed?

EDIT: To clarify, these settings are configured in the app settings for the function: enter image description here

Upvotes: 8

Views: 17198

Answers (2)

Kane
Kane

Reputation: 16802

Assuming you are using targeting the ~2 runtime for your Azure Functions you can access your configuration values through:

log.Info(Environment.GetEnvironmentVariable("AzureWebJobsStorage", EnvironmentVariableTarget.Process));

Upvotes: 8

I Stand With Israel
I Stand With Israel

Reputation: 2389

Those environmental variables work when you are testing your function locally. However, when you deploy to the Azure Function Portal, you need to setup your variables using their built-in system to handle Environmental Variables.

Copy and past your key-values into the sections that I highlighted in the image below.

<Azure Functions Portal - Environmental

Upvotes: 3

Related Questions