We have enough references telling what an Azure Event Grid is. If you are not aware of it, please search and read about it. This article is more focused on creating an Event Grid Subscription and seeing the output. The underlined architectural pattern used for Event Grid is a publish–subscribe pattern. The advantage of that is that one publisher can publish a "Topic" and multiple subscribers of the same "Topic" can receive messages related to that "Topic". So, a one to many relation is there. It's different from queue messages where only one specific subscriber can accept queue messages, and this is a one to one relation.
One example of the publish–subscribe pattern is, if customers of an online food delivery company have subscribed to a "Topic" like "daily deals", then all those customers will get SMS messages about daily deals. Here, SMS isn't sent to a specific person but to the whole community whoever subscribed to the "Topic" named "daily deals". Now, let us go to the code directly.
We need to create supporting services first. Below are the supporting services I created for this article.
Create Storage Account for Event Source
Whenever a BLOB is added or deleted, an event will be fired which will be captured inside our proposed Event Grid. I created a normal BLOB storage account, eventgridsourcesa, and there's nothing special in it.
Create Node Function App for Event Target
The above-mentioned event will be transferred to a Function App by our proposed Event Grid. This Function App has the below code written to log the event details.
- module.exports = async function (context, eventGridEvent) {
- context.log("JavaScript Event Grid function processed a request.");
- context.log("Subject: " + eventGridEvent.subject);
- context.log("Time: " + eventGridEvent.eventTime);
- context.log("Data: " + JSON.stringify(eventGridEvent.data));
- context.done();
- };
Create the Proposed Event Grid
You need to choose the below options from the Azure portal.
Under the "TOPIC DETAILS" section, choose your event source storage account created above.
For "EVENT TYPES" section, I opted only 2 as below.
Now comes the "ENDPOINT DETAILS" section to choose based on our target function app created above. Endpoint type is "Webhook" and I have selected my target function app URL as the endpoint URL.
After your Event Grid deployment, you need to properly apply the filter to make it display.
Test the proposed Event Grid
I am deleting a BLOB in my event source storage account.
Notice the below logs under your target function app. These details are displaying as per your function app code that you have written.
- 2019-07-23T16:46:26 Welcome, you are now connected to log-streaming service.
-
- 2019-07-23T16:46:53.584 [Information] Executing 'Functions.EventGridTargetFunction' (Reason='EventGrid trigger fired at 2019-07-23T16:46:53.5845518+00:00', Id=7fd394e4-a5c2-49f1-8e7b-47eb0022fadd)
- 2019-07-23T16:46:53.591 [Information] JavaScript Event Grid function processed a request.
- 2019-07-23T16:46:53.592 [Information] Subject: /blobServices/default/containers/test/blobs/New Text Document.txt
- 2019-07-23T16:46:53.593 [Information] Time: 2019-07-23T16:46:52.698093Z
- 2019-07-23T16:46:53.593 [Information] Data: {"api":"DeleteBlob","requestId":"27a74301-f01e-008b-7776-415f3a000000","eTag":"0x8D70F8D5D56AB42","contentType":"text/plain","contentLength":13,"blobType":"BlockBlob","url":"https://eventgridsourcesa.blob.core.windows.net/test/New Text Document.txt","sequencer":"000000000000000000000000000047D60000000003b68c26","storageDiagnostics":{"batchId":"ea971000-5006-0041-0076-41ccf7000000"}}
- 2019-07-23T16:46:53.593 [Information] Executed 'Functions.EventGridTargetFunction' (Succeeded, Id=7fd394e4-a5c2-49f1-8e7b-47eb0022fadd)
Now, I am uploading a BLOB to my source storage account.
See the logs now produced against putting a new BLOB.
- 2019-07-23T16:52:33 Welcome, you are now connected to log-streaming service.
-
- 2019-07-23T16:52:56.381 [Information] Executing 'Functions.EventGridTargetFunction' (Reason='EventGrid trigger fired at 2019-07-23T16:52:56.3811180+00:00', Id=4f7a09bb-515d-4d63-b2fe-2100288d1023)
- 2019-07-23T16:52:56.390 [Information] JavaScript Event Grid function processed a request.
- 2019-07-23T16:52:56.391 [Information] Subject: /blobServices/default/containers/test/blobs/New Text Document.txt
- 2019-07-23T16:52:56.391 [Information] Time: 2019-07-23T16:52:55.4714185Z
- 2019-07-23T16:52:56.391 [Information] Data: {"api":"PutBlockList","clientRequestId":"69303a3c-8c34-4455-9eec-e6e43877905e","requestId":"d0233b99-f01e-0080-1d77-41474e000000","eTag":"0x8D70F8E35915536","contentType":"text/plain","contentLength":13,"blobType":"BlockBlob","url":"https://eventgridsourcesa.blob.core.windows.net/test/New Text Document.txt","sequencer":"000000000000000000000000000047D60000000003b80e3a","storageDiagnostics":{"batchId":"ea9b2bbc-5006-0041-0077-41ccf7000000"}}
- 2019-07-23T16:52:56.391 [Information] Executed 'Functions.EventGridTargetFunction' (Succeeded, Id=4f7a09bb-515d-4d63-b2fe-2100288d1023)
So, our Event Grid is processing the events as expected. You can see the operation summary inside Event Grid namespace as well.
Now, the same Topic can be added to another subscription and there also, it will get the same results. Now as a continuation of this, you should read about custom Topics too. In this article, we used built-in Storage Account as a Topic, but in the real world, you always need a custom Topic.
Hope this article has given you all the basic inputs to proceed with more complex scenarios in the Azure Event Grid.