Azure Event Grid订阅级部署报错:topic属性与预期不匹配问题咨询
问题根因与排查解决步骤
核心错误原因
你收到的The specified topic property does not match the expected topic from the event subscription scope报错,本质是事件订阅的部署作用域与你指定的Topic(订阅级系统Topic)不匹配:
当前你是在资源组级别执行ARM部署,模板里定义的Microsoft.EventGrid/eventSubscriptions类型默认绑定的作用域是当前资源组,但是你在properties里指定的topic是整个Azure订阅的ID,两者作用域不一致导致校验失败。
具体修复方案
1. 修改事件订阅的资源类型与部署层级
要监听整个订阅内的虚拟机事件,你需要做两处调整:
- 把ARM模板的部署层级从「资源组级」改为「订阅级」,对应修改模板的
$schema为订阅级部署的schema:
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#"
- 订阅级部署的事件订阅默认绑定当前订阅作为Topic,不需要单独指定
topic属性。
2. 修正模板里的其他逻辑错误
你当前模板还有两处会导致后续事件过滤失效的问题:
- 高级过滤里的虚拟机删除操作名拼写错误,正确应为
Microsoft.Compute/virtualMachines/delete(你漏写了virtualMachine的复数后缀s) - 删除事件对应的HTTP请求方法是
DELETE,你当前的高级过滤只包含PUT/PATCH,会导致删除事件被过滤掉,需要把DELETE加入data.httpRequest.method的取值列表。
额外优化建议:把操作名匹配的StringContains改为StringIn,避免误匹配到其他包含对应字符串的无关操作
3. 调整后可直接使用的完整模板参考
{ "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "location": { "type": "string", "defaultValue": "[deployment().location]" }, "resourceGroupName": { "type": "string", "defaultValue": "替换为存放EventHub的资源组名" } }, "variables": { "eventHubNamespace": "evtNameSpace", "eventHubName": "evtHub001" }, "resources": [ // 若资源组已存在可删除这段资源定义 { "type": "Microsoft.Resources/resourceGroups", "apiVersion": "2021-04-01", "name": "[parameters('resourceGroupName')]", "location": "[parameters('location')]", "properties": {} }, // 部署EventHub相关资源到指定资源组 { "type": "Microsoft.Resources/deployments", "apiVersion": "2021-04-01", "name": "eventHubDeployment", "resourceGroup": "[parameters('resourceGroupName')]", "dependsOn": [ "[resourceId('Microsoft.Resources/resourceGroups', parameters('resourceGroupName'))]" ], "properties": { "mode": "Incremental", "template": { "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "resources": [ { "type": "Microsoft.EventHub/namespaces", "apiVersion": "2018-01-01-preview", "name": "[variables('eventHubNamespace')]", "location": "[parameters('location')]", "sku": { "name": "Standard" }, "properties": { "isAutoInflateEnabled": true, "maximumThroughputUnits": 7 }, "resources": [ { "type": "EventHubs", "apiVersion": "2017-04-01", "name": "[variables('eventHubName')]", "dependsOn": [ "[resourceId('Microsoft.EventHub/namespaces', variables('eventHubNamespace'))]" ], "properties": { "messageRetentionInDays": 1, "partitionCount": 2 } } ] } ] } } }, // 订阅级事件订阅定义 { "type": "Microsoft.EventGrid/eventSubscriptions", "apiVersion": "2020-06-01", "name": "cyberarktest00001", "location": "global", "dependsOn": [ "eventHubDeployment" ], "properties": { "destination": { "endpointType": "EventHub", "properties": { "resourceId": "[resourceId(parameters('resourceGroupName'), 'Microsoft.EventHub/namespaces/eventhubs', variables('eventHubNamespace'), variables('eventHubName'))]" } }, "filter": { "isSubjectCaseSensitive": false, "includedEventTypes": [ "Microsoft.Resources.ResourceWriteSuccess", "Microsoft.Resources.ResourceDeleteSuccess" ], "advancedFilters": [ { "operatorType": "StringIn", "key": "data.operationName", "values": [ "Microsoft.Compute/virtualMachines/write", "Microsoft.Compute/virtualMachines/delete" ] }, { "operatorType": "StringIn", "key": "data.httpRequest.method", "values": [ "PUT", "PATCH", "DELETE" ] } ] }, "retryPolicy": { "maxDeliveryAttempts": 2, "eventTimeToLiveInMinutes": 120 } } } ] }
4. 部署注意事项
- 订阅级部署需要指定部署位置,执行部署时请添加对应参数,比如Azure CLI部署命令参考:
az deployment sub create --location 替换为对应区域编码 --template-file 替换为你的模板文件名.json
- 执行部署的账号需要有订阅级的
EventGrid EventSubscription Contributor权限,以及对应资源组的资源创建权限。
内容的提问来源于stack exchange,提问作者Gladiator
相关产品推荐
相关产品推荐

