为 Azure Functions 中的函数应用自动执行资源部署Automate resource deployment for your function app in Azure Functions

可以使用 Azure 资源管理器模板来部署函数应用。You can use an Azure Resource Manager template to deploy a function app. 本文概述了完成此操作所需的资源和参数。This article outlines the required resources and parameters for doing so. 可能还需要部署其他资源,具体取决于函数应用中的触发器和绑定You might need to deploy additional resources, depending on the triggers and bindings in your function app.

有关创建模板的详细信息,请参阅创作 Azure 资源管理器模板For more information about creating templates, see Authoring Azure Resource Manager templates.

有关示例模板,请参阅:For sample templates, see:

所需资源Required resources

函数应用需要以下资源:A function app requires these resources:

  • Azure 存储帐户An Azure Storage account
  • 托管计划(消耗计划或应用服务计划)A hosting plan (Consumption plan or App Service plan)
  • 函数应用A function app

有关这些资源的 JSON 语法和属性,请参阅:For JSON syntax and properties for these resources, see:

存储帐户Storage account

函数应用需要 Azure 存储帐户。An Azure storage account is required for a function app. 你需要一个支持 blob、表、队列和文件的常规用途帐户。You need a general purpose account that supports blobs, tables, queues, and files. 有关详细信息,请参阅 Azure Functions 存储帐户要求For more information, see Azure Functions storage account requirements.

{
    "type": "Microsoft.Storage/storageAccounts",
    "name": "[variables('storageAccountName')]",
    "apiVersion": "2015-06-15",
    "location": "[resourceGroup().location]",
    "properties": {
        "accountType": "[parameters('storageAccountType')]"
    }
}

此外,在站点配置中,必须将属性 AzureWebJobsStorage 指定为应用设置。In addition, the property AzureWebJobsStorage must be specified as an app setting in the site configuration. 函数应用应将 AzureWebJobsDashboard 指定为应用设置。The function app should specify AzureWebJobsDashboard as an app setting.

Azure Functions 运行时使用 AzureWebJobsStorage 连接字符串创建内部队列。The Azure Functions runtime uses the AzureWebJobsStorage connection string to create internal queues. 运行时使用 AzureWebJobsDashboard 连接字符串登录到 Azure 表存储并为为门户中的“监视器”选项卡提供支持。The runtime uses the AzureWebJobsDashboard connection string to log to Azure Table storage and power the Monitor tab in the portal.

这些属性在 siteConfig 对象中的 appSettings 集合中指定:These properties are specified in the appSettings collection in the siteConfig object:

