Azure Event Grid is a great technology for creating “push-push” asynchronous communication between resources in Azure.
One of the benefits of such a “push-push” system is that it allows a consumer of events to remain stateless - no listener is required.
The Azure Portal allows for a fairly easy creation of subscriptions but ideally these subscriptions should be scripted and stored in source control.
The following PowerShell script creates two subscriptions that filter to two different event types.
Note that in this example you will need to substitute
<subscription id> with the id for your Azure subscription.
You will also need to login to Azure using
az login before running this script.
# this is a placeholder api endpoint - substitute with your own.
$eventGridEndpoint = "https://my-api.azurewebsites.net/api/handleeventgridevent"
# this will need to be changed to point to your own Event Grid topic
$eventGridTopicId = "/subscriptions/<Subscription Id>/resourceGroups/MyResourceGroup/providers/Microsoft.EventGrid/topics/My-EventGrid-Topic"
az eventgrid event-subscription create --name MyWebHookSubscription `
--source-resource-id $eventGridTopicId `
--endpoint-type webhook `
--endpoint $eventGridEndpoint `
--included-event-types MyFirstEventType `
az eventgrid event-subscription create --name MyAzureFunctionSubscription `
--source-resource-id $eventGridEndpoint `
--endpoint /subscriptions/<Subscription Id>/resourceGroups/MyResourceGroup/providers/Microsoft.Web/sites/MyFunctionApp/functions/MyFunction `
--endpoint-type azurefunction `
The above example only covers Web Hook and Azure Function subscription endpoints - for details of the other endpoint types, consult the documentation.
To use Event Grid with your own endpoints, there is a validation step you will need to go through.
This article details the steps needed to integrate into your API (note that the article uses Azure Functions as the basis but these principles can be applied to any API endpoint).