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!
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.
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.
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.
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" } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With