17

I've got an ARM template (included below) to deploy an Azure Function App. I deploy it with:

az group deployment create --resource-group my-group --template-file my-function-app.json

This works and I can then deploy my functions successfully using the VS Code plugin or Azure Functions Core Tools.

However, if I then re-deploy the ARM template (for example to update an application setting) then I lose my functions and need to re-deploy them again. Is this expected behaviour? It's not what I observe when deploying e.g. a Web App via an ARM template. Is there something specific I can do when deploying an ARM template for a Function App to preserve my deployed functions?

my-function-app.json:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        ...
    },
    "variables": {
        ...
    },
    "resources": [
        {
            "apiVersion": "2015-08-01",
            "type": "Microsoft.Web/sites",
            "name": "[variables('collectorFunctionAppName')]",
            "location": "[parameters('location')]",
            "kind": "functionapp",
            "properties": {
                "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
                "siteConfig": {
                    "appSettings": [
                        {
                            ...
                        }
                    ]
                }
            }
        }
    ],
    "outputs": {}
}
eoinmullan
  • 1,157
  • 1
  • 9
  • 32
  • Once you create the function app normally and then you create the functions in it manually, find the path where it's saving the functions by default in Kudu. Once you get that you can add : "appSettings": [ { "name": "Project", "value": "src" } ] to the appSettings, and it would by default pick the functions available there. I am not sure of this solution, haven't tried yet, let me know if it works out. Refer: [link](https://learn.microsoft.com/en-us/azure/azure-functions/functions-infrastructure-as-code#create-a-function-app-1) – Abhirup Guha Dec 05 '18 at 06:51

3 Answers3

26

Are you deploying your function as a package? If so, make sure you set this setting in your template, since it will be removed when you redeploy otherwise:

{ "name": "WEBSITE_RUN_FROM_PACKAGE", "value": "1" }

curious coder
  • 831
  • 1
  • 8
  • 13
  • My solution is now working. I think the problem was that deploying from Azure Functions Core Tools added WEBSITE_RUN_FROM_ZIP= to the application settings. If I remove that setting then further deployments do not delete my functions. However, adding your suggestion prevents Azure Functions Core Tools from adding the WEBSITE_RUN_FROM_ZIP setting so it does help, but I'm not sure I can say it answers the question. The fact is I'd need to do a bit more research to really get to the bottom of this. – eoinmullan Dec 06 '18 at 09:48
  • 2
    I'd encountered this issue recently and it confused me as to why my two new App Services, one an Azure Function, were losing their code on template redeployment. This was not the behavior I had come to expect. This was the answer. – Robb Vandaveer Aug 07 '19 at 01:57
  • 1
    This worked for me. Thanks – jamesioppolo Sep 23 '21 at 05:43
  • 1
    Really good answer; lead me to this post. Found it quite useful to know why it works https://marcstan.net/blog/2020/03/azure-app-service-run-from-package/ – Jamez Dec 11 '22 at 00:55
0

You could try "--mode incremental" parameter although that should be the default when it is not provided.

mkstr
  • 153
  • 1
  • 11
-1

Yes that should be the expected behavior.

ARM Template is a declarative deployment meaning anytime you deploy it will overwrite anything you have with new template information. The template should always include everything you need.

Hannel
  • 1,656
  • 3
  • 10
  • 17
  • 1
    Since he is deploying the same template with revised application settings, it should just do an incremental update to the application settings and not the whole function itself. This might be a drawback in Azure Functions only, have to research more. – Abhirup Guha Dec 05 '18 at 06:41
  • Should have waited till you research before down voting a correct response. – Hannel Dec 05 '18 at 07:29
  • 2
    this doesnt make a lot of sense, unfortunately. even if this is by design, it shouldnt work this way from the solely declarative point of view, since he is not defining functions anywhere – 4c74356b41 Dec 05 '18 at 19:19