Add config sections and values in Azure Function Configuration

Most developers are familiar with defining configuration sections in appsettings.json or a json file, and then read it through IConfiguration. However, Azure Functions uses environment variables. Though there are ways you can use a json file in azure functions, it will hamper the flexibility of configuring azure functions.

Environment variables are stored as a key-value pair and therefore at top level, it may seem that there is no way to accomplish sections and values. However, you can always specify a section as below.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    // This is the custom setting which would move to environment variable while deployment
    "MySection:Key1": "Value1",
    "MySection:Key2": "Value2"
  }
}

The above settings goes to local.settings.json in a developer’s environment and to environment variables in Azure

Your code now can either call IConfiguration.GetSection(“MySection”).GetValue(“Key1”) or IConfiguration.GetValue(“MySection:Key1”)

Add config sections and values in Azure Function Configuration

Leave a Reply

Scroll to top
%d bloggers like this: