2

I'm trying to create an Eventgrid subscription on an Azure Storage Account using an ARM template. Manually creating it in the Portal and going to the advanced settings yielded me the template below. I further added the required template items such as schema to it, but it keeps yielding me errors. I've tried looking online for similar templates, but can't seem to find any using the "endpointType": "AzureFunction". Also within the Resource Explorer there's no mention of the deployment to further help me along.

Anybody can help me out what is wrong?

The template as generated during creation from the portal:

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

The full template:

{
    "$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 error:

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

NotFound
  • 5,005
  • 2
  • 13
  • 33

2 Answers2

4

I've been trying to do the exact same thing by any option in the Azure tool chain (ARM Template/CLI/REST). I looked at the Portal's calls and found it is using the 2020-01-01-preview EventGrid API that you show.

After some testing I can confirm the new API allows deploying a subscription with an EndpointType of AzureFunction like so:

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

It seems that your problem is unrelated to trying to target the AzureFunction and you're using the right API version so it doesnt seem to be that.

I think the problem is your "Type" value. I think it should be in this format: //providers/eventSubscriptions

So it would be Microsoft.Storage/storageAccounts/providers/eventSubscriptions.

  • This was very helpful! Have you figured out how to subscribe to an Event Grid Domain Topic as well? I've tried this: "topic": "[resourceId('Microsoft.EventGrid/Domains/topics/', '{my Event Grid domain name}', '{my topic name}')]", Used this syntax, since this is what I saw in the portal "Advanced Editor" page. And it gives the error: "The specified topic property does not match the expected topic from the event subscription scope." Since EventGrid Domain Topics aren't precreated like non-Domain topics, I can't figure out how to avoid this error. – Kirk Marple Feb 09 '20 at 09:38
  • I have not tried doing an Event Grid Domain Topic but I would expect it would be a "type" of "Microsoft.EventGrid/domains/topics/providers/eventSubscriptions". – Allan Kintz Feb 10 '20 at 13:25
1

I don't believe there is a separate endpointType of AzureFunction as documented. It is simply a special case of a webhook handler.

This GitHub Repo contains a sample ARM Template that you can refer to. Here is the exact snippet that you would need

...
"destination": {
    "endpointType": "WebHook",
    "properties": {
        "endpointUrl": "[concat(variables('functionUrl'), listKeys(resourceId('Microsoft.Web/sites/host/', variables('functionAppName'), 'default'),'2016-08-01').systemkeys.eventgrid_extension)]"
    }
}
...
PramodValavala
  • 6,026
  • 1
  • 11
  • 30
  • 1
    For newer ARM API versions it **is** [documented](https://learn.microsoft.com/en-us/azure/templates/microsoft.eventgrid/eventsubscriptions#endpointproperties-object) – hansmbakker Dec 07 '20 at 15:23