41

I'm testing an azure function locally with several api keys. Whats the best place to store environment variables and how do I access them? I tried

System.Environment.GetEnvironmentVariable("name")

but I'm not sure where the environment variable is stored.

Thanks!

Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165
DarthVadar123451
  • 619
  • 1
  • 6
  • 14

4 Answers4

59

You should have a file called local.settings.json. Here is the azure website for Functions-Run-Local

It states

These settings can also be read in your code as environment variables. In C#, use System.Environment.GetEnvironmentVariable or ConfigurationManager.AppSettings. In JavaScript, use process.env. Settings specified as a system environment variable take precedence over values in the local.settings.json file.

example local.settings.json

{
  "IsEncrypted": false,   
  "Values": {
    "AzureWebJobsStorage": "<connection string>", 
    "AzureWebJobsDashboard": "<connection string>" 
  },
  "Host": {
    "LocalHttpPort": 7071, 
    "CORS": "*" 
  },
  "ConnectionStrings": {
    "SQLConnectionString": "Value"
  }
}

It says that you need to put the application settings under the Values property within the local.settings.json.

To retrieve I used ConfigurationManager.AppSettings["CustomSetting"] as it lets you retrieve connection strings.

I have just been playing around with this and discovered that you have to have a a string key and a string value. I got an error when I tried having a subsection (like you would in an appsettings.json). I had to have the local.settings.json look like this:

{
  "IsEncrypted": false,   
  "Values": {
    "AzureWebJobsStorage": "<connection string>", 
    "AzureWebJobsDashboard": "<connection string>" 
    "CustomSetting":"20"
  }
}
chris31389
  • 8,414
  • 7
  • 55
  • 66
22

In Azure Functions 2, we moved to ASP.NET Core configuration, and ConfigurationManager is no longer supported. This remains true in 3.

I have seen many articles where people are spraining themselves getting ConfigurationBuilder setups working. The problem is that once you start doing DI, which was recently added, some annoying scenarios arise caused by not having access to a Context parameter (1). It is much cleaner to just use Environment.GetEnvironmentVariable.

This maps perfectly to the configuration model of Azure Functions: in dev, it picks up items in the local.settings.json > Values array, and in production it picks up your environment variables. It just works.

I see no downside other than it's different than what we do in MVC. But this is not MVC, it is Functions, and until MS brings these platforms into better alignment, we should open our minds to what is best in Functions rather than forcing a different paradigm to work here.

Your local.settings.json might look like this:

{
    "IsEncrypted": false,
    "Values": {
        "KeystoneDB": "[CONNECTION STRING HERE]"
        "FUNCTIONS_WORKER_RUNTIME": "dotnet"
    }
}

And in production, you set the environment variables in the portal.

(1) There's no other clean way to satisfy ConfigurationBuilder().SetBasePath that I'm aware of. See: Get root directory of Azure Function App v2

Brian MacKay
  • 31,133
  • 17
  • 86
  • 125
12

To store api keys you can use Application Settings. Details: https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings#application-settings

To get an environment variable or an app setting value, use System.Environment.GetEnvironmentVariable, as shown in the following code example:

public static void Run(TimerInfo myTimer, TraceWriter log)
{
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
    log.Info(GetEnvironmentVariable("AzureWebJobsStorage"));
    log.Info(GetEnvironmentVariable("WEBSITE_SITE_NAME"));
}

public static string GetEnvironmentVariable(string name)
{
    return name + ": " + 
        System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
}

Details: https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#environment-variables

Thanks, Alexey

Alexey Rodionov
  • 1,436
  • 6
  • 8
  • 1
    Nice article. It's difficult to navigate the azure pages to find ones like this. I'm actually looking to test this locally without having settings stored in Azure which I gathered from the above that key/value pairs belong in appSettings.json under the values property. – DarthVadar123451 Jan 05 '17 at 19:43
  • Can you give an example of how a local.settings.json would look for AzureWebJobsStorage and WEBSITE_SITE_NAME? – chris31389 Nov 22 '17 at 11:55
  • Looks like you can use env variables in Azure and locally set them in visual studio for local runtime: https://msdn.microsoft.com/en-us/library/669zx6zc.aspx#Setting environment variables for a build – th3morg Feb 04 '18 at 02:15
3

Put key/value pairs in the appsettings.json file under the "Values" property.

Nate
  • 698
  • 1
  • 7
  • 18
DarthVadar123451
  • 619
  • 1
  • 6
  • 14