ARM Template for Event Grid integration with a new Azure Function

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.

Update 2/12/2020
There has been a recent update for a preview version of event grid ARM API that supports an endpoint specifically for AzureFunctions. I still need to test out this change to understand its behavior, but I just wanted to provide this update for those that may be interested in testing out the change.

“apiVersion”: “2020-01-01-preview”,
“properties”: {
“destination”: {
“endpointType”: “AzureFunction”,
“properties”: {
“resourceId”: “[resourceId(‘Microsoft.Web/sites/functions/’, parameters(‘AppName’), parameters(‘functionName’))]”
}
}

Update 1/10/2020
The section of creating your own Systemkeys for eventgrid is NO LONGER needed due to the update to the runtime here. 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