Azure Functions - Settings
Azure Functions - Settings

Azure Functions Config Files – Host Json and Local Settings Json

In the previous post, we have created an Azure Functions project. We also have seen that Visual Studio provides choice to select OpenAPI support and hence the Swagger page is by default selected. In addition to this, we also have seen how the OpenAPI is supported via a NuGet package.

In previous post, we mostly talked about the NuGet package and the attributes. In this article, we are going to use the same project, but this time, we are going to talk about the two JSON files (i.e. host.json and local.settings.json) which come with the default project template.

Note that this discussion is valid for the runtime version 2 or later.

About host.json file

As per documentation, this host.json is a metadata file which is supposed to contain all the configuration options, which would affect all the functions in the project. In other words, configurations in this file are equally applied to each function in the function app.

This file can contain a lot of different settings. By default, when the project is created, this file, host.json, contains only logging related settings, as shown in the snapshot given below.

There are many additional settings which can be added to this file. You can find the full list of configurable settings on this page.

About local.settings.json

When we create an Azure Functions project, it may run in different environments. Two most probable environments are :

  • When it is deployed on Azure
  • When it is running locally on the developer’s computer

When the Functions project is running on Azure, the application settings are basically used. When the application is running locally, these settings can be read from local.settings.json. In fact local.settings.json file settings are used only when the application is running locally.

For knowing all the available settings (and schema), we can refer this documentation page.

Overriding host.json settings

So, there are total 3 places where the configurations can be set.

  • app settings in Azure, which are used when functions are deploye din Azure
  • host.json file
  • local.settings.json file, which are applied when application is running locally

All these three files can contain the settings. The question that may arise now is – how to decide which setting should be added in host.json and which settings should be added to application settings in Azure ?

Basically, application settings in Azure provide a way to override the settings added in host.json file, without having to modify / redeploy the application. This can be helpful to apply environment specific settings.

When you are running application locally and we need to change some host.json setting, we have option to modify it in the same file itself. But if we modify host.json, there is a chance that we may accidentally commit the modifications to the GIT and those accidental modifications may flow to other environments (dev, test / staging, production).

The best way to override the host.json settings locally is to add that setting in local.settings.json file. But the schema of these two files is not the same. So, the question is – how would this overriding work ?

In local.settings.json,  when the runtime finds an application setting in the format AzureFunctionsJobHost__path__to__setting, it overrides the equivalent host.json setting located at path.to.setting.

Notice that path__to__setting is basically a JSON path of a setting present in host.json file. But json path contains dot (.) character between the two tokens. But we have replaced the dot with double underscores.

So, let’s take an example. In the code snippet given in host.json section, there is a setting to enable logging in application insights. Let’s say we want to disable it when running the application locally. For that, let’s first find out the JSON path of the key. And the JSON Path is –

  • logging.applicationInsights.samplingSettings.isEnabled

Now, we need to apply below mentioned transformations to this path to get the setting key:

  • Replace dots with double underscores
  • Then prefix the output of previous step with AzureFunctionsJobHost__

The resulting setting key would be:

  • AzureFunctionsJobHost__logging__applicationInsights__samplingSettings__isEnabled

Now this key can be added to local.settings.json as shown in the code snippet given below.

So, that was it ! We have now fair idea about how the Azure Functions settings can be configured at different places. I hope you find this information helpful. Let me know your thoughts.

Leave a ReplyCancel reply