创建 Azure 资源管理器模板以自动化 Azure 逻辑应用的部署Create Azure Resource Manager templates to automate deployment for Azure Logic Apps

为了帮助你自动创建和部署逻辑应用,本文介绍了在为逻辑应用创建 Azure 资源管理器模板时可用的方法。To help you automate creating and deploying your logic app, this article describes the ways that you can create an Azure Resource Manager template for your logic app. 有关包含工作流定义以及用于部署的其他资源的模板的结构和语法概述,请参阅概述:使用 Azure 资源管理器模板将逻辑应用部署自动化For an overview about the structure and syntax for a template that includes your workflow definition and other resources necessary for deployment, see Overview: Automate deployment for logic apps with Azure Resource Manager templates.

Azure 逻辑应用提供一个可以重复使用的预生成逻辑应用 Azure 资源管理器模板,使用它不仅可以创建逻辑应用,还可以定义要对部署使用的资源和参数。Azure Logic Apps provides a prebuilt logic app Azure Resource Manager template that you can reuse, not only for creating logic apps, but also to define the resources and parameters to use for deployment. 可以将此模板用于自己的业务方案,也可以根据需要自定义此模板。You can use this template for your own business scenarios or customize the template to meet your requirements.

重要

确保模板中的连接使用与逻辑应用相同的 Azure 资源组和位置。Make sure that connections in your template use the same Azure resource group and location as your logic app.

有关 Azure 资源管理器模板的详细信息,请参阅以下主题:For more about Azure Resource Manager templates, see these topics:

使用 Visual Studio 创建模板Create templates with Visual Studio

若要创建最适合部署的有效参数化逻辑应用模板,请使用 Visual Studio(免费的社区版或更高版本)以及适用于 Visual Studio 的 Azure 逻辑应用工具。For the easiest way to create valid parameterized logic app templates that are mostly ready for deployment, use Visual Studio (free Community edition or greater) and the Azure Logic Apps Tools for Visual Studio. 然后,可以在 Visual Studio 中创建逻辑应用,或者在 Azure 门户中找到某个现有逻辑应用并将其下载到 Visual StudioYou can then either create your logic app in Visual Studio or find and download an existing logic app from the Azure portal into Visual Studio.

下载逻辑应用后,你将获得一个模板,其中包含逻辑应用和其他资源(例如连接)的定义。By downloading your logic app, you get a template that includes the definitions for your logic app and other resources such as connections. 该模板还会参数化或定义用于部署逻辑应用和其他资源的值的参数。 The template also parameterizes, or defines parameters for, the values used for deploying your logic app and other resources. 可在单独的参数文件中提供这些参数的值。You can provide the values for these parameters in a separate parameters file. 这样,就可以根据部署需求更轻松地更改这些值。That way, you can more easily change these values based on your deployment needs. 有关详细信息,请参阅以下主题:For more information, see these topics:

使用 Azure PowerShell 创建模板Create templates with Azure PowerShell

可以结合 LogicAppTemplate 模块使用 Azure PowerShell 创建资源管理器模板。You can create Resource Manager templates by using Azure PowerShell with the LogicAppTemplate module. 此开源模块首先评估逻辑应用,以及逻辑应用使用的任何连接。This open-source module first evaluates your logic app and any connections that the logic app uses. 然后,该模块生成模板资源以及用于部署的必需参数。The module then generates template resources with the necessary parameters for deployment.

例如,假设某个逻辑应用要从 Azure 服务总线队列接收消息,并将数据上传到 Azure SQL 数据库。For example, suppose you have a logic app that receives a message from an Azure Service Bus queue and uploads data to an Azure SQL database. 该模块会保留所有业务流程逻辑,并参数化 SQL 和服务总线连接字符串,使你能够根据部署需求提供和更改这些值。The module preserves all the orchestration logic and parameterizes the SQL and Service Bus connection strings so that you can provide and change those values based on your deployment needs.

