Programmatically add application setting to azure functions

1k views Asked by At

I need to programmatically add new application settings to azure functions without having to add it manually from the azure portal every time I introduce a new setting. Is there any nuget package or some tool which i can use to automate this?

The idea is that when we deploy from one environment to another to automate this process without having to do this manually.

Thanks in advance.

2

There are 2 answers

0
jeffhollan On

Sounds like you just want a tool to add or update app settings without manually doing via portal? I’d recommend the azure cli as a great option. Via an ARM template works too. Very often we see users using these options in combo with a tool like Azure DevOps to automate and manage multiple environments.

1
HariHaran On

Use the .Net core configuration management style, contained in package Microsoft.Extensions.Configuration. The local.settings.json file isn't published to Azure, and instead, Azure will obtain settings from the Application Settings associated with the Function.

In your function add a parameter of type Microsoft.Azure.WebJobs.ExecutionContext context, where you can then build an IConfigurationRoot provider: Example :

[FunctionName("Stackoverflow")]
public static async Task Run([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer,
    TraceWriter log, ExecutionContext context, 
    CancellationToken ctx)
{
   var config = new ConfigurationBuilder()
        .SetBasePath(context.FunctionAppDirectory)
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();

    // This abstracts away the .json and app settings duality
    var myValue = config["MyKey"];
}

Once when its deployed to Azure, you can change the values of the settings on the function Application Settings.

enter image description here