快速入门:使用 Azure CLI 创建和管理逻辑应用Quickstart: Create and manage logic apps using the Azure CLI

本快速入门演示如何使用 Azure CLI 逻辑应用扩展 (az logic) 创建和管理逻辑应用。This quickstart shows you how to create and manage logic apps by using the Azure CLI Logic Apps extension (az logic). 在命令行中,可以使用逻辑应用工作流定义的 JSON 文件来创建逻辑应用。From the command line, you can create a logic app by using the JSON file for a logic app workflow definition. 然后,可以通过从命令行运行 listshow (get)、updatedelete 等操作来管理逻辑应用。You can then manage your logic app by running operations such as list, show (get), update, and delete from the command line.

警告

Azure CLI 逻辑应用扩展当前是试验性的,未涵盖在客户支持中 。The Azure CLI Logic Apps extension is currently experimental and not covered by customer support. 请谨慎使用此 CLI 扩展,尤其是在生产环境中选择使用扩展时。Use this CLI extension with caution, especially if you choose to use the extension in production environments.

如果你不熟悉逻辑应用,还可以了解如何通过 Azure 门户创建第一个逻辑应用,以及如何在 Visual Studio 中在 Visual Studio Code 中创建第一个逻辑应用。If you're new to Logic Apps, you can also learn how to create your first logic apps through the Azure portal, in Visual Studio, and in Visual Studio Code.

先决条件Prerequisites

先决条件检查Prerequisite check

开始之前验证环境:Validate your environment before you begin:

示例 - 创建资源组Example - create resource group

如果还没有用于逻辑应用的资源组,请使用命令 az group create 创建该组。If you don't already have a resource group for your logic app, create the group with the command az group create. 例如,以下命令在位置 chinanorth 创建名为 testResourceGroup 的资源组。For example, the following command creates a resource group named testResourceGroup in the location chinanorth.

az group create --name testResourceGroup --location chinanorth

已成功创建资源组时,输出会将 provisioningState 显示为 SucceededThe output shows the provisioningState as Succeeded when your resource group is successfully created:

<...>
  "name": "testResourceGroup",
  "properties": {
    "provisioningState": "Succeeded"
  },
<...>

工作流定义Workflow definition

使用 Azure CLI 创建新逻辑应用更新现有逻辑应用之前,需要逻辑应用的工作流定义。Before you create a new logic app or update an existing logic app by using the Azure CLI, you need a workflow definition for your logic app. 在 Azure 门户中,可以通过从“设计器”视图切换到“代码”视图,以 JSON 格式查看逻辑应用的基础工作流定义。In the Azure portal, you can view your logic app's underlying workflow definition in JSON format by switching from Designer view to Code view.

运行命令以创建或更新逻辑应用时,工作流定义会作为必需参数 (--definition) 进行上传。When you run the commands to create or update your logic app, your workflow definition is uploaded as a required parameter (--definition). 必须将工作流定义创建为遵循工作流定义语言架构的 JSON 文件。You must create your workflow definition as a JSON file that follows the Workflow Definition Language schema.

从 CLI 创建逻辑应用Create logic apps from CLI

可以使用命令 az logic workflow create 以及定义的 JSON 文件,从 Azure CLI 创建逻辑应用工作流。You can create a logic app workflow from the Azure CLI using the command az logic workflow create with a JSON file for the definition.

az logic workflow create --definition
                         --location
                         --name
                         --resource-group
                         [--access-control]
                         [--endpoints-configuration]
                         [--integration-account]
                         [--integration-service-environment]
                         [--state {Completed, Deleted, Disabled, Enabled, NotSpecified, Suspended}]
                         [--tags]

命令必须包含以下必需参数Your command must include the following required parameters:

参数Parameter Value 说明Description
工作流定义Workflow definition --definition JSON 文件,包含逻辑应用的工作流定义A JSON file with your logic app's workflow definition.
位置Location --location -l 逻辑应用所在的 Azure 区域。The Azure region in which your logic app is located.
名称Name --name -n 逻辑应用的名称。The name of your logic app. 名称只能包含字母、数字、连字符 (-)、下划线 (_)、括号 (()) 和句点 (.)。The name can contain only letters, numbers, hyphens (-), underscores (_), parentheses (()), and periods (.). 该名称还必须在区域间唯一。The name must also be unique across regions.
资源组名称Resource group name --resource-group -g 要在其中创建逻辑应用的 Azure 资源组The Azure resource group in which you want to create your logic app. 如果还没有用于逻辑应用的资源组,请在开始之前创建一个资源组Create a resource group before you begin if you don't already have one for your logic app.

还可以包含其他可选参数以配置逻辑应用的访问控制、终结点、集成帐户、集成服务环境、状态和资源标记。You can also include additional optional parameters to configure your logic app's access controls, endpoints, integration account, integration service environment, state, and resource tags.

示例 - 创建逻辑应用Example - create logic app