这些示例演示如何使用 Azure 资源管理器模板、Azure DevOps 中的 Azure Pipelines,以及 Azure PowerShell 来创建和部署逻辑应用:These samples show how to create and deploy logic apps by using Azure Resource Manager templates, Azure Pipelines in Azure DevOps, and Azure PowerShell:

安装 PowerShell 模块Install PowerShell modules

  1. 安装 Azure PowerShell(如果尚未安装)。If you haven't already, install Azure PowerShell.

  2. 若要以最简单的方式从 PowerShell 库安装 LogicAppTemplate 模块,请运行以下命令:For the easiest way to install the LogicAppTemplate module from the PowerShell Gallery, run this command:

    PS> Install-Module -Name LogicAppTemplate
    

    若要更新到最新版本,请运行以下命令:To update to the latest version, run this command:

    PS> Update-Module -Name LogicAppTemplate
    

或者,若要手动安装,请遵循 GitHub 中适用于逻辑应用模板创建者的步骤。Or, to install manually, follow the steps in GitHub for Logic App Template Creator.

安装 Azure 资源管理器客户端Install Azure Resource Manager client

若要使 LogicAppTemplate 模块能够使用任何 Azure 租户和订阅访问令牌,请安装 Azure 资源管理器客户端工具,这是一个可以调用 Azure 资源管理器 API 的简单命令行工具。For the LogicAppTemplate module to work with any Azure tenant and subscription access token, install the Azure Resource Manager client tool, which is a simple command line tool that calls the Azure Resource Manager API.

使用此工具运行 Get-LogicAppTemplate 命令时,该命令首先会通过 ARMClient 工具获取访问令牌,通过管道将该令牌传送到 PowerShell 脚本,然后以 JSON 文件的形式创建模板。When you run the Get-LogicAppTemplate command with this tool, the command first gets an access token through the ARMClient tool, pipes the token to the PowerShell script, and creates the template as a JSON file. 有关该工具的详细信息,请参阅这篇有关 Azure 资源管理器客户端工具的文章For more information about the tool, see this article about the Azure Resource Manager client tool.

使用 PowerShell 生成模板Generate template with PowerShell

若要在安装 LogicAppTemplate 模块和 Azure CLI 后生成模板,请运行以下 PowerShell 命令:To generate your template after installing the LogicAppTemplate module and Azure CLI, run this PowerShell command:

PS> Get-LogicAppTemplate -Token (az account get-access-token | ConvertFrom-Json).accessToken -LogicApp <logic-app-name> -ResourceGroup <Azure-resource-group-name> -SubscriptionId $SubscriptionId -Verbose | Out-File C:\template.json

若要根据建议从 Azure 资源管理器客户端工具以管道方式传送令牌,请运行以下命令,其中的 $SubscriptionId 是你的 Azure 订阅 ID:To follow the recommendation for piping in a token from the Azure Resource Manager client tool, run this command instead where $SubscriptionId is your Azure subscription ID:

PS> armclient token $SubscriptionId | Get-LogicAppTemplate -LogicApp <logic-app-name> -ResourceGroup <Azure-resource-group-name> -SubscriptionId $SubscriptionId -Verbose | Out-File C:\template.json

提取后,可运行以下命令基于模板创建参数文件:After extraction, you can then create a parameters file from your template by running this command:

PS> Get-ParameterTemplate -TemplateFile $filename | Out-File '<parameters-file-name>.json'

对于包含 Azure Key Vault 引用的提取内容(仅限静态值),请运行以下命令:For extraction with Azure Key Vault references (static only), run this command:

PS> Get-ParameterTemplate -TemplateFile $filename -KeyVault Static | Out-File $fileNameParameter
parametersParameters 必须Required 说明Description
TemplateFileTemplateFile Yes 模板文件的文件路径The file path to your template file
KeyVaultKeyVault No 一个枚举,描述如何处理可能的 Key Vault 值。An enum that describes how to handle possible key vault values. 默认为 NoneThe default is None.

后续步骤Next steps