"appSettings": [
    {
        "name": "AzureWebJobsStorage",
        "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';EndpointSuffix=core.chinacloudapi.cn;AccountKey=', listKeys(variables('storageAccountid'),'2015-05-01-preview').key1)]"
    },
    {
        "name": "AzureWebJobsDashboard",
        "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';EndpointSuffix=core.chinacloudapi.cn;AccountKey=', listKeys(variables('storageAccountid'),'2015-05-01-preview').key1)]"
    }

托管计划Hosting plan

托管计划的定义取决于是使用消耗计划还是使用应用服务计划。The definition of the hosting plan varies, depending on whether you use a Consumption or App Service plan. 请参阅基于消耗计划部署函数应用基于应用服务计划部署函数应用See Deploy a function app on the Consumption plan and Deploy a function app on the App Service plan.

函数应用Function app

函数应用资源使用 Microsoft.Web/Site 类型和 functionapp 类型的资源定义:The function app resource is defined by using a resource of type Microsoft.Web/Site and kind functionapp:

{
    "apiVersion": "2015-08-01",
    "type": "Microsoft.Web/sites",
    "name": "[variables('functionAppName')]",
    "location": "[resourceGroup().location]",
    "kind": "functionapp",            
    "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
    ]

基于消耗计划部署函数应用Deploy a function app on the Consumption plan

可以在两种不同的模式下运行函数应用:消耗计划和应用服务计划。You can run a function app in two different modes: the Consumption plan and the App Service plan. 代码运行时,消耗计划会自动分配计算能力,根据处理负载的需要进行扩展,然后在代码停止运行时进行缩减。The Consumption plan automatically allocates compute power when your code is running, scales out as necessary to handle load, and then scales down when code is not running. 因此,无需为空闲的 VM 付费,且无需提前保留容量。So, you don't have to pay for idle VMs, and you don't have to reserve capacity in advance. 若要详细了解托管计划,请参阅 Azure Functions 消耗计划和应用服务计划To learn more about hosting plans, see Azure Functions Consumption and App Service plans.

有关 Azure 资源管理器模板示例,请参阅基于消耗计划的函数应用For a sample Azure Resource Manager template, see Function app on Consumption plan.

创建消耗计划Create a Consumption plan

消耗计划是一种特殊的“serverfarm”资源。A Consumption plan is a special type of "serverfarm" resource. 可以通过为 computeModesku 属性使用 Dynamic 值来指定:You specify it by using the Dynamic value for the computeMode and sku properties:

{
    "type": "Microsoft.Web/serverfarms",
    "apiVersion": "2015-04-01",
    "name": "[variables('hostingPlanName')]",
    "location": "[resourceGroup().location]",
    "properties": {
        "name": "[variables('hostingPlanName')]",
        "computeMode": "Dynamic",
        "sku": "Dynamic"
    }
}

创建函数应用Create a function app

此外,消耗计划还需要站点配置中的两个附加设置:WEBSITE_CONTENTAZUREFILECONNECTIONSTRINGWEBSITE_CONTENTSHAREIn addition, a Consumption plan requires two additional settings in the site configuration: WEBSITE_CONTENTAZUREFILECONNECTIONSTRING and WEBSITE_CONTENTSHARE. 这些属性用于配置存储函数应用代码和配置的存储帐户和文件路径。These properties configure the storage account and file path where the function app code and configuration are stored.

{
    "apiVersion": "2015-08-01",
    "type": "Microsoft.Web/sites",
    "name": "[variables('functionAppName')]",
    "location": "[resourceGroup().location]",
    "kind": "functionapp",            
    "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
    ],
    "properties": {
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
        "siteConfig": {
            "appSettings": [
                {
                    "name": "AzureWebJobsDashboard",
                    "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(variables('storageAccountid'),'2015-05-01-preview').key1)]"
                },
                {
                    "name": "AzureWebJobsStorage",
                    "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(variables('storageAccountid'),'2015-05-01-preview').key1)]"
                },
                {
                    "name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
                    "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(variables('storageAccountid'),'2015-05-01-preview').key1)]"
                },
                {
                    "name": "WEBSITE_CONTENTSHARE",
                    "value": "[toLower(variables('functionAppName'))]"
                },
                {
                    "name": "FUNCTIONS_EXTENSION_VERSION",
                    "value": "~1"
                }
            ]
        }
    }
}

基于应用服务计划部署函数应用Deploy a function app on the App Service plan

在应用服务计划中,函数应用在基本、标准和高级 SKU 中的专用 VM 上运行,类似于 Web 应用。In the App Service plan, your function app runs on dedicated VMs on Basic, Standard, and Premium SKUs, similar to web apps. 如需详细了解如何使用应用服务计划,请参阅 Azure 应用服务计划深入概述For details about how the App Service plan works, see the Azure App Service plans in-depth overview.

有关 Azure 资源管理器模板示例,请参阅基于 Azure 应用服务计划的函数应用For a sample Azure Resource Manager template, see Function app on Azure App Service plan.

创建应用服务计划Create an App Service plan

{
    "type": "Microsoft.Web/serverfarms",
    "apiVersion": "2015-04-01",
    "name": "[variables('hostingPlanName')]",
    "location": "[resourceGroup().location]",
    "properties": {
        "name": "[variables('hostingPlanName')]",
        "sku": "[parameters('sku')]",
        "workerSize": "[parameters('workerSize')]",
        "hostingEnvironment": "",
        "numberOfWorkers": 1
    }
}

创建函数应用Create a function app

选择缩放选项后,便会创建函数应用。After you've selected a scaling option, create a function app. 该应用是一个包含所有函数的容器。The app is the container that holds all your functions.

函数应用有许多可用于部署的子资源,包括应用设置和源代码管理选项。A function app has many child resources that you can use in your deployment, including app settings and source control options. 还可以选择删除 sourcecontrols 子资源,改用另一个部署选项。You also might choose to remove the sourcecontrols child resource, and use a different deployment option instead.

Important

