您现在访问的是微软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

  • Azure 订阅。An Azure subscription. 如果没有 Azure 订阅,请注册一个免费 Azure 帐户If you don't have an Azure subscription, sign up for a free Azure account.

  • 来自逻辑应用支持的电子邮件提供程序且用于发送通知的电子邮件帐户,例如 Office 365 Outlook、Outlook.com 或 Gmail。An email account from an email provider supported by Logic Apps for sending notifications, such as Office 365 Outlook, Outlook.com, or Gmail. 至于其他提供商,请查看此处的连接器列表For other providers, review the connectors list here.

    本教程将使用 Office 365 Outlook 帐户。This tutorial uses an Office 365 Outlook account. 如果使用其他电子邮件帐户,则常规步骤保持不变,但 UI 显示可能稍有不同。If you use a different email account, the general steps stay the same, but your UI might appear slightly different.

  • 虚拟机A virtual machine. 如你尚未这样做,请通过创建 VM 教程创建虚拟机。If you haven't already done so, create a virtual machine through the Create a VM tutorial. 若要使虚拟机发布事件,你无需执行任何其他操作To make the virtual machine publish events, you don't need to do anything else.

创建空白逻辑应用Create blank logic app

  1. 使用 Azure 帐户凭据登录到 Azure 门户Sign in to the Azure portal with your Azure account credentials.

  2. 在 Azure 主菜单中,依次选择“创建资源” > “集成” > “逻辑应用”。 From the main Azure menu, select Create a resource > Integration > Logic App.

    创建逻辑应用

  3. 在“逻辑应用”下,提供有关逻辑应用的信息 。Under Logic App, provide information about your logic app. 完成后,选择“创建” 。When you're done, choose Create.

    提供逻辑应用的详细信息

    属性Property 建议的值Suggested value 说明Description
    名称Name <logic-app-name><logic-app-name> 提供逻辑应用的唯一名称。Provide a unique name for your logic app.
    订阅Subscription <Azure-subscription-name><Azure-subscription-name> 在本教程中,选择同一 Azure 订阅用于所有服务。Select the same Azure subscription for all services in this tutorial.
    资源组Resource group <Azure-resource-group> 在本教程中,选择同一 Azure 资源组用于所有服务。Select the same Azure resource group for all services in this tutorial.
    位置Location <Azure-datacenter-region><Azure-datacenter-region> 在本教程中,选择同一区域用于所有服务。Select the same region for all services in this tutorial.

    你现在已为逻辑应用程序创建 Azure 资源。You've now created an Azure resource for your logic app.

  4. 在 Azure 部署逻辑应用以后,逻辑应用设计器会显示一个包含简介视频和常用触发器的页面。After Azure deploys your logic app, the Logic Apps Designer shows a page with an introduction video and commonly used triggers. 滚动浏览视频和触发器。Scroll past the video and triggers.

  5. 在“模板”下选择“空白逻辑应用”。Under Templates, choose Blank Logic App.

    选择逻辑应用模板

    逻辑应用设计器现在显示可用于启动逻辑应用的触发器The Logic Apps Designer now shows you triggers that you can use to start your logic app. 每个逻辑应用都必须从触发器开始,该触发器在发生特定事件或特定条件得到满足的情况下触发。Every logic app must start with a trigger, which fires when a specific event happens or when a specific condition is met. 每当触发器触发时,Azure 逻辑应用都会创建一个运行逻辑应用的工作流实例。Each time the trigger fires, Azure Logic Apps creates a workflow instance that runs your logic app.

添加事件网格触发器Add Event Grid trigger

现在,请添加事件网格触发器,以便监视虚拟机的资源组。Now, add the Event Grid trigger that monitors the resource group for your virtual machine.

  1. 在设计器的搜索框中,输入“事件网格”作为筛选条件。On the designer, in the search box, enter "event grid" as your filter. 从触发器列表中选择此触发器:发生资源事件时 - Azure 事件网格From the triggers list, select this trigger: When a resource event occurs - Azure Event Grid

    选择此触发器:“发生资源事件时”

  2. 出现提示时,请使用 Azure 帐户凭据登录到 Azure 事件网格。When prompted, sign in to Azure Event Grid with your Azure account credentials. 在“租户”列表(其中显示与 Azure 订阅关联的 Azure Active Directory 租户)中,检查是否显示了正确的租户 。In the Tenant list, which shows the Azure Active Directory tenant that's associated with your Azure subscription, check that the correct tenant appears.

    使用 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.

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

    提供事件订阅的详细信息

    属性Property 必选Required ValueValue 说明Description
    订阅Subscription Yes <event-publisher-Azure-subscription-name><event-publisher-Azure-subscription-name> 选择与事件发布者关联的 Azure 订阅名称。Select the name for the Azure subscription associated with the event publisher. 对于本教程,请选择用于虚拟机的 Azure 订阅名称。For this tutorial, select the Azure subscription name for your virtual machine.
    资源类型Resource Type Yes <event-publisher-Azure-resource-type><event-publisher-Azure-resource-type> 选择事件发布者的 Azure 资源类型。Select the Azure resource type for the event publisher. 对于本教程,请选择下方的值以监视 Azure 资源组:For this tutorial, select this value to monitor Azure resource groups:

    Microsoft.Resources.ResourceGroupsMicrosoft.Resources.ResourceGroups

    资源名称 Resource Name Yes <event-publisher-Azure-resource-name><event-publisher-Azure-resource-name> 选择事件发布者的 Azure 资源名称。Select the name for the Azure resource for the event publisher. 此列表依据所选择的资源类型而异。This list varies based on the resource type that you selected. 对于本教程,请选择适用于你的虚拟机的 Azure 资源组名称。For this tutorial, select the name for the Azure resource group for your virtual machine.
    事件类型项Event Type Item No <event-types><event-types> 选择一个或多个特定事件类型以筛选并发送到事件网格。Select one or more specific event types to filter and send to your event grid. 例如,可选择添加这些事件类型以检测何时更改或删除了资源:For example, you can optionally add these event types to detect when resources are changed or deleted:

    - Microsoft.Resources.ResourceActionSuccess- Microsoft.Resources.ResourceActionSuccess
    - Microsoft.Resources.ResourceDeleteSuccess- Microsoft.Resources.ResourceDeleteSuccess
    - Microsoft.Resources.ResourceWriteSuccess- Microsoft.Resources.ResourceWriteSuccess

    有关详细信息,请参阅以下主题:For more information, see these topics:

    - 了解事件筛选- Understand event filtering
    - 筛选事件网格的事件- Filter events for Event Grid
    - 资源组的 Azure 事件网格事件架构- Azure Event Grid event schema for resource groups

    订阅名称Subscription Name No <event-subscription-name><event-subscription-name> 提供事件订阅的唯一名称。Provide a unique name for your event subscription.
    对于可选设置,请选择“添加新参数” 。For optional settings, choose Add new parameter. No {参阅说明}{see descriptions} * 前缀筛选器:对于本教程,请将此属性留空。* Prefix Filter: For this tutorial, leave this property 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 property 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.

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

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

  4. 保存逻辑应用。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 condition

若要仅在特定事件发生时运行逻辑应用工作流,请添加检查虚拟机“写入”操作的条件。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.

    选择“新步骤”

  2. 在搜索框中,输入“条件”作为筛选器。In the search box, enter "condition" as your filter. 在操作列表中选择此操作:条件From the actions list, select this action: 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.

    空条件

  3. 将条件标题重命名为 If a virtual machine in your resource group has changedRename the condition title to If a virtual machine in your resource group has changed. 在条件的标题栏中,选择省略号 (...) 按钮,然后选择“重命名” 。On the condition's title bar, choose the ellipses (...) button, and select Rename.

    重命名条件

  4. 创建一个条件,用于检查事件 bodydata 对象中的 operationName 属性是否等于 Microsoft.Compute/virtualMachines/write 操作。Create a condition that checks the event body for a data object where the operationName property is equal to the Microsoft.Compute/virtualMachines/write operation. 详细了解事件网格事件架构Learn more about Event Grid event schema.

    1. 在“和”下面的第一行中,单击左侧框的内部。 On the first row under And, click inside the left box. 在显示的“动态内容”中,选择“表达式” 。In the dynamic content list that appears, choose Expression.

      选择"表达式"

    2. 在表达式编辑器中,输入此表达式,然后选择“确定” :In the expression editor, enter this expression, and choose OK:

      triggerBody()?['data']['operationName']

      例如:For example:

      选择"表达式"

    3. 在中间框中,保留运算符“等于”。 In the middle box, keep the operator is equal to.

    4. 在右侧框中,输入此值:In the right box, enter this value:

      Microsoft.Compute/virtualMachines/write

    现在,已完成的条件应如下例所示:Your finished condition now looks like this example:

    已完成的条件

    如果从设计视图切换到代码视图并返回到设计视图,则在条件中指定的表达式将解析为 data.operationName 标记: If you switch from design view to code view and back to design view, the expression that you specified in the condition resolves to the data.operationName token:

    已解析的条件

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

发送电子邮件通知Send email notifications

现在添加操作 ,以便在指定条件为 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 "send an 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 连接器。This tutorial continues with the Office 365 Outlook connector. 如果使用其他提供程序,步骤将保持不变,但 UI 显示可能会略有不同。If you use a different provider, the steps remain the same, but your UI might appear slightly 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. 将发送电子邮件标题重命名为此标题:Send email when virtual machine updatedRename the send email title to this title: Send email when virtual machine updated

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

    空的电子邮件操作

    提示

    若要从工作流中之前步骤的结果中进行选择,请单击编辑框以显示动态内容列表,或选择“添加动态内容” 。To select from results from previous steps in your workflow, click in an edit box so that the dynamic content list appears, or choose Add dynamic content. 对于多个结果,请选择列表中每个部分的“查看更多” 。For more results, choose See more for each section in the list. 若要关闭“动态内容”列表,请再次选择“添加动态内容” 。To close the dynamic content list, choose Add dynamic content again.

    属性Property 必选Required ValueValue 说明Description
    收件人To Yes <接收方@域><recipient@domain> 输入收件人的电子邮件地址。Enter the recipient's email address. 为进行测试,可以使用自己的电子邮件地址。For testing purposes, you can use your own email address.
    主题Subject Yes 更新资源:主题Resource updated: Subject 输入电子邮件的主题内容。Enter the content for the email's subject. 对于本教程,请输入指定的文本并选择该事件的“主题”字段 。For this tutorial, enter the specified text, and select the event's Subject field. 此处,电子邮件主题包含更新资源(虚拟机)的名称。Here, your email subject includes the name for the updated resource (virtual machine).
    正文Body Yes 资源:主题Resource: 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 specified text and select the event's Topic, Event Type, ID, and Event Time fields so that your email includes the resource that fired the event, event type, event timestamp, and event ID for the update. 对于本教程,资源是在触发器中选择的 Azure 资源组。For this tutorial, the resource is the Azure resource group selected in the trigger.

    若要在内容中添加空行,请按 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:

    完成的逻辑应用

  6. 保存逻辑应用。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, select Overview. 若要查看有关运行的更多详细信息,请选择运行所在的行。To view more details about a run, select 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, select 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, select Overview. 在工具栏上选择“删除” 。On the toolbar, choose Delete. 确认要删除逻辑应用,然后选择“删除” 。Confirm that you want to delete your logic app, and choose Delete.

后续步骤Next steps