• Marcel Zehner

Event Grid: Overview And Custom Topics

When developing modern cloud apps, one needs to make the decision which archicture style fits best for an application. Some of the well-known styles are:

  • N-Tier > Traditional architecture for enterprise applications. The functionality is divided into multiple layers.

  • Microservices > Lots of independent and small services that can communicate together as needed. Every service implements a specific capability or function.

  • Event-driven > Event producers publish events that are consumed by event consumers.

Of course there's more than just this. But I want to drive your attention to the event-driven approach. Using this style, an event producer/publisher creates and publishes an event that is later consumed by an event handler/consumer. One service that routes events from producers to the correct consumers is Azure Event Grid.

Azure Event Grid

The Azure Event Grid functionality is "just there" and can be used without a need to provision it first. As an example, imagine you want to trigger an Azure Logic App every time a new blob has been written to a Storage Account container. This is where Event Grid can be used. So let's configure a Storage Account as an event publisher.



As we are configuring this on a Storage Account, we need to select the events of interest, e.g. "Blob Created". This can be further filtered down, e.g. to a specific container or a specific content type. Then finally configure the event handler aka. 'how the event will be consumed'. Is this example use a web hook that triggers a Logic App.


There are some more configuration options such as filters, dead-lettering or expiration settings available, but I will cover them in this post. But that's finally it, Event Grid is now used to accept events from the Storage Account and trigger a Logic App using a web hook every time a new blob has been created.


Custom Topics

Now what if you want to send custom events, e.g. from an application that lives on-premises? Of course this can be done as well. But before you can send events to Event Grid in this scenario, you need to prepare an endpoint to send the data to, or a so-called "Event Grid Topic". Just deploy a new resource and select "Event Grid Topic". The process is straightforward. Once created you will see that the new topic has an endpoint and 2 keys. Both components are needed to send custom events to the Azure Event Grid.


To simply test the custom topic you can use a PowerShell script to send an event. Here is a simple example.


$uri = "https://YOURTOPIC.westeurope-1.eventgrid.azure.net/api/events" $key = "YOURTOPICKEY" $body = @{ id= 100 eventTime= (Get-Date -Format s) eventType="Information" subject="My Application" data= @{ title="My Application was restarted" details="Restart enforced by administrator" } dataVersion="1.0" } $body = "["+(ConvertTo-Json $body)+"]" Invoke-WebRequest -Uri $uri -Method POST -Body $body -Headers @{"aeg-sas-key" = $key}


This should successfully send an event to Event Grid. Of course, you should first create a consumer for the event to make sure the event is handled as needed.

Call To Action

So, what's next?

Have fun routing your events!

Cheers, Marcel

make it noble gmbh

Bundesgasse 26

3011 Bern 

Switzerland

  • LinkedIn - White Circle
  • Instagram - White Circle
  • Twitter - White Circle