Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best place to store environment variables for Azure Function

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!

like image 708
DarthVadar123451 Avatar asked Jan 05 '17 01:01

DarthVadar123451


People also ask

Where are Azure environment variables stored?

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.

Where do I put environment variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

How do I set an environment variable in Azure function app?

To begin, go to the Azure portal and sign in to your Azure account. In the search bar at the top of the portal, enter the name of your function app and select it from the list. Under Settings in the left pane, select Configuration.


1 Answers

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"   } } 
like image 155
chris31389 Avatar answered Sep 20 '22 14:09

chris31389