Update an app setting in Azure Functions using SDKs or PowerShell

Antonio Miron 31 Reputation points
2020-09-25T01:09:36.373+00:00

We want to update only one application setting programmatically in an Azure Function App after it has been deployed. We have tried with .NET SDK, Python and PowerShell and so far have been unsuccessful.

The reason for being unable to do it is caused by two factors:

  1. When sending one app setting value in the update operation all of the other app settings are deleted, thus ruining our Azure Function app.
  2. The solution would be to retrieve the existing app settings and add the new one, then perform the update operation. However, the SDKs and PowerShell always return an empty object when querying the app settings, presumably for security reasons.

Surprisingly, the Azure CLI seems to be the only method where this update actually works, which seems to be inconsistent with the behaviour of other tools. The Azure CLI is not suitable for our needs, as we would like to perform this operation from another function app (or some compute that can react to events).

Then the question is:
Is there a way to perform a single app setting update while preserving the existing app settings from any of the SDKs or PowerShell, excluding the Azure CLI? Any existing API that can be leveraged for this?

Below there are code snippets in C# and PowerShell that we have used to attempt to update app settings, resulting in the existing ones being deleted:

C#

           var tokenp = new AzureServiceTokenProvider();

            var at = await tokenp.GetAccessTokenAsync("https://management.azure.com/");

            var c = new WebSiteManagementClient(new TokenCredentials(at))

            {

                SubscriptionId = "Subscription_id"

            };



            log.LogInformation("Retrieved token and Credentials");

            var settings = new Dictionary<string, string>();

            settings.Add("mySetting", "myValue!");

            var stringDictSettings = new StringDictionary(properties: settings);

            await c.WebApps.UpdateApplicationSettingsAsync("RGName", "FunctionAppName", stringDictSettings);     

PowerShell

$settings = @{"NewSetting"="aValue"}
Set-AzWebApp -AppSettings $settings -Name func-app-name -ResourceGroupName RG-NAME 
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
0 comments No comments
{count} vote

Accepted answer
  1. MayankBargali-MSFT 70,936 Reputation points Moderator
    2020-09-25T05:01:04.647+00:00

    Hi @Antonio Miron

    This is expected behavior when you are using the CLI command for webconfig as CLI exposes different command delete, list, set, therefore the delete and set retain the existing configuration.

    If you are using PowerShell then there is no such command that will retain the value. Therefore you need to first get the app setting using the GetAzWebApp command and then call the Set-AzWebApp to update the settings. Any addition, removal of the app setting you need to pass the object again that overwritten all the existing settings. If you are not passing the existing values then it will only retain the new app settings.

    For PowerShell I have tested the below script and it is working as expected.

    $appset = (Get-AzWebApp -Name 'webappname' -ResourceGroupName 'resourcegroupname').SiteConfig.AppSettings   
    $newsettings = new-object Microsoft.Azure.Management.WebSites.Models.NameValuePair  
    $newsettings.Name = "key"  
    $newsettings.Value = "value"  
      
    $appset.Add($newsettings)  
      
    $newappset = @{}  
    $appset | ForEach-Object {  
            $newappset[$_.Name] = $_.Value  
       }  
      
    Set-AzWebApp -AppSettings $newappset -Name webappname-ResourceGroupName resourcegroupname  
    

    Similarly, when you are using Microsoft.Azure.Management.WebSites package then you need to call ListApplicationSettings or ListApplicationSettingsAsyn first to get the application settings and then make the changes to the object (add/delete) and then pass that object to UpdateApplicationSettingsAsync method as we have done in the above PowerShell script.

    Hope the above helps you to resolve the issue. Feel free to reach out to me if you need any assistance.

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.