VOOZH about

URL: https://thenewstack.io/tutorial-exploring-azure-event-grid-custom-webhooks/

⇱ Tutorial: Exploring Azure Event Grid with Custom Webhooks - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2018-02-09 10:00:39
Tutorial: Exploring Azure Event Grid with Custom Webhooks
analysis,tutorial,
Microservices / Serverless / Software Development

Tutorial: Exploring Azure Event Grid with Custom Webhooks

Feb 9th, 2018 10:00am by Janakiram MSV
👁 Featued image for: Tutorial: Exploring Azure Event Grid with Custom Webhooks
Feature image via Pixabay.

Azure Event Grid is a managed event routing service based on the publish-subscribe protocol. It enables developers to easily connect event publishers with consumers. In August 2017, Microsoft launched Event Grid service in preview. Last week, it became generally available across 10 Azure regions.

For an overview of Azure EventGrid, refer to my article published in October 2017. Since the initial preview, Microsoft has added multiple event sources and destinations including Azure IoT Hub and Azure Event Hubs. The service now comes with 99.99 percent availability with a financially backed SLA.

Apart from consuming the events generated by Azure resources, developers can use Event Grid as a generic messaging infrastructure. This capability enables serverless applications and microservices to seamlessly communicate with each other.

One of the consumers of Event Grid messages is a custom WebHook. With this integration, it is possible to trigger events running in a variety of environments including Functions as a Service (FaaS) or custom REST endpoints running behind firewalls.

In this tutorial, we will explore a scenario where an on-premises web service gets a notification each time a file is uploaded or deleted from an Azure Storage Container. We will use this scenario to understand the schema of Azure Event Grid subscriptions. Though the REST endpoint is based on Node.js Express framework, it can be easily extended to Serverless environments such as Azure Functions or Apache OpenWhisk.

👁 Image

Before we get started with the step-by-step guide, make sure that you have an active Azure subscription. You also need to have the latest version of Azure CLI, ngrok and node.js installed on your machine.

Let’s start by setting up the environment. Open a new terminal window and start ngrok on port 8000. This will open an HTTPS tunnel to your local machine making it possible to receive messages from Azure Event Grid.

$ ngrok http 8000

👁 Image

Open another terminal window, and run the following commands to set the environment variables. Don’t forget to replace the END_POINT variable with the actual ngrok URL.

$ LOC=westcentralus
$ RG=aegdemo
$ AZURE_STORAGE_ACCOUNT=input7675
$ END_POINT=https://e7fa39a5.ngrok.io

To ensure that Microsoft.EventGrid is registered in your account, run the below commands.

$ az provider register --namespace  Microsoft.EventGrid
$ az feature register --name storageEventSubscriptions --namespace Microsoft.EventGrid
$ az feature show --name storageEventSubscriptions --namespace Microsoft.EventGrid

You should see the output similar to the following. This confirms that Azure EventGrid is available for your subscription.

{
 "id": "/subscriptions/<subscription_id>/providers/Microsoft.Features/providers/Microsoft.EventGrid/features/storageEventSubscriptions",
 "name": "Microsoft.EventGrid/storageEventSubscriptions",
 "properties": {
   "state": "Registered"
 },
 "type": "Microsoft.Features/providers/features"
}

Let’s go ahead a create an Azure Resource Group for the resources that we will create.

$ az group create --name $RG  --location $LOC
{
 "id": "/subscriptions/<subscription_id>/resourceGroups/aegdemo",
 "location": "westcentralus",
 "managedBy": null,
 "name": "aegdemo",
 "properties": {
   "provisioningState": "Succeeded"
 },
 "tags": null
}

Next, we will create an Azure Storage account for storing blobs. The JSON output confirms that the storage account is successfully created.

$ az storage account create \
 --name $AZURE_STORAGE_ACCOUNT \
 --location $LOC \
 --resource-group $RG \
 --sku Standard_LRS \
 --kind BlobStorage \
 --access-tier Hot

Before we go any further, it’s time for us to launch the Node.js program that will start listening for the requests routed via ngrok. You also need to install Express framework before executing the code.

'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var webhook_res=""
var app = express();
app.use(bodyParser.json());
app.post('/',function (request, response) {
//  console.log(request.body);
 if('data' in request.body[0]){
   if('validationCode' in request.body[0].data) {
     webhook_res = {'validationResponse': request.body[0].data.validationCode}
     console.log('Azure EventGrid subscription successfully validated')
     response.send(webhook_res);
   }
     if(request.body[0].data.api=='PutBlob'){
       console.log('>> Blob uploaded - %s', request.body[0].data.url)
     }
     if(request.body[0].data.api=='DeleteBlob'){
       console.log('>> Blob deleted - %s', request.body[0].data.url)
     }
     response.send();
     response.end();
   }
});
var server = app.listen(process.env.PORT || '8000', function () {
 console.log('App listening on port %s', server.address().port);
 console.log('Press Ctrl+C to quit.');
});

Save and run the above code in a new terminal window:

