In one of my previous article, I already have discussed the basic concepts of Azure event grid. In this article, let’s have a closer look at what is event grid, where it should be used and how to create subscriptions in Azure Portal.
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.
Event Grid: A Closer Look
Azure event grid allows you to build event-based applications. By event-based, we mean they can automatically react to the subscribed events. They do not need to poll to check if something has happened or not.
The above diagram shows all the supported event sources on the left hand side and supported event handlers on right hand side.
The left side of diagram
Event grid has built in support for many azure services like blob storage, service bus, resource groups, etc. You can also use event grid custom topics for your custom application events.
Event grid also supports your application specific custom events (e.g. new user registered, password changed by user, new order was placed, etc) through custom topics.
The right side of diagram
On the right side of image, you can see possible types of event handlers. You can handle these events in either Azure services like logic apps, Azure functions, etc. OR you can send event to a web hook – where you can specify internet URL for your application which should consume this event.
You can send specific events to different endpoints. You can also multicast events to multiple endpoints. There are also provisions to ensure that your message is reliably delivered.
Let’s start with enabling event gridservice for your subscription.
Registering Event Grid
Click on “All services” option in left navigation. All services panel will open. Then you can search for “subscriptions“.
Alternatively, you can also select “General” and then “Subscriptions” as shown in below snapshot.
The subscriptions page will show you list of subscriptions available. You can select the subscription under which you want to setup the event grid.
Once the subscription is selected it will open subscription details panel. On that panel, select Resource providers from left navigation and then check if Microsoft.EventGrid service is registered as shown in below snapshot.
If it is not, then you will see “Not registered” test and beside that you may also see “Register” hyperlink depending on whether you have rights in the selected subscription.
You will have to make sure that this service is registered under your subscription.
Event Grid Demo
In this section, we will quickly see how event created by Azure service can be consumed in the Azure service.
In this sample, we will use storage account as event source while logic app will be the event handler.
You can follow above steps to make sure that event grid service is registered in your subscription. You should also have blank logic app and storage account already created.
Logic App Trigger
Then, you can go to logic app designer, it would be completely blank. Search for “Azure event grid” and then select “When a resource event occurs” trigger.
Once you click on that, it will show you login button. You can login to your Azure account. Once login is successful, you will have to enter below details:
- Subscription – Select subscription under which the event source is located.
- Resource type – select storage accounts from the drop down
- Resource name – select name of storage account which will act as event source.
- Event name – the event which your application intends to handle. In this case, select blob created event.
Logic App Action
Then click on Add new step button. In new step, you can search for “gmail” and select send email action. This action will also ask for login. After login is successful, you can enter details like to address for mail, subject, body of mail, etc.
Your logic app is complete. It will look like below snapshot:
Simulate Blob Created Event
Then you can use storage explorer for simulating blob creation. The storage explorer is a desktop based application. You can login to your Azure subscription. Then you can go to same storage account which you are using in logic app and then click on upload button to upload a file.
As soon as file is uploaded event will be triggered and the configured mail id will receive an email notification.
I hope this article provided you enough information to get started on Azure event grid and use azure services as event sources or event handlers. Please do comment and let me know your thoughts and experiences.