您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:通过 Azure 事件网格和逻辑应用监视虚拟机更改Tutorial: Monitor virtual machine changes with Azure Event Grid and Logic Apps

你可以在 Azure 资源或第三方资源中发生特定事件时,启动自动化逻辑应用工作流You can start an automated logic app workflow when specific events happen in Azure resources or third-party resources. 这些资源可以将这些事件发布到 Azure 事件网格These resources can publish those events to an Azure event grid. 然后,事件网格会将这些事件推送给具有队列、webhook 或事件中心作为终结点的订阅者。In turn, the event grid pushes those events to subscribers that have queues, webhooks, or event hubs as endpoints. 作为订阅者,逻辑应用可以在运行自动化工作流以执行任务之前等待这些来自事件网格的事件 - 而无需编写任何代码。As a subscriber, your logic app can wait for those events from the event grid before running automated workflows to perform tasks - without you writing any code.

例如,下面是发布者可以将通过 Azure 事件网格服务发送给订阅者的某些事件:For example, here are some events that publishers can send to subscribers through the Azure Event Grid service:

  • 创建、读取、更新或删除资源。Create, read, update, or delete a resource. 例如,你可以监视可能在 Azure 订阅中产生费用并影响你账单的更改。For example, you can monitor changes that might incur charges on your Azure subscription and affect your bill.
  • 从 Azure 订阅添加或删除某个人。Add or remove a person from an Azure subscription.
  • 你的应用可执行特定的操作。Your app performs a particular action.
  • 队列中显示新消息。A new message appears in a queue.

本教程将创建一个逻辑应用,监视对虚拟机的更改并就这些更改发送电子邮件。This tutorial creates a logic app that monitors changes to a virtual machine and sends emails about those changes. 当你为 Azure 资源创建具有事件订阅的逻辑应用时,事件会通过事件网格从该资源流向逻辑应用。When you create a logic app with an event subscription for an Azure resource, events flow from that resource through an event grid to the logic app. 本教程将指导你完成构建此逻辑应用:The tutorial walks you through building this logic app:

概述 - 通过事件网格和逻辑应用监视虚拟机

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 创建通过事件网格监视事件的逻辑应用。Create a logic app that monitors events from an event grid.
  • 添加一个专门检查虚拟机更改的条件。Add a condition that specifically checks for virtual machine changes.
  • 虚拟机更改时发送电子邮件。Send email when your virtual machine changes.

先决条件Prerequisites

创建通过事件网格监视事件的逻辑应用Create a logic app that monitors events from an event grid

