ARM Template for Event Grid integration with an Existing Azure Function

Recently the Azure Functions Team released a set of ARM APIs related to retrieving information about your function app such as the function keys, host key, functions deployed to the function app, and the system keys. The system keys are required to wire up Event Grid with your function app since Event Grid has to validate whether the endpoint it will be sending information to is an endpoint that is expecting the requests.

Special thanks to DilbranMulder on providing the starting point.

Here’s the first ARM template I was able to complete that successfully allows me to complete the integration with an existing function app. You’ll need to change the values of the function app name, resource group the function is deployed to, as well as the function that will be integrated with Event Grid. Also at the bottom I output the value of the system key which you probably will want to remove for a production scenario. The output section of ARM is helpful for debugging purposes.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "eventGridTopicPrefix": {
            "type": "string",
            "defaultValue": "EventGridTopic",
            "metadata": {
                "description": "The name of the Event Grid custom topic."
            }
        },
        "eventGridSubscriptionName": {
            "type": "string",
            "defaultValue": "EventGridSubscriptoin",
            "metadata": {
                "description": "The prefix of the Event Grid custom topic's subscription."
            }
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "The location in which the Event Grid resources should be deployed."
            }
        },
        "functionAppResourceGroup": {
            "type": "string",
            "defaultValue": "This-Value-Should-Change"
        },
        "FunctionName" :{
            "type": "string",
            "defaultValue": "This-Value-Should-Change"
        },
        "functionAppName": {
            "type": "string",
            "defaultValue": "This-Value-Should-Change"
        }
    },
    "variables": {
        "functionAppId": "[concat(resourceId(parameters('functionAppResourceGroup'),'Microsoft.Web/sites/',parameters('functionAppName') ), '/host/default')]",
        "functionUrl" : "[concat('https://', parameters('FunctionAppName'),'.azurewebsites.net/runtime/webhooks/eventgrid?functionName=', parameters('FunctionName'),'&code=')]",
        "eventGridTopicName" : "[concat( parameters('eventGridTopicPrefix'), uniqueString(resourceGroup().id))]"
    },
    "resources": [
        {
            "name": "[variables('eventGridTopicName')]",
            "type": "Microsoft.EventGrid/topics",
            "location": "[parameters('location')]",
            "apiVersion": "2018-01-01"
        },
        {
            "name": "[concat(variables('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(variables('functionAppId'),'2016-08-01').systemkeys.eventgrid_extension)]"
                    }
                },
                "filter": {
                    "includedEventTypes": [
                        "All"
                    ]
                }
            },
            "dependsOn": [
                "[variables('eventGridTopicName')]"
            ]
        }
    ],
    "outputs": {
        "SystemKeyOutputed":{
            "type": "string",
            "value": "[listKeys(variables('functionAppId'),'2016-08-01').systemkeys.eventgrid_extension]"
        }
    }
}