all Technical posts

Accepting Azure Event Grid events with Azure API Management

Azure API Management & Azure Event Grid are a great combination, but it needs some setup. I've contributed an API Management policy to get you started!

Recently I’ve been working heavily with Azure Event Grid where we were triggering new processes when events occurred. To achieve this, this meant that we had to subscribe for events, interpret the content and call the downstream process.

Azure API Management is a great way to decouple your internal APIs from your consumers and implement edge intelligence via Policies.

This was a perfect fit for our scenario as we can subscribe to Azure Event Grid to receive all events and route it to one operation of our API so that we can call the downstream system.

The benefit of doing this is that if the system that I need to trigger changes, I only need to change my API gateway routing and not my event subscription. It also allows us to use API Management as our API gateway for both customers and internal systems.

This allows you to build reactive applications without having to tightly couple your components to Azure Event Grid topic:

A great example is that you can easily trigger an Azure Data Factory pipeline from within a policy when an Event occurs.

Don’t waste your time, use the starter template

Implementing this was not a walk in the park at first but I managed to get there. This is because of every new event subscription needs to prove the ownership of the specified URL by returning a validation code as part of the initial event. Another issue was that I was reading the body of the request multiple times without passing preserveContent which caused the policy to fail.

The Azure API Management team started gathering quickstart templates on GitHub for policies that help you build certain scenarios.

To save you some time and effort, I decided to contribute my effort for accepting Azure Event Grid events via a policy template for an Azure API Management which you can use to get started. The policy will handle the handshake for you and allow you to take it from there! Currently, it only supports one event in the payload but you can easily extend it to support multiple events.

I hope this saves you a lot of time and feel free to suggest changes!

API Management ❤ Event Grid

Azure API Management & Azure Event Grid are a good match and should certainly be considered.

However, it would be great to see some closer collaboration between these services so that:

  • Azure API Management is considered a trusted endpoint for Event Grid – That would allow us to skip the authentication part
  • Build-in support for sending custom events from an API Management policy towards Azure Event Grid
  • Emit service events for Azure API Management to Azure Event Grid– A good example would be an event when somebody requests access to an API and is waiting for approval

Thanks for reading,

Tom.

Subscribe to our RSS feed