3

This is a follow up question to this question:

appsettings.json and azure function by timer

I want to have dynamic settings which I can change without redeploying the Azure function.

The linked question referrers to a short article: https://medium.com/awesome-azure/azure-reading-application-settings-in-azure-functions-asp-net-core-1dea56cf67cf

I understood from the article how to handle local settings meant for debugging locally using the local.settings.json file. What I didn't understand is what I need to do to add settings when running in the cloud and how exactly to read them.

So my questions are:

  1. How do I add settings to read when running in Azure (not sure from the article what is this "Application Settings" in the picture there)?

  2. Will using var value = Environment.GetEnvironmentVariable("your_key_here"); work for both local environment and when running in Azure?

  3. How can I change one of the setting from the outside without having to redeploy the function?

  4. The article I linked above shows a code that creates a config var in the run method. Do I need to add it somewhere as well? Since if the keys are in the application settings in azure or in the json file for local development, why is this code needed? Can't I just use GetEnvironmentVariable also in the Run method?

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203

1 Answers1

2

In code, read the settings from environment variables. Then...

  • For local development, you can add settings in the local.settings.json file.
  • In Azure, add settings with the same name in Application settings
  • Application settings override settings.json

App settings in a function app contain configuration options that affect all functions for that function app. When you run locally, these settings are accessed as local environment variables.

and

The function app settings values can also be read in your code as environment variables.

See Use application settings.

  1. By adding them in the Application Settings for the Function App
  2. Yes.
  3. Change it in Application Settings (it will restart)

Can't I just use GetEnvironmentVariable also in the Run method
4. Yes, you can. Adding it to a config variable enables you to read it more easily in multiple locations.

For an even more centralized way of working with settings and not restarting your application (if you don't want it to), have a look at App Configuration.

rickvdbosch
  • 14,105
  • 2
  • 40
  • 53
  • About what you wrote "App settings in a function app contain configuration options that affect all functions for that function app. When you run locally, these settings are accessed as local environment variables.". Does it mean that I can only set the keys and values in the application settings in Azure and it will also work locally if I'm running the function app from Visual Studio? – CodeMonkey Apr 05 '22 at 10:40
  • Yes. Locally, settings from local.settings.json will be available as environment variables. In Azure, if you provide an Application Setting with the same name, it will override the existing environment variable. – rickvdbosch Apr 05 '22 at 10:43
  • What I meant to ask is that if I set the Application settings, does it mean I can avoid adding the local.settings.json file and still have the values when querying the env variables locally? Meaning if it knows to take the settings from Azure even when running locally if no local.settings.json file exists? – CodeMonkey Apr 05 '22 at 10:50
  • No. Since the connection to Azure (and what Azure Function it would be connected to) is not necessarily known when running locally, you will need to add configuration locally. Using App Configuration (see link in answer) you would be able to get the information from Azure while running locally. Here's an [introduction](https://www.rickvandenbosch.net/blog/azure-app-configuration-an-introduction/). – rickvdbosch Apr 05 '22 at 10:54
  • I added question number 4 (edited original question) if you might be able to also edit your answer please if you know the answer for that one as well? – CodeMonkey Apr 07 '22 at 11:57
  • @YonatanNir I did see your added question. However, editing your question to add additional questions is frowned upon. If you have additional questions, please open a new one. Unaccepting the answer because someone didn't respond to an added question is simply wrong. – rickvdbosch Apr 20 '22 at 13:03
  • Ok so is it possible if you could also edit your answer to answer the 4th one? Generally opening an entirely new question for the same exact topic is frowned upon. It's a shame if someone came across this answer and won't have the full information – CodeMonkey Apr 24 '22 at 07:08
  • It's already there – rickvdbosch Apr 24 '22 at 08:04
  • Was wondering.. why do you think that adding the code which reads the configs to a variable makes it easier to read from multiple locations? Since then I need to pass that var to all the places I want instead of just using a single line of GetEnvironmentVariable wherever I want without being dependent on that var – CodeMonkey Apr 24 '22 at 10:13
  • 1
    Since it'll enable you to use Dependency Injection and an `IConfiguration` – rickvdbosch Apr 24 '22 at 16:29