Create an Event Grid Subscription to an Azure Function V2

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.

Nuget

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.

egextensionnotinstalled

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.

SystemKey

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:

http://{functionappname}.azurewebsites.net/admin/host/systemkeys/eventgrid_extension?code={masterkey}

Where the MasterKey is the “_master” key of your function app.

App Setting

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.

Deployment

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: