Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions v2 - New .netcore Configuration and deployment

Now that we can use the hugely flexible configuration engine from .NETCore - we can do something like this :

   private static IConfigurationRoot SetConfig(ExecutionContext executionContext)
    {
        return new ConfigurationBuilder()
           .SetBasePath(executionContext.FunctionAppDirectory)
           .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
           .AddEnvironmentVariables()
           .Build();
    }

Which is great as it allow you to put more complicated configuration data in the config file - for instance

    {
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "<< removed >>",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "<< removed >>"
  },
  "MyCustomSettings": [
    {
      "ConnectionString": "<< removed >>",
      "Folders": [
        {
          "ShareName": "share1",
          "FolderName": "folder1"
        },
        {
          "ShareName": "share2",
          "FolderName": "folder2"
        }
      ]
    }
  ]
}

Again - great news! I can now get access to my strongly typed configuration with config["MyCustomSettings"]

What I don't get though - is how this can be deployed when publishing the function. Only the Values section is migrated to the Azure function Application Settings. I can obviously put this custom json in a json file and add it to the load statement like the local.settings.json

.AddJsonFile("my-custom-settings.json", optional: false, reloadOnChange: true)

but then this file has to be included in the deploy, and is not stored securely.

Any ideas?

like image 430
Andrew G Avatar asked Oct 08 '18 12:10

Andrew G


People also ask

How do you deploy Azure function from Visual Studio to Azure portal?

Use the following steps to publish your project to a function app in Azure. In Solution Explorer, right-click the project and select Publish. In Target, select Azure then Next. Select Azure Function App (Windows) for the Specific target, which creates a function app that runs on Windows, and then select Next.


1 Answers

This is not officially supported as of Nov 2019.

Warning

Avoid attempting to read values from files like local.settings.json or appsettings.{environment}.json on the Consumption plan. Values read from these files related to trigger connections aren't available as the app scales because the hosting infrastructure has no access to the configuration information.

There are so many blogs around advising to do this and it appears this may work if you only have a single instance, but as soon as the ScaleController triggers scaling, new instances will not be able to find the config files.

If you have triggers that use the %SettingName% syntax, they will not work as the function scales.

Functions team is considering possible Options (lol)

There is also the option of using the new App Configuration service but it is currently in preview and isn't available in all Azure regions.

It may be simpler to put your config in blob storage, and load it at startup? (Your blob store connectionstring will need to be in env variables)

So far the best we can do is to include your "not so secret" settings (things like MyThingTimeout or ExternalEndpointAddress) in a json file and use .AddJsonFile(...) and put secrets in KeyVault. This does force you to split your config and decide which goes where. (And also make sure your triggers only read from the Values section/Environment Variables)

like image 98
Madushan Avatar answered Sep 19 '22 21:09

Madushan