首先,创建逻辑应用并添加事件网格触发器,以便监视虚拟机的资源组。First, create a logic app and add an Event grid trigger that monitors the resource group for your virtual machine.

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 在 Azure 主菜单的左上角,选择“创建资源” > “企业集成” > “逻辑应用”。From the upper left corner of the main Azure menu, choose Create a resource > Enterprise Integration > Logic App.

    创建逻辑应用

  3. 使用下表中指定的设置创建逻辑应用:Create your logic app with the settings specified in the following table:

    提供逻辑应用的详细信息

    设置Setting 建议的值Suggested value 说明Description
    名称Name {your-logic-app-name}{your-logic-app-name} 提供一个唯一的逻辑应用名称。Provide a unique logic app name.
    订阅Subscription {your-Azure-subscription}{your-Azure-subscription} 在本教程中,选择同一 Azure 订阅用于所有服务。Select the same Azure subscription for all services in this tutorial.
    资源组Resource group {your-Azure-resource-group}{your-Azure-resource-group} 在本教程中,选择同一 Azure 资源组用于所有服务。Select the same Azure resource group for all services in this tutorial.
    位置Location {your-Azure-region}{your-Azure-region} 在本教程中,选择同一区域用于所有服务。Select the same region for all services in this tutorial.
  4. 准备就绪后,请选择“固定到仪表板”,并选择“创建”。When you're ready, select Pin to dashboard, and choose Create.

    你现在已为逻辑应用程序创建 Azure 资源。You've now created an Azure resource for your logic app. 在 Azure 部署逻辑应用后,逻辑应用设计器会显示针对常用模式的模板,以便你可以更快地入门。After Azure deploys your logic app, the Logic Apps Designer shows you templates for common patterns so you can get started faster.

    备注

    选择“固定到仪表板”时,逻辑应用会在逻辑应用设计器中自动打开。When you select Pin to dashboard, your logic app automatically opens in Logic Apps Designer. 否则,你可以手动查找和打开逻辑应用。Otherwise, you can manually find and open your logic app.

  5. 接着,选择逻辑应用模板。Now choose a logic app template. 在“模板”下,选择“空白逻辑应用”,以便可以从头开始构建逻辑应用。Under Templates, choose Blank Logic App so you can build your logic app from scratch.

    选择逻辑应用模板

    逻辑应用设计器现显示连接器触发器,可用来启动逻辑应用,以及在触发器之后添加执行任务的操作。The Logic Apps Designer now shows you connectors and triggers that you can use to start your logic app, and also actions that you can add after a trigger to perform tasks. 触发器是创建逻辑应用实例并开始逻辑应用工作流的事件。A trigger is an event that creates a logic app instance and starts your logic app workflow. 逻辑应用需要触发器作为第一项。Your logic app needs a trigger as the first item.

  6. 在搜索框中,输入“事件网格”作为筛选器。In the search box, enter "event grid" as your filter. 选择此触发器:“Azure 事件网格 - 在资源事件上”Select this trigger: Azure Event Grid - On a resource event

    选择此触发器:“Azure 事件网格 - 在资源事件上”

  7. 出现提示时,请使用 Azure 凭据登录到 Azure 事件网格。When prompted, sign in to Azure Event Grid with your Azure credentials.

    使用 Azure 凭据登录

    备注

    如果使用个人 Microsoft 帐户登录,如 @outlook.com 或 @hotmail.com,事件网格触发器可能不会正确显示。If you're signed in with a personal Microsoft account, such as @outlook.com or @hotmail.com, the Event Grid trigger might not appear correctly. 作为一种解决方法,选择与服务主体连接,或作为与 Azure 订阅关联的 Azure Active Directory 成员进行身份验证,例如,user-name@emailoutlook.onmicrosoft.com。As a workaround, choose Connect with Service Principal, or authenticate as a member of the Azure Active Directory that's associated with your Azure subscription, for example, user-name@emailoutlook.onmicrosoft.com.

  8. 现在订阅发布者事件的逻辑应用。Now subscribe your logic app to publisher events. 提供你在下表中指定的事件订阅的详细信息:Provide the details for your event subscription as specified in the following table:

    提供事件订阅的详细信息

    设置Setting 建议的值Suggested value DescriptionDescription
    订阅Subscription {virtual-machine-Azure-subscription}{virtual-machine-Azure-subscription} 选择事件发布者的 Azure 订阅。Select the event publisher's Azure subscription. 对于本教程,请选择用于虚拟机的 Azure 订阅。For this tutorial, select the Azure subscription for your virtual machine.
    资源类型Resource Type Microsoft.Resources.resourceGroupsMicrosoft.Resources.resourceGroups 选择事件发布者的资源类型。Select the event publisher's resource type. 对于本教程,请选择相应的指定值,以便逻辑应用仅监视资源组。For this tutorial, select the specified value so your logic app monitors only resource groups.
    资源名称Resource Name {virtual-machine-resource-group-name}{virtual-machine-resource-group-name} 选择发布者的资源名称。Select the publisher's resource name. 对于本教程,请选择适用于你的虚拟机的资源组名称。For this tutorial, select the name of the resource group for your virtual machine.
    对于可选设置,请选择“显示高级选项”。For optional settings, choose Show advanced options. {see descriptions}{see descriptions} * 前缀筛选器:对于本教程,请将此设置留空。* Prefix Filter: For this tutorial, leave this setting empty. 默认行为与所有值匹配。The default behavior matches all values. 但是,你可以指定一个前缀字符串作为筛选器,例如,特定资源的路径和参数。However, you can specify a prefix string as a filter, for example, a path and a parameter for a specific resource.

    * 后缀筛选器:对于本教程,请将此设置留空。* Suffix Filter: For this tutorial, leave this setting empty. 默认行为与所有值匹配。The default behavior matches all values. 但是,当你仅需要特定文件类型,可以指定一个后缀字符串作为筛选器,例如,文件扩展名。However, you can specify a suffix string as a filter, for example, a file name extension, when you want only specific file types.

    * 订阅名称:提供事件订阅的唯一名称。* Subscription Name: Provide a unique name for your event subscription.

    完成时,事件网格触发器可能如以下示例所示:When you're done, your event grid trigger might look like this example:

    示例事件网格触发器详细信息

  9. 保存逻辑应用。Save your logic app. 在设计器工具栏上,选择“保存”。On the designer toolbar, choose Save. 若要折叠和隐藏逻辑应用中操作的详细信息,请选择操作的标题栏。To collapse and hide an action's details in your logic app, choose the action's title bar.

    保存逻辑应用

    当你使用事件网格触发器保存逻辑应用时,Azure 将自动为选定资源的逻辑应用创建事件订阅。When you save your logic app with an event grid trigger, Azure automatically creates an event subscription for your logic app to your selected resource. 因此,当资源将事件发布到事件网格时,该事件网格会自动将事件推送到逻辑应用。So when the resource publishes an event to the event grid, that event grid automatically pushes the event to your logic app. 此事件触发逻辑应用,然后将创建并运行你在这些后续步骤中定义的工作流实例。This event triggers your logic app, then creates and runs an instance of the workflow that you define in these next steps.

