简体   繁体   中英

Using Event Grid, is it possible to trigger a logic app when a resource is created/deleted in Azure?

I would like to know if using Event Grid, is it possible to have a logic app triggered when any resource is deployed on a Azure Subscription.

The use case is:

  • Somebody creates/deletes a resource on a particular Azure subscription
  • It sends a event in Event Grid (not sure about that?)
  • A logic app is then triggered when such event occurs, this logic app will send notification in a Teams channel.

The goal here is to have a simple and basic helicopter view on what's happening on this sub.

For testing purposes, I've created a logic app and add a " When a resource event occurs " trigger with Microsoft.Resources.ResourceGroups and these event types:

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

在此处输入图像描述

Not sure I'm exploring here.

Then I've deployed a storageaccount , but I get notifications even when "Reviewing" the deployment just before the resource is actually deployed.

Once deployed, I also have random notifications (even if the storage account is not used, some kind of background activities I guess?)

As per this Official documentation :

Resource events are created for PUT, PATCH, POST, and DELETE operations that are sent to management.azure.com . GET operations don't create events.

Hence you are receiving multiple triggers. For minimal triggers you can add the filters for the deployment. Below is the flow I'm using.

在此处输入图像描述

Below is the complete JSON of my logic app

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Send_an_email_(V2)": {
                "inputs": {
                    "body": {
                        "Body": "<p>@{triggerBody()?['subject']} has been created</p>",
                        "Importance": "Normal",
                        "Subject": "xxx",
                        "To": "xxx"
                    },
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['office365']['connectionId']"
                        }
                    },
                    "method": "post",
                    "path": "/v2/Mail"
                },
                "runAfter": {},
                "type": "ApiConnection"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "When_a_resource_event_occurs": {
                "conditions": [],
                "inputs": {
                    "body": {
                        "properties": {
                            "destination": {
                                "endpointType": "webhook",
                                "properties": {
                                    "endpointUrl": "@{listCallbackUrl()}"
                                }
                            },
                            "filter": {
                                "includedEventTypes": [
                                    "Microsoft.Resources.ResourceDeleteSuccess",
                                    "Microsoft.Resources.ResourceActionSuccess"
                                ],
                                "subjectBeginsWith": "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Resources/deployments"
                            },
                            "topic": "/subscriptions/xxx/resourceGroups/xxx"
                        }
                    },
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['azureeventgrid']['connectionId']"
                        }
                    },
                    "path": "/subscriptions/@{encodeURIComponent('b83c1ed3-c5b6-44fb-b5ba-2b83a074c23f')}/providers/@{encodeURIComponent('Microsoft.Resources.ResourceGroups')}/resource/eventSubscriptions",
                    "queries": {
                        "x-ms-api-version": "2017-09-15-preview"
                    }
                },
                "splitOn": "@triggerBody()",
                "type": "ApiConnectionWebhook"
            }
        }
    },
    "parameters": {
        "$connections": {
            "value": {
                "azureeventgrid": {
                    "connectionId": "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Web/connections/azureeventgrid",
                    "connectionName": "azureeventgrid",
                    "id": "/subscriptions/xxx/providers/Microsoft.Web/locations/eastus/managedApis/azureeventgrid"
                },
                "office365": {
                    "connectionId": "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Web/connections/office365",
                    "connectionName": "office365",
                    "id": "/subscriptions/xxx/providers/Microsoft.Web/locations/eastus/managedApis/office365"
                }
            }
        }
    }
}

RESULTS:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM