简体   繁体   中英

Use ARM to create an event grid subscription to collect events for a subscription

Basically, I am trying to use ARM to deploy an event grid subscription to collect specific events within a subscription (Topic Types = Azure Subscriptions). I already have a function app with an event grid trigger function created, just need to tie the function with the event grid subscription as a webhook.

I am using a release pipeline in Azure DevOps to automate this whole workflow.

Here is one example that I used:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "egstopic": {
      "type": "string",
      "defaultValue": "egstopic1",
      "metadata": {
        "description": "Event grid system topic"
      }
    },
    "eventSubName": {
      "type": "string",
      "defaultValue": "esub1",
      "metadata": {
        "description": "Event grid system topic"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "eventGridFunc":{
        "type": "string",
        "defaultValue": "VmAddedListener",
        "metadata": {
            "description" : "Function Name"
        }
      }
    },
  "variables": {
    "functionUrl" : "[concat('https://', variables('FunctionAppName'),'.azurewebsites.net/runtime/webhooks/eventgrid?functionName=', parameters('eventGridFunc'),'&code=')]",
    "functionAppName": "event-driven-func2"
  },
  "resources": [
    {
        "type": "Microsoft.EventGrid/Topics",
        "apiVersion": "2018-01-01",
        "name": "[parameters('egstopic')]",
        "location": "[parameters('location')]",
        "properties":{}
    },
    {
    "type": "Microsoft.EventGrid/Topics/providers/eventSubscriptions",
    "name": "[concat(parameters('egstopic'), '/Microsoft.EventGrid/', parameters('eventSubName'))]",
    "location": "[parameters('location')]",
    "apiVersion": "2018-01-01",
    "dependsOn": [
                "[parameters('egstopic')]"
            ],
    "properties": {
        "destination": {
            "endpointType": "WebHook",
            "properties": {
                "endpointUrl": "[concat(variables('functionUrl'), listKeys(resourceId('Microsoft.Web/sites/host/', variables('functionAppName'), 'default'),'2016-08-01').masterKey)]"
            }
        },
        "filter": {
            "includedEventTypes": [
                "Microsoft.Resources.ResourceWriteSuccess"
            ],
            "advancedFilters": [
                {
                "key": "data.operationName",
                "operatorType": "StringContains",
                "values": [
                    "Microsoft.Compute/virtualMachines/write"
                ]
                }
            ] 
        }           
        }
    }
  ]
}

This ended up deploying an event grid topic instead of an event grid subscription.

Then I was suggested to attempt the following:

{
  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
  "contentVersion": "1.0.0.1",
  "parameters": {
    "egstopic": {
      "type": "string",
      "defaultValue": "egstopic1",
      "metadata": {
        "description": "Event grid system topic"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "eventGridFunc":{
        "type": "string",
        "defaultValue": "VmAddedListener",
        "metadata": {
            "description" : "Function Name"
        }
      }
    },
  "variables": {
    "functionUrl" : "[concat('https://', variables('FunctionAppName'),'.azurewebsites.net/runtime/webhooks/eventgrid?functionName=', parameters('eventGridFunc'),'&code=')]",
    "functionAppName": "event-driven-func2",
    "eventSubName": "[concat('esub',uniquestring(resourceGroup().id))]",
    "eventSubTopic": "[concat('/subscriptions/',subscription().subscriptionid)]"

  },
  "resources": [
      {
        "type": "Microsoft.EventGrid/systemTopics/eventSubscriptions",
        "name": "eventSubEG1",
        "location": "[parameters('location')]",
        "apiVersion": "2020-04-01-preview",
        "properties": {
            "destination": {
                "endpointType": "WebHook",
                "properties": {
                    "endpointUrl": "[concat(variables('functionUrl'), listKeys(resourceId('Microsoft.Web/sites/host/', variables('functionAppName'), 'default'),'2016-08-01').masterKey)]"
                }
            },
            "filter": {
                "includedEventTypes": [
                    "Microsoft.Resources.ResourceWriteSuccess"
                ],
                "advancedFilters": [
                    {
                    "key": "data.operationName",
                    "operatorType": "StringContains",
                    "values": [
                        "Microsoft.Compute/virtualMachines/write"
                    ]
                    }
                ] 
            }           
          }
      }
    ]
}

But this ended up failing with this error: A nested resource type must have identical number of segments as its resource name. A root resource type must have segment length one greater than its resource name A nested resource type must have identical number of segments as its resource name. A root resource type must have segment length one greater than its resource name

Just need to find a way to use ARM or Azure DevOps to automate this process.

I have a updated template based on your templates and tested this template and it is working fine. It creates a Event Grid Topic and Subscription and ties the Event Grid Trigger function to it.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "eventGridTopicName": {
            "type": "String",
            "metadata": {
                "description": "The name of the Event Grid custom topic."
            }
        },
        "eventGridSubscriptionName": {
            "type": "String",
            "metadata": {
                "description": "The name of the Event Grid custom topic's subscription."
            }
        },
        "eventGridSubscriptionURL": {
            "type": "String",
            "metadata": {
                "description": "Event grid subscription URL."
            }
        },
        "location": {
            "defaultValue": "[resourceGroup().location]",
            "type": "String",
            "metadata": {
                "description": "The location in which the Event Grid resources should be deployed."
            }
        }
    },
    "resources": [
        {
            "type": "Microsoft.EventGrid/topics",
            "apiVersion": "2018-01-01",
            "name": "[parameters('eventGridTopicName')]",
            "location": "[parameters('location')]"
        },
        {
            "type": "Microsoft.EventGrid/topics/providers/eventSubscriptions",
            "apiVersion": "2018-01-01",
            "name": "[concat(parameters('eventGridTopicName'), '/Microsoft.EventGrid/', parameters('eventGridSubscriptionName'))]",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[parameters('eventGridTopicName')]"
            ],
            "properties": {
                "destination": {
                    "endpointType": "WebHook",
                    "properties": {
                        "endpointUrl": "[parameters('eventGridSubscriptionURL')]"
                    }
                },
                "filter": {
                    "includedEventTypes": [
                        "All"
                    ]
                }
            }
        }
    ]
}

You can copy Event Grid Subscription URL by going to Function App -> Functions -> Event Grid Trigger Function -> Integrate tab. On this tab you will find the Event Grid Subscription URL copy that and provide as input to template.

在此处输入图片说明

Hope this helps!

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