$ node app.js
App listening on port 8000
Press Ctrl+C to quit.

The crux of this program lies in the POST handler that deals with the WebHook requests hitting the endpoint.

There are three types of requests that this application will handle —

  1. Subscription validation
  2. Blob creation
  3. Blob deletion

When we register our own WebHook endpoint with Event Grid, it sends a POST request with a simple validation code in order to prove endpoint ownership. The app needs to respond by echoing back the validation code. Event Grid does not deliver events to WebHook endpoints that have not passed the validation.

To enable this validation, we check for validationCode within the body and embed the same code in the response. The following code snippet is responsible for this validation.

   if('validationCode' in request.body[0].data) {
     webhook_res = {'validationResponse': request.body[0].data.validationCode}
     console.log('Azure EventGrid subscription successfully validated')
     response.send(webhook_res);
   }

Subsequent requests generated by Azure Event Grid will not contain the validation code. It’s only required during the subscription. Once the subscription is made, we will receive requests when a new blob is uploaded or an existing blob is deleted. The code snippets shown below tackle these events.

     if(request.body[0].data.api=='PutBlob'){
       console.log('>> Blob uploaded - %s', request.body[0].data.url)
     }
     if(request.body[0].data.api=='DeleteBlob'){
       console.log('>> Blob deleted - %s', request.body[0].data.url)
     }

In our sample, we simply print the URL of the blob. This scenario can be easily extended to powerful use cases such as backing up the blobs to the local storage or to other cloud storage environments.

With the WebHook in place, it’s time for us to create an Event Grid subscription.

$ az eventgrid resource event-subscription create \
--endpoint $END_POINT  \
--name aegdemosub \
--provider-namespace Microsoft.Storage \
--resource-type storageAccounts \
--resource-group $RG \
--resource-name $AZURE_STORAGE_ACCOUNT

Notice that we are instructing Event Grid to handle events raised by storage accounts.

When you run the above command, our Node.js app responds to show the following output.

$ node app.js
App listening on port 8000
Press Ctrl+C to quit.
Azure EventGrid subscription successfully validated

You will also see the JSON payload emitted by the Azure CLI. This indicates that the subscription is successfully registered with the WebHook.

We will now create a container in Azure Storage to hold the uploaded files. To complete this step, we need to store the access key in the AZURE_STORAGE_ACCESS_KEY environment variable. The below commands handle these steps.

$ export AZURE_STORAGE_ACCOUNT=input7675
$ export AZURE_STORAGE_ACCESS_KEY="$(az storage account keys list --account-name $AZURE_STORAGE_ACCOUNT --resource-group $RG --query "[0].value" --output tsv)"
$ az storage container create --name aegdemo
{
 "created": true
}

With the storage container in place, let’s go ahead and upload a file to Azure Storage.

$ echo "Hello Azure Event Grid" > hello.txt
$ az storage blob upload --file hello.txt --container-name aegdemo --name hello.txt
Alive[################################################################]  100.000Finished[#############################################################]  100.0000%
{
 "etag": "\"0x8D568A4964D19FD\"",
 "lastModified": "2018-01-31T12:17:24+00:00"
}

Now, check the terminal window running the Node.js application. It prints the URL of the blob.

App listening on port 8000
Press Ctrl+C to quit.
Azure EventGrid subscription successfully validated
>> Blob uploaded - https://input7675.blob.core.windows.net/aegdemo/hello.txt

Let’s go ahead and delete the blob.

$ az storage blob delete --container-name aegdemo --name hello.txt
{
 "deleted": null
}

Don’t forget the to check the output of the application. You will see the response to all the three requests — subscription validation, blob creation, and blob deletion.

👁 Image

Perform clean up by running the below commands.

# Delete the blob container
$ az storage container delete -n aegdemo
# Delete AEG subscription
$ az eventgrid resource event-subscription delete \
--name aegdemosub \
--provider-namespace Microsoft.Storage \
--resource-type storageAccounts \
--resource-group $RG \
--resource-name $AZURE_STORAGE_ACCOUNT
# Delete Azure Storage account
$ az storage account delete \
--name $AZURE_STORAGE_ACCOUNT \
--resource-group $RG
# Delete Azure Resource Group
$ az group delete -n $RG

The objective of this tutorial is to walk you through all the steps involved in creating an Azure Event Grid subscription and consuming the events through a custom WebHook. I chose the CLI route to highlight the workflow involved in this setup.

In one of the upcoming tutorials, I will demonstrate how to extend this scenario to create a serverless, multi-cloud replication of Azure Storage accounts. Stay tuned!

TRENDING STORIES
Janakiram MSV (Jani) is a practicing architect, research analyst, and advisor to Silicon Valley startups. He focuses on the convergence of modern infrastructure powered by cloud-native technology and machine intelligence driven by generative AI. Before becoming an entrepreneur, he spent...
Read more from Janakiram MSV
SHARE THIS STORY
TRENDING STORIES
Microsoft is a sponsor of The New Stack.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.