您如何使用Arm模板部署python azure函数?

时间:2019-11-19 07:23:54

标签: python azure azure-functions arm-template

以下内容部署了运行指定C#的azure函数。对于应该运行python的函数我该怎么做?

我尝试仅将名称更改为__init__.py,就像您将azure-function-core-tools func命令与--python开关一起使用时生成的一样,但是甚至找不到关于为什么事情无法正常工作的错误消息。

{
    "$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;\r\n\r\n public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log)\r\n\r\n {\r\n\r\nreturn req.CreateResponse(\"Hello from MyFunction\", HttpStatusCode.OK);\r\n\r\n }"
                  }
                }
              }
            ]
        }
    ]
}

谢谢。

1 个答案:

答案 0 :(得分:1)

您可能需要以下条件:

  1. 应用设置下的运行时

    "FUNCTIONS_WORKER_RUNTIME": "python"

  2. 我的模板看起来有些不同,但是确实部署了python函数,以下是同一资源:

{ "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 } }