azure-webappspulumipulumi-pythonpulumi-azure

How to add a new app setting to Azure Web App using pulumi without removing the existing settings?


I'm using pulumi azure native for infrastructure as code. I need to create an Azure Web App (based on an App Service Plan) and add some app settings (and connection strings) throughout the code, e.g., Application Insights instrumentation key, Blob Storage account name, etc.

I figured out that there is a method, WebAppApplicationSettings, that can update web app settings:

from pulumi_azure_native import web

web_app = web.WebApp(
    'my-web-app-test123',
    ...
)

web.WebAppApplicationSettings(
    'myappsetting',
    name=web_app.name,
    resource_group='my-resource-group',
    properties={'mySetting': 123456},
    opts=ResourceOptions(depends_on=[web_app])
)

It turns out that WebAppApplicationSettings replaces the entire app settings with the value given in the properties parameter, which is not what I need. I need to append a new setting to the existing settings.

So, I tried this:

  1. Fetch the existing settings from web app using list_web_app_application_settings_output
  2. Add the new settings the existing settings
  3. Update the app settings using WebAppApplicationSettings
from pulumi_azure_native import web

app = web.WebApp(
    'my-web-app-test123',
    ...
)

current_apps_settings = web.list_web_app_application_settings_output(
    name=web_app.name,
    resource_group_name='my-resource-group',
    opts=ResourceOptions(depends_on=[web_app])
).properties

my_new_setting = {'mySetting': 123456}
new_app_settings = Output.all(current=current_apps_settings).apply(
    lambda args: my_new_setting.update(args['current'])
)

web.WebAppApplicationSettings(
    'myappsetting',
    name=app.name,
    resource_group='my-resource-group',
    properties=new_app_settings,
    opts=ResourceOptions(depends_on=[web_app])
)

However, this doesn't work either and throws the following error during pulumi up:

 Exception: invoke of azure-native:web:listWebAppApplicationSettings failed: invocation of azure-native:web:listWebAppApplicationSettings returned an error: request failed /subscriptions/--------------/reso
urceGroups/pulumi-temp2/providers/Microsoft.Web/sites/my-web-app-test123/config/appsettings/list: autorest/azure: Service returned an error. Status=404 Code="ResourceNotFound" Message="The Resource 'Microsoft.Web/sites/my-web-app-test123' under resource group 'pulumi-temp2' was not found. For more details please go to https://aka.ms/ARMResourceNotFoundFix"
    error: an unhandled error occurred: Program exited with non-zero exit code: 1

Is there way that I can add a new app setting to Azure Web App using pulumi without changing/removing the existing settings?


Solution

  • After some searching and reaching out to pulumi-azure-native people, I found an answer:

    Azure REST API doesn't currently support this feature, i.e., updating a single Web App setting apart from the others. So, there isn't such a feature in pulumi-azure-native as well.

    As a workaround, I stored (kept) all the app settings I needed to be added, updated, or removed in a dictionary throughout my Python script, and then I passed them to the web.WebAppApplicationSettings class at the end of the script so that they will be applied all at once to the Web App resource. This is how I solved my problem.