Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 986
Mike Baron Avatar asked Feb 07 '19 18:02

Mike Baron


People also ask

How do I set an Azure environment variable?

To set environment variables when you start a container in the Azure portal, specify them in the Advanced page when you create the container. Under Environment variables, enter NumWords with a value of 5 for the first variable, and enter MinLength with a value of 8 for the second variable.

How do you specify app settings for an Azure Functions project?

To find the application settings, see Get started in the Azure portal. The Application settings tab maintains settings that are used by your function app. You must select Show values to see the values in the portal. To add a setting in the portal, select New application setting and add the new key-value pair.


2 Answers

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));
like image 196
Kane Avatar answered Oct 02 '22 03:10

Kane


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

like image 20
I Stand With Israel Avatar answered Oct 02 '22 04:10

I Stand With Israel