CodeMonkey
CodeMonkey

Reputation: 12444

Using appsettings in Azure function

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?

Upvotes: 5

Views: 17916

Answers (2)

Ivan Pavlov
Ivan Pavlov

Reputation: 55

You can use local.settings.json to store parameters for local development BUT values must be stored inside the "Values" object in the flat format. For example, in a usual .NET application, we store settings in objects like this:

"Credentials" : {
    "TenantId": "",
    "ClientId": "",
    "ClientSecret": ""
}

but local.settings.json allows only strings, so the settings should be written as:

"Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "Credentials:TenantId": "",
    "Credentials:ClientId": "",
    "Credentials:ClientSecret": ""
}

Upvotes: 1

rickvdbosch
rickvdbosch

Reputation: 15621

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.

Upvotes: 2

Related Questions