Where are you using WebAssembly?
Wasm promises to let developers build once and run anywhere. Are you using it yet?
At work, for production apps
At work, but not for production apps
I don’t use WebAssembly but expect to when the technology matures
I have no plans to use WebAssembly
No plans and I get mad whenever I see the buzzword
Microservices / Serverless / Software Development

Tutorial: Exploring Azure Event Grid with Custom Webhooks

Feb 9th, 2018 10:00am by
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.

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.

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.

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

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

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

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

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.

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

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.

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.

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.

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.

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.

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

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

Let’s go ahead and delete the blob.

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.

Perform clean up by running the below commands.

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!

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.