ARM Template for Event Grid integration with a new Azure Function

July 19, 2019 3 By JeremyBrooks

TL/DR – Link to the template: https://github.com/jcbrooks92/EventGridAzureFunctionARMTemplate/tree/master

Creating an ARM template with integration between an Azure function and Event Grid requires the general function template, Event Grid resource and lastly a system key from the Azure function that is used to authenticate the validation call from Event Grid.

In order for the System key to be created the Azure Function job host has to initialize at least once. As a workaround you can generate your own sytem key using the portion of the ARM template below .

 {
        "name": "default/eventgrid_extension",
        "apiVersion": "2018-11-01",
        "type": "host/systemkeys",
        "properties": {
                "name": "eventgrid_extension",
                        "value": "12345"
             },
        "dependsOn": [
               "[resourceId('Microsoft.Web/sites', variables('functionAppName'))]"
                 ]
} 

Lastly you need to generate the web hook URL that Event Grid will use to authenticate to the function app with the system key. You’ll need to make sure the job host has started in order for the validation to complete. I used a function app deployed with run from package and made the Event Grid Topic creation dependent on the function to provide enough time for the app to deploy prior to the validation occurring. My ‘endpointUrl’ is a value that creates the general webhook URL so the system key just needs to be plugged in.

        "functionUrl" : "[concat('https://', variables('FunctionAppName'),'.azurewebsites.net/runtime/webhooks/eventgrid?functionName=', parameters('FunctionName'),'&code=')]",
{
            "name": "[concat(parameters('eventGridTopicName'), '/Microsoft.EventGrid/', parameters('eventGridSubscriptionName'))]",
            "type": "Microsoft.EventGrid/topics/providers/eventSubscriptions",
            "location": "[parameters('location')]",
            "apiVersion": "2018-01-01",
            "properties": {
                "destination": {
                    "endpointType": "WebHook",
                    "properties": {
                        "endpointUrl": "[concat(variables('functionUrl'), listKeys(resourceId('Microsoft.Web/sites/host/', variables('functionAppName'), 'default'),'2016-08-01').systemkeys.eventgrid_extension)]"
                    }
                },
                "filter": {
                    "includedEventTypes": [
                        "All"
                    ]
                }
            },
            "dependsOn": [
                "[parameters('eventGridTopicName')]",
                "[resourceId('Microsoft.Web/sites/host/systemkeys', variables('functionAppName'),'default','eventgrid_extension')]"

            ]
        }

Azure Function ARM API Reference : https://github.com/Azure/azure-functions-host/issues/3994