How do you deploy a python azure function with an arm template?

Question:

The following deploys a azure function that run the specified C#. How do I do the same for a function that should run python?

I tried just changing the name to __init__.py as is generated when you use the azure-function-core-tools func command with the --python switch, but couldn’t even find error messages as to why things weren’t working.

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "appName": {
            "type": "string",
            "metadata": {
                "description": "The name of the function app that you wish to create."
            }
        },
        "storageAccountType": {
            "type": "string",
            "defaultValue": "Standard_LRS",
            "allowedValues": [
                "Standard_LRS",
                "Standard_GRS",
                "Standard_ZRS",
                "Premium_LRS"
            ],
            "metadata": {
                "description": "Storage Account type"
            }
        }
    },
    "variables": {
        "functionAppName": "[parameters('appName')]",
        "hostingPlanName": "[parameters('appName')]",
        "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'azfunctions')]"
    },
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "name": "[variables('storageAccountName')]",
            "apiVersion": "2015-06-15",
            "location": "[resourceGroup().location]",
            "properties": {
                "accountType": "[parameters('storageAccountType')]"
            }
        },
        {
            "type": "Microsoft.Web/serverfarms",
            "apiVersion": "2015-04-01",
            "name": "[variables('hostingPlanName')]",
            "location": "[resourceGroup().location]",
            "properties": {
                "name": "[variables('hostingPlanName')]",
                "computeMode": "Dynamic",
                "sku": "Dynamic"
            }
        },
        {
            "apiVersion": "2015-08-01",
            "type": "Microsoft.Web/sites",
            "name": "[variables('functionAppName')]",
            "location": "[resourceGroup().location]",
            "kind": "functionapp",
            "properties": {
                "name": "[variables('functionAppName')]",
                "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]"
            },
            "dependsOn": [
                "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
                "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
            ],
            "resources": [
                {
                    "apiVersion": "2016-03-01",
                    "name": "appsettings",
                    "type": "config",
                    "dependsOn": [
                        "[resourceId('Microsoft.Web/sites', variables('functionAppName'))]",
                        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
                    ],
                    "properties": {
                        "AzureWebJobsStorage": "[concat('DefaultEndpointsProtocol=https;AccountName=',variables('storageAccountName'),';AccountKey=',listkeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2015-05-01-preview').key1,';')]",
                        "AzureWebJobsDashboard": "[concat('DefaultEndpointsProtocol=https;AccountName=',variables('storageAccountName'),';AccountKey=',listkeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2015-05-01-preview').key1,';')]",
                        "FUNCTIONS_EXTENSION_VERSION": "latest"
                    }
                },
                {
                "apiVersion": "2015-08-01",
                "name": "TestFunctionCM",
                "type": "functions",
                "dependsOn": [
                    "[resourceId('Microsoft.Web/sites', variables('functionAppName'))]"
                ],
                "properties": {
                    "config": {
                    "bindings": [
                        {
                        "authLevel": "anonymous",
                        "name": "req",
                        "type": "httpTrigger",
                        "direction": "in"
                        },
                        {
                        "name": "res",
                        "type": "http",
                        "direction": "out"
                        }
                    ]
                  },
                  "files": {
                    "run.csx": "using System.Net;rnrn public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log)rnrn {rnrnreturn req.CreateResponse("Hello from MyFunction", HttpStatusCode.OK);rnrn }"
                  }
                }
              }
            ]
        }
    ]
}

Thank you.

Asked By: xaxxon

||

Answers:

You will probably need the following:

  1. Runtime under appsettings

    "FUNCTIONS_WORKER_RUNTIME": "python"

  2. My template looks bit different but does deploy a python function, here is the resource from the same:

{
  "type": "Microsoft.Web/sites",
  "apiVersion": "2018-11-01",
  "name": "[parameters('name')]",
  "location": "[parameters('location')]",
  "dependsOn": [
    "microsoft.insights/components/mycoolfunction",
    "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
    "[concat('Microsoft.Storage/storageAccounts/', parameters('storageAccountName'))]"
  ],
  "tags": {},
  "kind": "functionapp,linux",
  "properties": {
    "name": "[parameters('name')]",
    "siteConfig": {
      "appSettings": [
        {
          "name": "FUNCTIONS_WORKER_RUNTIME",
          "value": "python"
        },
        {
          "name": "FUNCTIONS_EXTENSION_VERSION",
          "value": "~2"
        },
        {
          "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
          "value": "[reference('microsoft.insights/components/mycoolfunction', '2015-05-01').InstrumentationKey]"
        },
        {
          "name": "AzureWebJobsStorage",
          "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('storageAccountName'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')), '2019-06-01').keys[0].value,';EndpointSuffix=','core.windows.net')]"
        }
      ]
    },
    "serverFarmId": "[concat('/subscriptions/', parameters('subscriptionId'),'/resourcegroups/', parameters('serverFarmResourceGroup'), '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
    "hostingEnvironment": "[parameters('hostingEnvironment')]",
    "clientAffinityEnabled": false
  }
}
Answered By: bit