Azure Function ARM template deletes the existing application settings those are not part of the template file

1.2k Views Asked by At

I have created function app with default application settings using ARM template. Unfortunately, when the ARM template deployed it deleted any application settings that were not defined in the ARM template.

This is ARM template using for function app:

    {
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "functionAppName": {
      "type": "string",
      "defaultValue": "[concat('fnapp', uniqueString(resourceGroup().id))]",
      "metadata": {
        "description": "The name of the function app that you wish to create."
      }
    },
    "newOrExisting": {
      "type": "string",
      "allowedValues": [
        "new",
        "existing"
      ],
      "metadata": {
        "description": "Choose the app service plan based on these values"
      }
    },
    "existingHostingPlanName": {
      "type": "string",
      "metadata": {
        "description": "The name of the existing hosting plan"
      }
    },
    "existingResourceGroupName": {
      "type": "string",
      "metadata": {
        "description": "The name of the existing resource group"
      }
    },
    "newHostingPlanName": {
      "type": "string",
      "metadata": {
        "description": "The name of the new hosting plan"
      }
    },
    "sku": {
      "type": "string",
      "allowedValues": [
        "D1",
        "F1",
        "B1",
        "B2",
        "B3",
        "S1",
        "S2",
        "S3",
        "P1",
        "P2",
        "P3",
        "P1V2",
        "P2V2",
        "P3V2",
        "I1",
        "I2",
        "I3",
        "Y1"
      ],
      "defaultValue": "S1",
      "metadata": {
        "description": "The pricing tier for the hosting plan."
      }
    },
    "workerSize": {
      "type": "string",
      "allowedValues": [
        "0",
        "1",
        "2"
      ],
      "defaultValue": "0",
      "metadata": {
        "description": "The instance size of the hosting plan (small, medium, or large)."
      }
    },
    "applicationInsightsName": {
      "type": "string",
      "metadata": {
        "description": "The Application Insights Name."
      }
    },
    "storageAccountName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Storage Account."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "runtime": {
      "type": "string",
      "defaultValue": "dotnet",
      "allowedValues": [
        "node",
        "dotnet",
        "java"
      ],
      "metadata": {
        "description": "The language worker runtime to load in the function app."
      }
    },
    "identityType": {
      "type": "string",
      "metadata": {
        "description": "The Identity type"
      }
    },
    "functionRuntimeVersion": {
      "type": "string",
      "defaultValue": "~3",
      "metadata": {
        "description": "The runtime version for the function app"
      }
    }
  },
  "variables": {
    "serverFarmId": "[ if(equals(parameters('newOrExisting'),'new'),resourceId('Microsoft.Web/serverfarms',parameters('newHostingPlanName')), concat(subscription().id,'/resourceGroups/',parameters('existingResourceGroupName'),'/providers/Microsoft.Web/serverfarms/',parameters('existingHostingPlanName')))]",
    "functionWorkerRuntime": "[parameters('runtime')]"
  },
  "resources": [
    {
      "condition": "[equals(parameters('newOrExisting'),'new')]",
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2020-06-01",
      "name": "[parameters('newHostingPlanName')]",
      "location": "[parameters('location')]",
      "sku": {
        "Name": "[parameters('sku')]"
      },
      "properties": {
        "name": "[parameters('newHostingPlanName')]",
        "workerSize": "[parameters('workerSize')]",
        "numberOfWorkers": 1
      }
    },
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2020-06-01",
      "name": "[parameters('functionAppName')]",
      "location": "[parameters('location')]",
      "kind": "functionapp",
      "dependsOn": [
        "[resourceId('microsoft.insights/components', parameters('applicationInsightsName'))]"
      ],
      "identity": {
        "type": "[parameters('identityType')]"
      },
      "properties": {
        "serverFarmId": "[variables('serverFarmId')]",
        "httpsOnly": true,
        "siteConfig": {
          "alwaysOn": true,
          "ftpsState": "FtpsOnly",
          "appSettings": [
            {
              "name": "AzureWebJobsStorage",
              "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', parameters('storageAccountName'), ';EndpointSuffix=', environment().suffixes.storage, ';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')), '2019-06-01').keys[0].value)]"
            },
            {
              "name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
              "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', parameters('storageAccountName'), ';EndpointSuffix=', environment().suffixes.storage, ';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')), '2019-06-01').keys[0].value)]"
            },
                        {
              "name": "WEBSITE_CONTENTSHARE",
              "value": "[toLower(parameters('functionAppName'))]"
            },
            {
              "name": "WEBSITE_RUN_FROM_PACKAGE",
              "value": "1"
            },
            {
              "name": "FUNCTIONS_EXTENSION_VERSION",
              "value": "[parameters('functionRuntimeVersion')]"
            },
            {
              "name": "WEBSITE_NODE_DEFAULT_VERSION",
              "value": "~10"
            },
            {
              "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
              "value": "[reference(resourceId('microsoft.insights/components', parameters('applicationInsightsName')), '2020-02-02-preview').InstrumentationKey]"
            },
            {
              "name": "FUNCTIONS_WORKER_RUNTIME",
              "value": "[variables('functionWorkerRuntime')]"
            }
          ]
        }
      }
    },
    {
      "type": "microsoft.insights/components",
      "apiVersion": "2020-02-02-preview",
      "name": "[parameters('applicationInsightsName')]",
      "location": "[parameters('location')]",
      "tags": {
        "[concat('hidden-link:', resourceId('Microsoft.Web/sites', parameters('applicationInsightsName')))]": "Resource"
      },
      "properties": {
        "ApplicationId": "[parameters('applicationInsightsName')]",
        "Request_Source": "IbizaWebAppExtensionCreate"
      }
    }
  ],
  "outputs": {}
}

I have referred this git hub issue but still it is open.

So, can anyone suggest me how to resolve this issue?

1

There are 1 best solutions below

1
On

This is a normal phenomenon, when you deploy the template, there are two modes, Complete and Incremental, but even if with Incremental mode, the appsettings which is a child resource of Microsoft.Web/sites/config will be overwritten if you provide new values(web app and function app are the same).

Reference - https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/deployment-modes#incremental-mode

enter image description here

So in your case, if you want to keep the default settings, you also need to add them to your temaplate.