若要使用 Azure 资源管理器成功部署应用程序,了解如何在 Azure 中部署资源尤为重要。To successfully deploy your application by using Azure Resource Manager, it's important to understand how resources are deployed in Azure. 在下面的示例中,通过使用 siteConfig 应用顶级配置。In the following example, top-level configurations are applied by using siteConfig. 请务必在顶级设置这些配置,因为这些配置会将信息传达给 Functions 运行时和部署引擎。It's important to set these configurations at a top level, because they convey information to the Functions runtime and deployment engine. 应用 sourcecontrols/web 子资源前,需要顶级信息。Top-level information is required before the child sourcecontrols/web resource is applied. 虽然可以在子级别 config/appSettings 资源中配置这些设置,但在某些情况下,需要在应用 config/appSettings 之前部署函数应用。Although it's possible to configure these settings in the child-level config/appSettings resource, in some cases your function app must be deployed before config/appSettings is applied. 比如在逻辑应用中使用函数时,函数是另一资源的依赖项。For example, when you are using functions with Logic Apps, your functions are a dependency of another resource.

{
  "apiVersion": "2015-08-01",
  "name": "[parameters('appName')]",
  "type": "Microsoft.Web/sites",
  "kind": "functionapp",
  "location": "[parameters('location')]",
  "dependsOn": [
    "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]",
    "[resourceId('Microsoft.Web/serverfarms', parameters('appName'))]"
  ],
  "properties": {
     "serverFarmId": "[variables('appServicePlanName')]",
     "siteConfig": {
        "alwaysOn": true,
        "appSettings": [
            { "name": "FUNCTIONS_EXTENSION_VERSION", "value": "~1" },
            { "name": "Project", "value": "src" }
        ]
     }
  },
  "resources": [
     {
        "apiVersion": "2015-08-01",
        "name": "appsettings",
        "type": "config",
        "dependsOn": [
          "[resourceId('Microsoft.Web/Sites', parameters('appName'))]",
          "[resourceId('Microsoft.Web/Sites/sourcecontrols', parameters('appName'), 'web')]",
          "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
        ],
        "properties": {
          "AzureWebJobsStorage": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';EndpointSuffix=core.chinacloudapi.cn;AccountKey=', listKeys(variables('storageAccountid'),'2015-05-01-preview').key1)]",
          "AzureWebJobsDashboard": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';EndpointSuffix=core.chinacloudapi.cn;AccountKey=', listKeys(variables('storageAccountid'),'2015-05-01-preview').key1)]"
        }
     },
     {
          "apiVersion": "2015-08-01",
          "name": "web",
          "type": "sourcecontrols",
          "dependsOn": [
            "[resourceId('Microsoft.Web/sites/', parameters('appName'))]"
          ],
          "properties": {
            "RepoUrl": "[parameters('sourceCodeRepositoryURL')]",
            "branch": "[parameters('sourceCodeBranch')]",
            "IsManualIntegration": "[parameters('sourceCodeManualIntegration')]"
          }
     }
  ]
}

Tip

此模板使用 Project 应用设置值,这将设置基本目录,Functions 部署引擎 (Kudu) 在此目录中查找可部署代码。This template uses the Project app settings value, which sets the base directory in which the Functions deployment engine (Kudu) looks for deployable code. 在存储库内,函数位于 src 文件夹的子文件夹中。In our repository, our functions are in a subfolder of the src folder. 因此,在前一个示例中,将应用设置值设置为 srcSo, in the preceding example, we set the app settings value to src. 如果函数位于存储库的根目录中,或者不从源代码管理进行部署,则可删除此应用设置值。If your functions are in the root of your repository, or if you are not deploying from source control, you can remove this app settings value.

部署模板Deploy your template

可以使用以下任意方法部署模板:You can use any of the following ways to deploy your template:

“部署到 Azure”按钮Deploy to Azure button

<url-encoded-path-to-azuredeploy-json> 替换为 GitHub 中 azuredeploy.json 文件的原始路径的 URL 编码版本。Replace <url-encoded-path-to-azuredeploy-json> with a URL-encoded version of the raw path of your azuredeploy.json file in GitHub.

以下示例使用 Markdown:Here is an example that uses markdown:

[![Deploy to Azure](https://azuredeploy.net/deploybutton.png)](https://portal.azure.cn/#create/Microsoft.Template/uri/<url-encoded-path-to-azuredeploy-json>)

以下示例使用 HTML:Here is an example that uses HTML:

<a href="https://portal.azure.cn/#create/Microsoft.Template/uri/<url-encoded-path-to-azuredeploy-json>" target="_blank"><img src="https://azuredeploy.net/deploybutton.png"></a>

后续步骤Next steps

深入了解如何开发和配置 Azure Functions。Learn more about how to develop and configure Azure Functions.