Getting Started with Azure Event Grid: A Comprehensive Guide

Abhimanyubajaj
4 min readNov 6, 2023

--

Introduction

Azure Event Grid is a highly scalable, fully managed Pub Sub message distribution service that offers flexible message consumption patterns using the MQTT and HTTP protocols. Event Grid provides durable delivery. It tries to deliver each event at least once for each matching subscription immediately. If a subscriber’s endpoint doesn’t acknowledge receipt of an event or if there’s a failure, Event Grid retries delivery based on a fixed retry schedule and retry policy. By default, Event Grid delivers one event at a time to the subscriber, and the payload is an array with a single event.

Azure simplifies event consumption and lowers costs by eliminating the need for constant polling. Event Grid efficiently and reliably routes events from Azure and non-Azure resources and distributes the events to registered subscriber endpoints.

Pre-requisite->

We would need an Azure Resource group, A Web App that will be hosting a python application. To deploy ->

git clone https://github.com/Abhimanyu9988/azure-event-grid.git
cd azure-event-grid
terraform init
terraform apply --auto-approve

Once this is done, You should be able to check your python application on portal.azure.com. Search for App Services -> Web App.

Great work!! Click on “Default domain” and you should see “Hello, Azure Web App!”.
We will be sending event_grid push to <Default_domain>/event_data. You should see a default event_data already present though.

Let’s create Azure Event grid components now.

Before, Proceeding to creating Azure Event grid, Run below that you will get by cloning https://github.com/Abhimanyu9988/azure-event-grid.git

./pre-req.sh

This will make sure all pre-requisite are met before we create Azure Event grid.

Create Event Grid topic

  1. Go to portal.azure.com, In the search bar type Azure Event Grid.

2. We will be creating a Custom Topic, I will be using subscription and resource group that got created using my Terraform with a custom name.

3. You can let everything as it is and select Review + Create. A Validation screen will show up, Click Create.

Create Event Topic Subscription

  1. Navigate to Entities -> Event Subscription and Add an Event Subscription.

2. Use a custom name, In Endpoint type, Select “Web Hook”. Under Endpoint section use <Default_domain_of_your_web_app>/event_data. Click Confirm Selection -> Create.

It will take few minutes for everything to propagate. In the meantime, Open your CLI and we will create a custom event.

Custom Event

In your local CLI, Run->

event='[ {"id": "'"$RANDOM"'", "eventType": "recordInserted", "subject": "myapp/vehicles/motorcycles", "eventTime": "'`date +%Y-%m-%dT%H:%M:%S%z`'", "data":{ "make": "Contoso", "model": "Monster"},"dataVersion": "1.0"} ]'

This is the JSON event we will be pushing to our WebApp event_data url.
Before making the request to our AzureEvent Topic, We would need the aeg-sas-key

Azure Event Grid Shared Access Signature Key is a security key used for authentication and authorization in Azure Event Grid to grant access to specific resources and actions. This key allows applications or services to interact with Azure Event Grid securely and perform actions such as sending events, subscribing to topics, and managing resources.

To retrieve that, Run below from your local Cli->

az eventgrid topic key list --name <EventGrid_topic_name> -g <Resource_group_name> --query "key1" --output tsv

For instance, In my case it is->

az eventgrid topic key list --name azure-first-event -g azure-events-rg --query "key1" --output tsv
Output : I/CpTGoiAju3cplwEOnGmFl+cDWL8QnOvJQt32FQBos=

Once you have the key, Run below command to send custom event data to your WebApp/event_data domain ->

curl -X POST -H "aeg-sas-key: I/CpTGoiAju3cplwEOnGmFl+cDWL8QnOvJQt32FQBos=" -H "Content-Type: application/json" -d "$event" https://azure-events-web-ovlrd4.azurewebsites.net/event_data

Good work!! Now if you go to your <WebApp_domain>/event_data url, you will see

Viola!! Great work everyone. You have succesfully created an Azure Event grid service and tested it out with your custom python code :)

For more information or in case you get stuck, Feel free to connect with me on LinkedIn

https://www.linkedin.com/in/theabhibajaj/

--

--

Abhimanyubajaj

I solve problems. CKAD, CKA, Azure, AWS, GCP, Terraform Certified. Senior Software Engineer at Cisco.