You can extend functionality of any API through use of a decentralized architecture and events through event grid. This article will cover listening to events from event grids using an Azure function and is a follow-up to another post covering adding events with event grid to a GraphQL API (using HotChocolate).
Prerequisites
- The previous post
Why
Microservice architecture lends itself well to extensibility via event-driven design. The project this post references is a good example of this. Team Builder is intended to help organizations with getting started running hackathons. Some organizations may be interested in integrating Team Builder with GitHub. Using event-driven extensibility allows us to provide this integration as a feature without compromising SOLID principles when developing the base functionality of Team Builder. Organizations can choose to deploy the API and Azure function that support GitHub integration or leave the base implementation as is.
In addition, Event Grid is the way many of Azure’s services communicate and their events are available for you to listen to and develop against. You can read more about Azure services and their events here.
Creating an Event Grid Trigger Azure Function
While there are many ways to listen to event grid events (you can find more here), we will be using an Azure function. You can easily start a new event grid triggered Azure function in Visual Studio like so:
Easy enough. We now have a function where we add our business logic or wire up a workflow from various microservices.
Wiring Up to Event Grid
Once you have deployed your function application to Azure, we can wire it up to Event Grid. In the previous post, we created an Event Grid topic. To listen to our Event Grid topic with the function, we need to create an Event Grid subscription.
It is worth noting that you can filter on multiple event types. This can be helpful in minimizing the number of times your function executes (thereby lowering your consumption costs). There are more advanced filtering features you can read about here.
Once you’ve set up your subscription, you’re all set. New events that subscription is filtering on (or all events if you chose not to setup filters) will trigger execution of your Azure function.
Extending Hackathon Team Builder
Now that you are familiar with extensibility through Event Grid, you are invited to add features that you’d like to see in Team Builder following the same pattern.
Conclusion
Event-driven design works hand-in-glove with adding extensibility to microservices and is incredibly easy to achieve with Azure Event Grid. In addition, Event Grid allows you to not only add extensibility to your own applications but take advantage of extending other Azure services you may want to leverage.