Monitor Virtual machine changes by Azure Event Grid and Logic Apps
To monitor azure virtual machine operation changes you can automate and run task as workflow by creating azure logic apps using minimal code. These resources publish events to an Azure event grid. In turn event grid pushes those events to subscribers (e.g. logic app, webhook, event hubs endpoints) . As subscriber to event grid your logic app can wait for the events before running the workflow.
In this article, I will explain the below stages:
- 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.
Create blank logic app
- Sign in to the Azure portal with your Azure account credentials.
- From the main Azure menu, select Create a resource > Integration > Logic App.
Once logic apps gets created , go to the logic app designer and search for the event grid and select “When a resource event occurs”
Since we want to monitor any changes in the azure virtual machine we need to trigger an email to the administrator. Below configuration has event types
- Resource action success
- Resource action delete
- Resource action write
Once event configuration done then add action to trigger email with the details of the events.
Add Action Condition
Select action as “Condition” from the action pane as below:
Now in the condition the below expression :
triggerBody()?['data']['operationName']
And in the add the value as Microsoft.Compute/virtualMachines/write
Once above condition is then switch to logic code view and back again to logic designer view, it will automatically reflect the changes in the conditions.
Now Select the conditions to send email to the user, add below details :
To: Email id of the administrator
Subject: Email subject add Subject from the dynamic context
Body: Add resource full details in the email body
Test your logic app workflow
To check that your logic app is getting the specified events, update your virtual machine.