Last weeks I was developing an Azure Function with an Event Grid Trigger using the new V2 Runtime (in .Net Core) and because the changes from V1 some things where not working anymore as expected.
In this post I will show you how to create an Event Grid subscription to an Azure Function with an Event Grid Trigger in the V2 Runtime.
One of the changes in the V2 runtime is that now the functions bindings (HTTP, Storage Queue, Timer, Event Grid etc..) are now provided in separate Nuget packages, called “binding extensions”.
For an Event Grid Trigger we need the Microsoft.Azure.WebJobs.Extensions.EventGrid Nuget package (at least version 2.0.0 or higher). If you create an Azure Function with an Event Grid trigger from the Portal the UI will alert you that you need this extension and it will be installed for you behind the scenes.
It’s important to install the package because it has to be present in the bin directory of the function app otherwise the following steps will not work.
If you are using a precompiled C# function after installing the Nuget package you have to publish the Function in Azure.
To create an Event Grid Subscription to the function we need a SystemKey, the endpoints to get this keys are changed in V2, unfortunately at the moment of writing the documentation has not been updated. I have created an issue for this and Microsoft is working to update the docs.
The right endpoint to get the SystemKey is:
Where the MasterKey is the “_master” key of your function app.
Before we can do the call to get the SystemKey we need to set/add the
AzureWebJobsSecretStorageType app setting to ‘files‘, this because this issue due to changes in the Azure Function V2 Runtime.
After all the previous steps are done we can now generate the SystemKey and use it to create an Event Grid Subscription to the function endpoint.
I have created a Gist with a PowerShell script to do all the work needed:
In the PowerShell script I use an ARM Template (from the Azure Quickstart Gallery) to create the Event Grid Subscription at Azure Subscription level (sorry for the confusing subscription words).
If you want to create the Event Grid Subscription at Resource Group level use the New-AzureRmResourceGroupDeployment cmdlet instead (at line 49), providing the Resource Group name you want to link.