azureazure-logic-apps

Receive notification of changing virtual machine size with Azure LogicApp


  1. I'm all set up to get notifications by email and Slack when the size of the virtual machine changes. But I don't know how to get the old virtual machine size and the changed current virtual machine size.

  2. When "Microsoft.Compute/virtualMachines/write" is triggered, you'll be notified of something, but the write option has too many notifications. I only want to be notified when the virtual machine size has changed.

  3. The following is part of my code, but it doesn't work.

{
  "type": "If",
  "expression": {
    "or": [
      {
        "equals": [
          "@triggerBody()?['data']['operationName']",
          "Microsoft.Compute/virtualMachines/write"
        ]
      },
      {
        "equals": [
          "@triggerBody()?['data']['properties']['hardwareProfile']['vmSize']",
          "@triggerBody()?['data']['properties']['hardwareProfile']['vmSize']"
        ]
      }
    ]
  },
  1. If you can't import the size of the past, or if you have to use too many codes, I'd like to get the current virtual machine size that has been changed. I need your help. And make sure to get a notification when the following two conditions are True. You need to change "Microsoft.Compute/virtualMachines/write" and "vmsize" to get the notification.

Simply "Microsoft.Compute/virtualMachines/write" should not be notified just because this command has been executed.

I ask for your help.


Solution

  • A better way to get resource changes, such as virtual machine size is using azure resource graph. reference

    resourcechanges
    | extend vmSize = properties.changes["properties.hardwareProfile.vmSize"], changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceId = tostring(properties.targetResourceId), changeType = tostring(properties.changeType) 
    | where isnotempty(vmSize) 
    | order by changeTime desc 
    | project changeTime, targetResourceId, changeType, properties.changes, previousSize = vmSize.previousValue, newSize = vmSize.newValue
    

    output:

    enter image description here


    If you want using azure resource graph in logic app, you can use azure resource graph rest api. I have already coded a sample. But you need to pay attention to the authorization in http request in logic app. My example here directly applies the bearer token, you can also use managed identity instead.

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "HTTP_1": {
                    "inputs": {
                        "body": {
                            "query": "resourcechanges | extend vmSize = properties.changes['properties.hardwareProfile.vmSize'], changeTime = todatetime(properties.changeAttributes.timestamp), targetResourceId = tostring(properties.targetResourceId), changeType = tostring(properties.changeType)  | where isnotempty(vmSize) | order by changeTime desc | project changeTime, targetResourceId, changeType, properties.changes, previousSize = vmSize.previousValue, newSize = vmSize.newValue"
                        },
                        "headers": {
                            "Authorization": "Bearer eyJ0eXAiO*****Gs6m9iBfmQ",
                            "Content-Type": "application/json"
                        },
                        "method": "POST",
                        "queries": {
                            "api-version": "2021-03-01"
                        },
                        "uri": "https://management.azure.com/providers/Microsoft.ResourceGraph/resources"
                    },
                    "runAfter": {},
                    "runtimeConfiguration": {
                        "contentTransfer": {
                            "transferMode": "Chunked"
                        }
                    },
                    "type": "Http"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {
                "$connections": {
                    "defaultValue": {},
                    "type": "Object"
                }
            },
            "triggers": {
                "When_a_HTTP_request_is_received": {
                    "correlation": {
                        "clientTrackingId": "wbtrackingId"
                    },
                    "kind": "Http",
                    "type": "Request"
                }
            }
        },
        "parameters": {
            "$connections": {
                "value": {}
            }
        }
    }
    

    logic app output:

    enter image description here