This wiki article provides an example of using Azure Event Grid by providing an example of posting a message to an Event Grid Topic and using a Subscription to create a new queue item. This example will use a combination of Azure CLI commands, the Azure Portal, and Postman. The purpose of the example is to show the simplicity of using Event Grid to react to events in Azure resources as well using a custom topic to show how Event Grid can be extended. This article does assume some basic understanding of Event Grid terminology. Please see the community post Event Grid for background. Note: You will most likely find that you need to create a unique topic name and storage name. These are unique as they are used to generate the endpoint for the created resource.
Azure CLI is a great way to issue commands to an Azure subscription. These commands can be issued in a local Bash or Powershell command shell, or the Azure Cloud Shell. The following instructions were used in a local command shell so the first step is to set the Azure Subscription. In the local command shell, issue the following to verify you have Azure CLI installed and the version is equal to or more recent than 2.2.0:
az --version
Invoke-WebRequest -Uri https:
//aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'
az login
az account set --subscription
"{your subscription id}"
We will refer to the Resource Group multiple times so we will issue two commands. The first creates a variable to contain the name of the resource group while the second creates the group:
$rg=
"TechNetEventGrid"
az group create --name $rg --location southeastasia
Azure Storage is a resource that has multiple features. For this example, we will look at blob storage and queues. Blob storage provides a repository for storing unstructured objects. Storage queues provide a messaging platform. First, let's create a new storage account:
$storage=
"technetsamplestorage"
az storage account create --name $storage --location southeastasia --resource-group $rg --sku Standard_LRS
$storageid=$(az storage account show --name $storage --resource-group $rg --query id --output tsv)
$queuename=
"queue1"
az storage queue create --name $queuename --account-name $storage
$queueid=
"$storageid/queueservices/default/queues/$queuename"
An Event Grid Topic will be used to receive a http post.
$topic=
"TechNetEventGrid-tpc"
az eventgrid topic create --resource-group $rg --name $topic --location southeastasia
$endpoint=$(az eventgrid topic show --name $topic -g $rg --query
"endpoint"
--output tsv)
$key=$(az eventgrid topic key list --name $topic -g $rg --query
"key1"
$topicid=$(az eventgrid topic show --name $topic -g $rg --query id --output tsv)
With events being received, we now need a subscription. The subscription will direct an event towards a handler. In this example we will create a subscription to send the event to the queue we created earlier (Note: update the expiration date value to be in the future if you are running these steps after May 25th):
az eventgrid event-subscription create --source-resource-id $topicid --name TopicToStorageQueue-sub --endpoint-type storagequeue --endpoint $queueid --expiration-date
"2020-05-25"
In the created resource group, TechNetEventGrid, we can see to resources that have been created: If we look at the Event Grid Topic, TechNetEventGrid-tpc, we can see that there is one subscription, TopicToStorageQueue-sub. This is indicated by the arrow below: Let's use Postman to create some activity next.
In Postman, let's create a new request. We need the endpoint we are going to post to and the key. Both of these we captured in the steps previously and their values are in $endpoint and $key. In Post, we will put the $endpoint value as the url and be sure to set the request to POST: A header needs to be sent that contains the $key value as shown below:
[
{
"id"
:
"1231123123q2312"
,
"eventType"
"a value to indicate the type ofevent"
"subject"
"a subject that this event is about"
"eventTime"
"2020-03-17T15:12:13Z"
"data"
: {
"note"
"the data section can be any valid json so this is where you want to extend"
},
"dataVersion"
"1.0"
}
]
"error"
"code"
"Unauthorized"
"message"
"Request must contain one of the following authorization signature: aeg-sas-token, aeg-sas-key. Report 'fe5d5108-383c-430c-b054-64a79322d4c7:4:3/17/2020 2:10:27 AM (UTC)' to our forums for assistance or raise a support ticket."
"details"
: [
Back in the Portal, we can now see some activity. At the bottom of the graph are three values: Published Events, Publish Failed Events, Unmatched Events.
If the subscription is selected below the graph, you will be taken to a view that shows the handling of the messages by the subscription: This view shows the following information:
This article pulled content from several docs: