Quickstart: Route custom events to Azure Queue storage via Event Grid using Azure CLI

Azure Event Grid is a highly scalable and serverless event broker that you can use to integrate applications using events. Events are delivered by Event Grid to supported event handlers and Azure Queue storage is one of them. In this article, you use Azure CLI for the following steps:

  1. Create an Event Grid custom topic.
  2. Create an Azure Queue subscription for the custom topic.
  3. Send sample events to the custom topic.
  4. Verify that those events are delivered to Azure Queue storage.

If you don't have an Azure subscription, create an Azure free account before you begin.

Create a resource group

Event Grid topics are Azure resources, and must be placed in an Azure resource group. The resource group is a logical collection into which Azure resources are deployed and managed.

Create a resource group with the az group create command. The following example creates a resource group named gridResourceGroup in the westus2 location.

Note

Select Try it next to the CLI example to launch Cloud Shell in the right pane. Select Copy button to copy the command, paste it in the Cloud Shell window, and then press ENTER to run the command.

az group create --name gridResourceGroup --location westus2

Enable the Event Grid resource provider

If you haven't previously used Event Grid in your Azure subscription, you might need to register the Event Grid resource provider. Run the following command to register the provider:

az provider register --namespace Microsoft.EventGrid

It might take a moment for the registration to finish. To check the status, run:

az provider show --namespace Microsoft.EventGrid --query "registrationState"

When registrationState is Registered, you're ready to continue.

Create a custom topic

An Event Grid topic provides a user-defined endpoint that you post your events to. The following example creates the custom topic in your resource group. Replace <topic_name> with a unique name for your custom topic. The Event Grid topic name must be unique because it's represented by a DNS entry.

  1. Specify a name for the topic.

    topicname="<TOPIC NAME>"
    
  2. Run the following command to create the topic.

    az eventgrid topic create --name $topicname -l westus2 -g gridResourceGroup
    

Create Queue storage

Before subscribing to the custom topic, let's create the endpoint for the event message. You create a Queue storage for collecting the events.

  1. Specify a unique name for the Azure Storage account.

    storagename="<STORAGE ACCOUNT NAME>"    
    
  2. Run the following commands to create an Azure Storage account and a queue (named eventqueue) in the storage.

    queuename="eventqueue"
    
    az storage account create -n $storagename -g gridResourceGroup -l westus2 --sku Standard_LRS
    key="$(az storage account keys list -n $storagename --query "[0].{value:value}" --output tsv)"    
    az storage queue create --name $queuename --account-name $storagename --account-key $key
    

Subscribe to a custom topic

The following example subscribes to the custom topic you created, and passes the resource ID of the Queue storage for the endpoint. With Azure CLI, you pass the Queue storage ID as the endpoint. The endpoint is in the format:

/subscriptions/<AZURE SUBSCRIPTION ID>/resourcegroups/<RESOURCE GROUP NAME>/providers/Microsoft.Storage/storageAccounts/<STORAGE ACCOUNT NAME>/queueservices/default/queues/<QUEUE NAME>

The following script gets the resource ID of the storage account for the queue. It constructs the ID for the queue storage, and subscribes to an Event Grid topic. It sets the endpoint type to storagequeue and uses the queue ID for the endpoint.

Important

Replace expiration date placeholder (<yyyy-mm-dd>) with an actual value. For example: 2022-11-17 before running the command.

storageid=$(az storage account show --name $storagename --resource-group gridResourceGroup --query id --output tsv)
queueid="$storageid/queueservices/default/queues/$queuename"
topicid=$(az eventgrid topic show --name $topicname -g gridResourceGroup --query id --output tsv)

az eventgrid event-subscription create \
  --source-resource-id $topicid \
  --name mystoragequeuesubscription \
  --endpoint-type storagequeue \
  --endpoint $queueid \
  --expiration-date "<yyyy-mm-dd>"

The account that creates the event subscription must have write access to the queue storage. Notice that an expiration date is set for the subscription.

If you use the REST API to create the subscription, you pass the ID of the storage account and the name of the queue as a separate parameter.

"destination": {
  "endpointType": "storagequeue",
  "properties": {
    "queueName":"eventqueue",
    "resourceId": "/subscriptions/<subscription-id>/resourcegroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-name>"
  }
  ...

Send an event to your custom topic

Let's trigger an event to see how Event Grid distributes the message to your endpoint. First, let's get the URL and key for the custom topic.

endpoint=$(az eventgrid topic show --name $topicname -g gridResourceGroup --query "endpoint" --output tsv)
key=$(az eventgrid topic key list --name $topicname -g gridResourceGroup --query "key1" --output tsv)

To simplify this article, you use sample event data to send to the custom topic. Typically, an application or Azure service would send the event data. CURL is a utility that sends HTTP requests. In this article, you use CURL to send the event to the custom topic. The following example sends three events to the Event Grid topic:

for i in 1 2 3
do
   event='[ {"id": "'"$RANDOM"'", "eventType": "recordInserted", "subject": "myapp/vehicles/motorcycles", "eventTime": "'`date +%Y-%m-%dT%H:%M:%S%z`'", "data":{ "make": "Ducati", "model": "Monster"},"dataVersion": "1.0"} ]'
   curl -X POST -H "aeg-sas-key: $key" -d "$event" $endpoint
done

Navigate to the Queue storage in the portal, and notice that Event Grid sent those three events to the queue.

Screenshot showing the list of messages in the queue that are received from Event Grid.

Note

If you use an Azure Queue storage trigger for Azure Functions for a queue that receives messages from Event Grid, you may see the following error message on the function execution: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

The reason is that when you use an Azure Queue storage trigger, Azure Functions expect a base64 encoded string, but Event Grid sends messages to a storage queue in a plain text format. Currently, it's not possible to configure the queue trigger for Azure Functions to accept plain text.

Clean up resources

If you plan to continue working with this event, don't clean up the resources created in this article. Otherwise, use the following command to delete the resources you created in this article.

az group delete --name gridResourceGroup

Next steps

Now that you know how to create topics and event subscriptions, learn more about what Event Grid can help you do:

See the following samples to learn about publishing events to and consuming events from Event Grid using different programming languages.