在 Function 端点上使用 ARM 创建 Eventgrid 订阅

Creating Eventgrid subscription using ARM on Function endpoint

我正在尝试使用 ARM 模板在 Azure 存储帐户上创建 Eventgrid 订阅。在门户中手动创建它并转到高级设置,我得到了下面的模板。我进一步向其中添加了所需的模板项,例如架构,但它一直给我带来错误。我试过在网上寻找类似的模板,但似乎无法使用 "endpointType": "AzureFunction" 找到任何模板。在 Resource Explorer 中也没有提及部署以进一步帮助我。

任何人都可以帮我解决问题吗?

从门户创建期间生成的模板:

{
    "name": "test123",
    "properties": {
        "topic": "/subscriptions/<guid>/resourceGroups/<myGroup>/providers/Microsoft.Storage/storageAccounts/<myStorageAccount>",
        "destination": {
            "endpointType": "AzureFunction",
            "properties": {
                "resourceId": "/subscriptions/<guid>/resourceGroups/<myGroup>/providers/Microsoft.Web/sites/<myFunctionsApp>/functions/<myFunction>",
                "maxEventsPerBatch": 1,
                "preferredBatchSizeInKilobytes": 64
            }
        },
        "filter": {
            "includedEventTypes": [
                "Microsoft.Storage.BlobCreated"
            ],
            "advancedFilters": [
                {
                    "operatorType": "StringContains",
                    "key": "Subject",
                    "values": [
                        "-original"
                    ]
                }
            ]
        },
        "labels": [],
        "eventDeliverySchema": "EventGridSchema"
    }
}

完整模板:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
    },
    "resources": [
        {
            "name": "test123",
            "type": "Microsoft.EventGrid/eventSubscriptions",
            "apiVersion": "2020-01-01-preview",
            "location": "westeurope",
            "properties": {
                "topic": "/subscriptions/<guid>/resourceGroups/<myGroup>/providers/Microsoft.Storage/storageAccounts/<myStorageAccount>",
                "destination": {
                    "endpointType": "AzureFunction",
                    "properties": {
                        "resourceId": "/subscriptions/<guid>/resourceGroups/<myGroup>/providers/Microsoft.Web/sites/<myFunctionsApp>/functions/<myFunction>",
                        "maxEventsPerBatch": 1,
                        "preferredBatchSizeInKilobytes": 64
                    }
                },
                "filter": {
                    "includedEventTypes": [
                        "Microsoft.Storage.BlobCreated"
                    ],
                    "advancedFilters": [
                        {
                            "operatorType": "StringContains",
                            "key": "Subject",
                            "values": [
                                "-original"
                            ]
                        }
                    ]
                },
                "labels": [
                ],
                "eventDeliverySchema": "EventGridSchema"
            }
        }
    ]
}

错误:

The specified topic property does not match the expected topic from the event subscription scope

我不认为 AzureFunctiondocumented 有单独的 endpointType。它只是 webhook 处理程序的一个特例。

GitHub Repo 包含您可以参考的示例 ARM 模板。这是您需要的确切片段

...
"destination": {
    "endpointType": "WebHook",
    "properties": {
        "endpointUrl": "[concat(variables('functionUrl'), listKeys(resourceId('Microsoft.Web/sites/host/', variables('functionAppName'), 'default'),'2016-08-01').systemkeys.eventgrid_extension)]"
    }
}
...

我一直在尝试通过 Azure 工具链 (ARM Template/CLI/REST) 中的任何选项来做完全相同的事情。我查看了 Portal 的调用,发现它正在使用您显示的 2020-01-01-preview EventGrid API。

经过一些测试后,我可以确认新的 API 允许使用 AzureFunction 的 EndpointType 部署订阅,如下所示:

{
  "name": "[concat(variables('eventDomainName'), '/Microsoft.EventGrid/', variables('subscriptionName'))]",
  "type": "Microsoft.EventGrid/domains/providers/eventSubscriptions",
  "location": "[variables('location')]",
  "apiVersion": "2020-01-01-preview",
  "properties": {
    "destination": {
        "endpointType": "AzureFunction",
        "properties": {
            "resourceId": "[resourceId('Microsoft.Web/sites/functions/', parameters('functionAppName'), parameters('functionName'))]"
        }
    },
    "filter": "[parameters('subscriptionProperties').filter]"
  }
}

您的问题似乎与尝试以 AzureFunction 为目标无关,并且您使用的是正确的 API 版本,因此看起来并非如此。

我认为问题出在您的 "Type" 值上。我觉得应该是这样的格式://providers/eventSubscriptions

所以它将是 Microsoft.Storage/storageAccounts/providers/eventSubscriptions。