逻辑应用现已发布,并侦听事件网格中的事件,但在将操作添加到工作流之前它不会执行任何操作。Your logic app is now live and listens to events from the event grid, but doesn't do anything until you add actions to the workflow.

添加一个条件以检查虚拟机更改Add a condition that checks for virtual machine changes

若要仅在特定事件发生时运行逻辑应用工作流,请添加检查虚拟机“写入”操作的条件。To run your logic app workflow only when a specific event happens, add a condition that checks for virtual machine "write" operations. 如果此条件为 true,你的逻辑应用会向你发送包含已更新虚拟机详细信息的电子邮件。When this condition is true, your logic app sends you email with details about the updated virtual machine.

  1. 在“逻辑应用设计器”的事件网格触发器下,选择“新步骤” > “添加条件”。In Logic App Designer, under the event grid trigger, choose New step > Add a condition.

    将条件添加到逻辑应用

    逻辑应用设计器将在工作流中添加一个空条件,包括要遵循的操作路径,具体要取决于条件为 true 还是 false。The Logic App Designer adds an empty condition to your workflow, including action paths to follow based whether the condition is true or false.

    空条件

  2. 在“条件”框中,选择“在高级模式下编辑”。In the Condition box, choose Edit in advanced mode. 输入此表达式:Enter this expression:

    @equals(triggerBody()?['data']['operationName'], 'Microsoft.Compute/virtualMachines/write')

    现在,条件应如下例所示:Your condition now looks like this example:

    空条件

    此表达式将检查事件 bodydata 对象,其中 operationName 属性是 Microsoft.Compute/virtualMachines/write 操作。This expression checks the event body for a data object where the operationName property is the Microsoft.Compute/virtualMachines/write operation. 详细了解事件网格事件架构Learn more about Event Grid event schema.

  3. 若要提供条件说明,请选择条件形状上的“省略号”(“...”) 按钮,然后选择“重命名”。To provide a description for the condition, choose the ellipses (...) button on the condition shape, then choose Rename.

    备注

    本教程后面的示例还提供了逻辑应用工作流中的步骤说明。The later examples in this tutorial also provide descriptions for steps in the logic app workflow.

  4. 现在选择“在基本模式下编辑”,以便表达式自动解析,如下所示:Now choose Edit in basic mode so that the expression automatically resolves as shown:

    逻辑应用条件

  5. 保存逻辑应用。Save your logic app.

虚拟机更改时发送电子邮件Send email when your virtual machine changes

现在添加操作,以便在指定条件为 true 时收到电子邮件。Now add an action so that you get an email when the specified condition is true.

  1. 在该条件的“如果为 true”框中,选择“添加操作”。In the condition's If true box, choose Add an action.

    在条件为 true 时添加操作

  2. 在搜索框中,输入“电子邮件”作为筛选器。In the search box, enter "email" as your filter. 根据你的电子邮件提供程序,找到并选择匹配的连接器。Based on your email provider, find and select the matching connector. 然后选择连接器的“发送电子邮件”操作。Then select the "send email" action for your connector. 例如:For example:

    • 对于 Azure 工作或学校帐户,请选择 Office 365 Outlook 连接器。For an Azure work or school account, select the Office 365 Outlook connector.
    • 对于个人 Microsoft 帐户,请选择 Outlook.com 连接器。For personal Microsoft accounts, select the Outlook.com connector.
    • 对于 Gmail 帐户,则选择 Gmail 连接器。For Gmail accounts, select the Gmail connector.

    我们将继续使用 Office 365 Outlook 连接器。We're going to continue with the Office 365 Outlook connector. 如果你使用不同的提供程序,步骤保持不变,但你的 UI 显示可能会有所不同。If you use a different provider, the steps remain the same, but your UI might appear different.

    选择“发送电子邮件”操作

  3. 如果你还没有与你的电子邮件提供程序建立连接,则在系统要求你进行身份验证时登录到电子邮件帐户。If you don't already have a connection for your email provider, sign in to your email account when you're asked for authentication.

  4. 提供下表中指定的电子邮件的详细信息:Provide details for the email as specified in the following table:

    空的电子邮件操作

    提示

    若要选择工作流中可用的字段,请单击“编辑”框,以便打开“动态内容”列表,或选择“添加动态内容”。To select from fields available in your workflow, click in an edit box so that the Dynamic content list opens, or choose Add dynamic content. 对于多个字段,请选择列表中每个部分的“查看更多”。For more fields, choose See more for each section in the list. 若要关闭“动态内容”列表,请选择“添加动态内容”。To close the Dynamic content list, choose Add dynamic content.

    设置Setting 建议的值Suggested value 说明Description
    收件人To {recipient-email-address}{recipient-email-address} 输入收件人的电子邮件地址。Enter the recipient's email address. 为进行测试,可以使用自己的电子邮件地址。For testing purposes, you can use your own email address.
    主题Subject 更新资源:主题Resource updated: Subject 输入电子邮件的主题内容。Enter the content for the email's subject. 对于本教程,请输入建议的文本并选择该事件的“主题”字段。For this tutorial, enter the suggested text and select the event's Subject field. 此处,电子邮件主题包含更新资源(虚拟机)的名称。Here, your email subject includes the name for the updated resource (virtual machine).
    正文Body 资源组:主题Resource group: Topic

    事件类型:事件类型Event type: Event Type

    事件 ID:IDEvent ID: ID

    时间:事件时间Time: Event Time

    输入电子邮件的正文内容。Enter the content for the email's body. 对于本课程,请输入建议的文本并选择事件的“主题”、“事件类型”和“ID”和“事件时间”字段,以便电子邮件包括资源组名称、事件类型、事件时间戳和用于更新的事件 ID。For this tutorial, enter the suggested text and select the event's Topic, Event Type, ID, and Event Time fields so that your email includes the resource group name, event type, event timestamp, and event ID for the update.

    若要在内容中添加空行,请按 Shift + Enter。To add blank lines in your content, press Shift + Enter.

    备注

    如果你选择表示数组的字段,设计器会围绕引用数组的操作自动添加“For each”循环。If you select a field that represents an array, the designer automatically adds a For each loop around the action that references the array. 这样一来,逻辑应用会对每个数组项执行该操作。That way, your logic app performs that action on each array item.

    现在,电子邮件操作可能如下例所示:Now, your email action might look like this example:

    选择要包含在电子邮件中的输出

    而完成的逻辑应用可能如下例所示:And your finished logic app might look like this example:

    完成的逻辑应用

  5. 保存逻辑应用。Save your logic app. 若要折叠和隐藏逻辑应用中每个操作的详细信息,请选择操作的标题栏。To collapse and hide each action's details in your logic app, choose the action's title bar.

    保存逻辑应用

    逻辑应用现已发布,但在执行任何操作之前会等待对虚拟机的更改。Your logic app is now live, but waits for changes to your virtual machine before doing anything. 若要现在测试逻辑应用,请继续学习下一节。To test your logic app now, continue to the next section.

测试逻辑应用工作流Test your logic app workflow

  1. 若要检查逻辑应用是否将获取指定事件,请更新你的虚拟机。To check that your logic app is getting the specified events, update your virtual machine.

    例如,你可以在 Azure 门户中或使用 Azure PowerShell 重设你的虚拟机大小For example, you can resize your virtual machine in the Azure portal or resize your VM with Azure PowerShell.

    几分钟后,你应会收到一封电子邮件。After a few moments, you should get an email. 例如:For example:

    有关虚拟机更新的电子邮件

  2. 若要查看逻辑应用的运行与触发历史记录,请在逻辑应用菜单中选择“概述”。To review the runs and trigger history for your logic app, on your logic app menu, choose Overview. 若要查看有关运行的更多详细信息,请选择运行所在的行。To view more details about a run, choose the row for that run.

    逻辑应用运行历史记录

  3. 若要查看每个步骤的输入和输出,请展开要查看的步骤。To view the inputs and outputs for each step, expand the step that you want to review. 此信息可以帮助你诊断和调试逻辑应用中的问题。This information can help you diagnose and debug problems in your logic app.

    逻辑应用运行历史记录详细信息

祝贺你,你已经创建并运行逻辑应用,它将通过事件网格监视资源事件,并在这些事件发生时向你发送电子邮件。Congratulations, you've created and run a logic app that monitors resource events through an event grid and emails you when those events happen. 此外,还学习了如何轻松创建工作流,以便将流程自动化,并将系统和云服务相集成。You also learned how easily you can create workflows that automate processes and integrate systems and cloud services.

可使用事件网格和逻辑应用监视其他配置更改,例如:You can monitor other configuration changes with event grids and logic apps, for example:

  • 虚拟机获取基于角色的访问控制 (RBAC) 权限。A virtual machine gets role-based access control (RBAC) rights.
  • 对网络接口 (NIC) 上的网络安全组 (NSG) 进行的更改。Changes are made to a network security group (NSG) on a network interface (NIC).
  • 添加或删除虚拟机磁盘。Disks for a virtual machine are added or removed.
  • 公共 IP 地址被分配给虚拟机 NIC。A public IP address is assigned to a virtual machine NIC.

清理资源Clean up resources

本教程使用的资源和执行的操作将会在你的 Azure 订阅上产生费用。This tutorial uses resources and performs actions that incur charges on your Azure subscription. 因此,完成本教程和测试后,请确保禁用或删除你不希望产生费用的任何资源。So when you're done with the tutorial and testing, make sure that you disable or delete any resources where you don't want to incur charges.

  • 若要在不删除所做工作的情况下停止运行逻辑应用,请禁用应用。To stop running your logic app without deleting your work, disable your app. 在逻辑应用菜单上,选择“概述”。On your logic app menu, choose Overview. 在工具栏上,选择“禁用”。On the toolbar, choose Disable.

    关闭逻辑应用

    提示

    如果看不到逻辑应用菜单,请尝试返回到 Azure 仪表板,然后重新打开逻辑应用。If you don't see the logic app menu, try returning to the Azure dashboard, and reopen your logic app.

  • 若要永久删除逻辑应用,请在逻辑应用菜单上选择“概览”。To permanently delete your logic app, on the logic app menu, choose Overview. 在工具栏上选择“删除”。On the toolbar, choose Delete. 确认要删除逻辑应用,然后选择“删除”。Confirm that you want to delete your logic app, then choose Delete.

后续步骤Next steps