使用ARM创建事件网格订阅以收集订阅事件

时间:2020-03-30 15:00:26

标签: azure azure-functions azure-eventgrid

基本上,我正在尝试使用ARM部署事件网格订阅以收集订阅中的特定事件(主题类型= Azure订阅)。我已经有一个创建了事件网格触发器函数的函数应用程序,只需要将该函数与事件网格订阅绑定在一起即可。

我在Azure DevOps中使用发布管道来自动化整个工作流程。

这是我使用的一个示例:

{
  "$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"
                ]
                }
            ] 
        }           
        }
    }
  ]
}

这最终部署了事件网格主题,而不是事件网格订阅。

然后建议我尝试以下操作:

{
  "$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"
                    ]
                    }
                ] 
            }           
          }
      }
    ]
}

但这最终以以下错误失败: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

只需要找到一种使用ARM或Azure DevOps来自动执行此过程的方法即可。

1 个答案:

答案 0 :(得分:1)

我有一个基于您的模板的更新模板,并对此模板进行了测试,它工作正常。它会创建一个“事件网格主题和订阅”,并将“事件网格触发器”功能与其关联。

{
    "$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"
                    ]
                }
            }
        }
    ]
}

您可以通过转到功能应用程序->功能->事件网格触发功能->集成选项卡来复制事件网格订阅URL。在此选项卡上,您将找到事件网格订阅URL副本,该副本将作为模板的输入提供。

enter image description here

希望这会有所帮助!