在此示例中,在位置 chinanorth 的资源组 testResourceGroup 中创建了名为 testLogicApp 的工作流。In this example, a workflow named testLogicApp is created in the resource group testResourceGroup in the location chinanorth. JSON 文件 testDefinition.json 包含工作流定义。The JSON file testDefinition.json contains the workflow definition.

az logic workflow create --resource-group "testResourceGroup" --location "chinanorth" --name "testLogicApp" --definition "testDefinition.json"

成功创建工作流后,CLI 会显示新工作流定义的 JSON 代码。When your workflow is successfully created, the CLI shows your new workflow definition's JSON code. 如果工作流创建失败,请参阅可能错误的列表If your workflow creation fails, see the list of possible errors.

从 CLI 更新逻辑应用Update logic apps from CLI

还可以使用命令 az logic workflow create,从 Azure CLI 更新逻辑应用工作流。You can also update a logic app's workflow from the Azure CLI using the command az logic workflow create.

命令必须包含与创建逻辑应用时相同的必需参数Your command must include the same required parameters as when you create a logic app. 还可以添加与创建逻辑应用时相同的可选参数You can also add the same optional parameters as when creating a logic app.

az logic workflow create --definition
                         --location
                         --name
                         --resource-group
                         [--access-control]
                         [--endpoints-configuration]
                         [--integration-account]
                         [--integration-service-environment]
                         [--state {Completed, Deleted, Disabled, Enabled, NotSpecified, Suspended}]
                         [--tags]

示例 - 更新逻辑应用Example - update logic app

在此示例中,会更新在上一节中创建的示例工作流,以使用不同的 JSON 定义文件 newTestDefinition.json,并添加两个具有说明值的资源标记 testTag1testTag2In this example, the sample workflow created in the previous section is updated to use a different JSON definition file, newTestDefinition.json, and add two resource tags, testTag1 and testTag2 with description values.

az logic workflow create --resource-group "testResourceGroup" --location "chinanorth" --name "testLogicApp" --definition "newTestDefinition.json" --tags "testTag1=testTagValue1" "testTag2=testTagValue"

成功更新工作流后,CLI 会显示逻辑应用更新后的工作流定义。When your workflow is successfully updated, the CLI shows your logic app's updated workflow definition. 如果更新失败,请参阅可能错误的列表If your update fails, see the list of possible errors.

从 CLI 删除逻辑应用Delete logic apps from CLI

可以使用命令 az logic workflow delete,从 Azure CLI 删除逻辑应用工作流。You can delete a logic app's workflow from the Azure CLI using the command az logic workflow delete.

命令必须包含以下必需参数Your command must include the following required parameters:

参数Parameter Value 说明Description
名称Name --name -n 逻辑应用的名称。The name of your logic app.
资源组名称Resource group name -resource-group -g 逻辑应用所在的资源组。The resource group in which your logic app is located.

还可以包含一个可选参数以跳过确认提示 --yes -yYou can also include an optional parameter to skip confirmation prompts, --yes -y.

az logic workflow delete --name
                         --resource-group
                         [--yes]

CLI 随后会提示确认删除逻辑应用。The CLI then prompts you to confirm the deletion of your logic app. 可以通过将可选参数 --yes -y 与命令一起使用来跳过确认提示。You can skip the confirmation prompt by using the optional parameter --yes -y with your command.

Are you sure you want to perform this operation? (y/n):

可以通过在 CLI 中列出逻辑应用,或者通过在 Azure 门户中查看逻辑应用,来确认逻辑应用的删除。You can confirm a logic app's deletion by listing your logic apps in the CLI, or by viewing your logic apps in the Azure portal.

示例 - 删除逻辑应用Example - delete logic app

在此示例中,会删除在上一节中创建的示例工作流In this example, the sample workflow created in a previous section is deleted.

az logic workflow delete --resource-group "testResourceGroup" --name "testLogicApp"

在使用 y 响应确认提示之后,会删除逻辑应用。After you respond to the confirmation prompt with y, the logic app is deleted.

注意事项 - 删除逻辑应用Considerations - delete logic app

删除逻辑应用会以下列方式方式影响工作流实例:Deleting a logic app affects workflow instances in the following ways:

  • 逻辑应用服务会可能取消任何正在进行和挂起的运行。The Logic Apps service makes a best effort to cancel any in-progress and pending runs.

    即使使用较大的卷或积压工作 (backlog),大多数运行在完成或开始之前都将被取消。Even with a large volume or backlog, most runs are canceled before they finish or start. 但是,取消过程可能需要一些时间才能完成。However, the cancellation process might take time to complete. 同时,在运行时执行取消过程中,可能会选取某些运行来执行。Meanwhile, some runs might get picked up for execution while the runtime works through the cancellation process.

  • 逻辑应用服务不会创建或运行新的工作流实例。The Logic Apps service doesn't create or run new workflow instances.

  • 如果删除工作流,然后重新创建相同的工作流,则重新创建的工作流不会具有与删除的工作流相同的元数据。If you delete a workflow and then recreate the same workflow, the recreated workflow won't have the same metadata as the deleted workflow. 必须重新保存任何调用删除工作流的工作流。You have to resave any workflow that called the deleted workflow. 这样,调用方就可获取重新创建的工作流的正确信息。That way, the caller gets the correct information for the recreated workflow. 否则,对重新创建的工作流的调用将失败并显示 Unauthorized 错误。Otherwise, calls to the recreated workflow fail with an Unauthorized error. 此行为也适用于在集成帐户中使用项目的工作流和调用 Azure 函数的工作流。This behavior also applies to workflows that use artifacts in integration accounts and workflows that call Azure functions.

在 CLI 中显示逻辑应用Show logic apps in CLI

可以使用命令 az logic workflow show 获取特定逻辑应用工作流。You can get a specific logic app workflow using the command az logic workflow show.

az logic workflow show --name
                       --resource-group

命令必须包含以下必需参数Your command must include the following required parameters

参数Parameter Value 说明Description
名称Name --name -n 逻辑应用的名称。The name of your logic app.
资源组名称Resource group name --resource-group -g 逻辑应用所在的资源组的名称。The name of the resource group in which your logic app is located.

示例 - 获取逻辑应用Example - get logic app

在此示例中,会返回资源组 testResourceGroup 中的逻辑应用 testLogicApp 以及用于调试的完整日志。In this example, the logic app testLogicApp in the resource group testResourceGroup is returned with full logs for debugging.

az logic workflow show --resource-group "testResourceGroup" --name "testLogicApp" --debug

在 CLI 中列出逻辑应用List logic apps in CLI

可以使用命令 az logic workflow list 按订阅列出逻辑应用。You can list your logic apps by subscription using the command az logic workflow list. 此命令返回逻辑应用工作流的 JSON 代码。This command returns the JSON code for your logic apps' workflows.

可以按以下可选参数来筛选结果:You can filter your results by the following optional parameters:

参数Parameter Value 说明Description
资源组名称Resource group name --resource-group -g 要用于筛选结果的资源组的名称。The name of the resource group by which you want to filter your results.
项数Number of items --top 结果中包含的项数。The number of items that are included in your results.
筛选器Filter --filter 要对列表使用的筛选器的类型。The type of filter that you're using on your list. 可以按状态 (State)、触发器 (Trigger) 以及所引用资源的标识符 (ReferencedResourceId) 进行筛选。You can filter by state (State), trigger (Trigger), and the identifier of the referenced resource (ReferencedResourceId).
az logic workflow list [--filter]
                       [--resource-group]
                       [--top]

示例 - 列出逻辑应用Example - list logic apps

在此示例中,资源组 testResourceGroup 中所有启用的工作流都会以 ASCII 表格式返回。In this example, all enabled workflows in the resource group testResourceGroup are returned in an ASCII table format.

az logic workflow list --resource-group "testResourceGroup" --filter "(State eq 'Enabled')" --output "table"

错误Errors

以下错误指示未安装 Azure 逻辑应用 CLI 扩展。The following error indicates that the Azure Logic Apps CLI extension isn't installed. 按照先决条件中的步骤在计算机上安装逻辑应用扩展Follow the steps in the prerequisites to install the Logic Apps extension on your computer.

az: 'logic' is not in the 'az' command group. See 'az --help'. If the command is from an extension, please make sure the corresponding extension is installed. To learn more about extensions, please visit https://docs.azure.cn/cli-cli-extensions-overview

以下错误可能指示用于上传工作流定义的文件路径不正确。The following error might indicate that the file path for uploading your workflow definition is incorrect.

Expecting value: line 1 column 1 (char 0)

全局参数Global parameters

可以将以下可选全局 Azure CLI 参数与 az logic 命令一起使用:You can use the following optional global Azure CLI parameters with your az logic commands:

参数Parameter Value 说明Description
输出格式Output format --output -o 从默认 JSON 更改输出格式Change the output format from the default JSON.
仅显示错误Only show errors --only-show-errors 取消显示警告,仅显示错误。Suppress warnings and only show errors.
“详细”Verbose --verbose 显示详细日志。Show verbose logs.
调试Debug --debug 显示所有调试日志。Shows all debug logs.
帮助消息Help message --help -h 显示帮助对话框。Show help dialog.
查询Query --query 为 JSON 输出设置 JMESPath 查询字符串。Set a JMESPath query string for JSON output.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see the Azure CLI documentation.

可以在 Microsoft 代码示例浏览器中找到其他逻辑应用 CLI 脚本示例。You can find additional Logic Apps CLI script samples in Microsoft's code samples browser.

接下来,可以使用示例脚本和工作流定义通过 Azure CLI 创建示例应用逻辑。Next, you can create an example app logic through the Azure CLI using a sample script and workflow definition.