In last few articles, I have been writing about the azure messaging and event-based solutions.
In last article, we have seen what are topics and subscriptions and how event grid can be used in solutions where Azure service is raising event and another Azure service is consuming it. Below are the links to those posts and I would suggest to go through them in case you did not get chance to look at those articles:
- Azure Solutions: Message Based vs Event Based
- Azure Event Grid Getting Started on Basics
- Azure Event Grid Topics and Subscriptions
In this article, we will see how how event grid can be used to send events to custom applications. These events can be from either Azure services or from the custom applications.
For following steps in this article, you will need access to Azure Portal.
If you don’t have an Azure subscription, create a free account before you begin.
Azure Events to Your Applications
We will try how to send storage account events to your handler application. The handler application is mocked here by using a logic app to explain the concept. But the handler application can expose a HTTP endpoint which is reachable from event grid and it should accept post request.
Azure event grid can send events from Azure services to your custom applications using Web hooks. While specifying event handler you have to specify the internet URL for your application endpoint which can handle the event.
In this section, lets have a look at how this can be done.
Logic app to mock custom application
For simulating custom application we will create logic app and add “when a HTTP request received” trigger. Once this logic app is saved, the trigger will be allocated with public HTTP URL.
Now, copy this URL, we will need this while creating subscription inside event grid.
Add event grid subscription
Click on “All services” from left navigation and then search for event grid subscriptions. Once it is found in search results, click on it.
In events subscriptions blade, make sure you have selected appropriate subscription, location and topic type to view your already existing subscriptions.
Click on Add event subscription button and it will open a new panel where subscription details can be entered.
Create event subscription panel
On create subscription page, enter below details:
- Name – unique name for subscription
- Event schema – keep it default.
- Topic Type – select storage account
- Subscription – Azure subscription under which storage account exists
- Resource group – name of the resource group under the storage account exists
- Resource – name of the storage account
- Event type – keep it default. It will subscribe to both blob created and deleted events
- Endpoint Type – select web hook from the dropdown. Once this is selected, there will be a new link visible below dropdown to add the endpoint. You can click on that and provide our logic app‘s URL.
Running the solution
Once this is done, using storage explorer, you can try adding a file to the container in the selected storage account. It will send an event to logic app.
You can then open the logic app run details and check the event details. It should show details of the events received from event grid.
As you understand now, the event handlers get events through subscriptions. Subscriptions can have filters to allow only subset of events to reach out to different subscribers.
Custom Events to Your Applications
For sending your application events via event grid, you need to create a topic.
Here we will try to send a json schema (which represents the event) from one custom application to another custom application. The event handler application is the same logic app we have created in previous section.
The event sending part, we will simulate using another logic app which just posts event to event grid.
Create event grid topic
For creating topic, you can search for Event grid topic in the search box and when you click on that you will see Event Grid Topics page. There click on Add button.
It will open Create Topic page. Below details are required on this page:
- Name – unique name of the topic
- Subscription – under which it should be created
- Resource group – the RG inside which the topic should be placed
- Location – where this resource should be deployed physically
- Event schema – keep the schema setting to the defaults
Create event grid subscription
Now, the event handler application should subscribe to the topic. The steps are same as those mentioned in our first sample.
While creating subscription, select topic type to be Event Grid Topic.
In endpoint details section, select web-hook and provide endpoint of our previously created logic app – which we created in previous section.
Mocking event sender using logic app
Event grid topics accept events through HTTP REST requests. You can send events using PowerShell, or Azure CLI or you can send events using your c# code.
In this section, we will create a logic app which posts event data to Event grid topic.
Create a logic app with recurrence trigger and set it to run after every 3 minutes.
Then search for event grid and add an action to publish event grid events.
After selecting Publish event you will have to provide subscription details, topic endpoint url and the SAS key.
On event grid topics page, select the topic which we created. Then on overview blade you will see topic endpoint. For SAS key, click on access key and copy primary key. Use these two values in logic app.
Your completed logic app should look like the below snapshot
Running the logic apps
We do not need to run anything now. After every 3 minutes, event sender logic app will send the events. This will trigger the event handler logic app.
In event handler logic app, you can open any previous run and check the event details to make sure that all the event data is flowing to other application.
Again, please note that we have used logic apps to mock the custom applications. But in real world, it can be any app which has internet reachable HTTP endpoint.
We have seen in this article that topics are used to send events from any application to event grid and subscriptions are used to subscribe to events. There are some azure services for which this works automatically.
I hope you enjoyed this article. Please do comment and let me know your thoughts.