使用 az cli 创建 EventGrid 订阅

Create EventGrid subscription with az cli

我正在使用此命令创建事件订阅:

az eventgrid event-subscription create --source-resource-id "/subscriptions/mysubscription/resourceGroups/myresourcegroup/providers/Microsoft.EventGrid/topics/mytopic" --name "new-subscription" --endpoint-type "webhook" --endpoint "https://myfunctionapp.azurewebsites.net/runtime/webhooks/EventGrid?functionName=myfunction&code=mymasterkey"

但总之,我收到此错误消息:

Deployment failed. Correlation ID: 154239e9-9992-4c4c-a991-83b88cc9bd91. Webhook validation handshake failed for https://myfunctionapp.azurewebsites.net/runtime/webhooks/EventGrid. Http POST request failed with response code Unknown. For troublehooting, visit https://aka.ms/esvalidation. Activity id:a9a4804c-927f-4bc4-8f0b-99d80eed08a3, timestamp: 7/14/2020 10:20:28 AM (UTC).

怎么了?

根据错误,看来你的函数URL不正确,无法完成验证。函数 URL 应该类似于 https://{functionappname}.azurewebsites.net/runtime/webhooks/eventgrid?functionName={functionname}&code={systemkey}。您可以通过 Azure 门户获取它。详情请参考document

例如

az eventgrid event-subscription create  -n "test" --source-resource-id "<my topic resource id>"  --endpoint "https://myfunctionapp.azurewebsites.net/runtime/webhooks/EventGrid?functionName=myfunction&code=mymasterkey"

此外,由于你使用了Azure函数事件乘坐触发器,你可以直接使用函数应用程序资源id作为端点。详情请参考document and here

例如

az extension add --name eventgrid

az eventgrid event-subscription create --name "test1" \
    --source-resource-id /subscriptions/{SubID}/resourceGroups/{RG}/providers/Microsoft.EventGrid/topics/topic1 \
    --endpoint /subscriptions/{SubID}/resourceGroups/{RG}/providers/Microsoft.Web/sites/{functionappname}/functions/{functionname} \
   --endpoint-type azurefunction