Rama
Rama

Reputation: 307

Override appSettings of existing azure function app via ARM templates

I have developed an azure timer trigger function. I am taking timer schedule from appSettings of the function app as following.

function.json

function.json

enter image description here

This is working fine for given static schedule. But this schedule should be able to change as per the user requirement from another web application, when user need to change the schedule.

I am struggling to change schedule parameter from external application dynamically. What i was tried is deploy an ARM templatere injecting new schedule values from following ARM template.

    {
  "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "name": {
      "type": "String"
    },
    "location": {
      "type": "String"
    },
    "subscriptionId": {
      "type": "String"
    },
    "schedule1": {
      "type": "String"
    },
    "schedule2": {
      "type": "String"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Web/sites",
      "kind": "functionapp",
      "name": "[parameters('name')]",
      "apiVersion": "2016-03-01",
      "location": "[parameters('location')]",
      "properties": {
        "name": "[parameters('name')]",
        "siteConfig": {
          "appSettings": [
            {
              "name": "schedule1",
              "value": "[parameters('schedule1')]"
            },
            {
              "name": "schedule2",
              "value": "[parameters('schedule2')]"
            }
          ]
        },
        "clientAffinityEnabled": false,
        "reserved": false
      }
    }
  ]
}

However, this is not overriding existing appSettings. Instead, it returns an error "Web site already exists" Is there any method to override appSettings as explained above and restart the function app in order to affect new appSettings parameters.

Upvotes: 1

Views: 3216

Answers (2)

Klaymen
Klaymen

Reputation: 83

For anyone coming across this, it is possible to override the Microsoft.Web/sites resource's app settings with an arm template (might be important for some buildout situations, to avoid having to use scripts in addition to arm templates):

Create the app settings as an outer child resource:

"resources": [
    {
        "type": "Microsoft.Web/sites",
        "kind": "functionapp",
        "name": "[parameters('name')]",
        "apiVersion": "2016-03-01",
        "location": "[parameters('location')]",
        "properties": {
            "name": "[parameters('name')]",
            "clientAffinityEnabled": false,
            "reserved": false
        }
    },
    {
        "name": "[concat(parameters('name'), '/', 'appsettings')]",
        "type": "Microsoft.Web/sites/config",
        "apiVersion": "2018-11-01",
        "dependsOn": [
            "[resourceId('Microsoft.Web/sites',parameters('name'))]"
        ],
        "properties": {
            "schedule1": "[parameters('schedule1')]",
            "schedule2": "[parameters('schedule2')]"
        }
    }
]

That way it isn't detected as an override to the website and thus allowed to be deployed.

Upvotes: 0

Joy Wang
Joy Wang

Reputation: 42063

Per my test, your template works fine on my side.

Just some information for you to refer.

This is my function app:

enter image description here

Test the template in the portal:

enter image description here

Deploy result:

enter image description here

Check in the portal:

enter image description here

Note: It will overwrite all the settings of the app, before the deployment, there were other settings in my app, currently, there are just two settings.

Besides, instead of using ARM template, I recommend you to use REST API, essentially, the template is also calling API. You could also use powershell to do it, here is a similar post, you could refer to it.

Upvotes: 4

Related Questions