Skip to content

Latest commit

 

History

History
144 lines (95 loc) · 7.66 KB

File metadata and controls

144 lines (95 loc) · 7.66 KB
title Quickstart: Send custom events to an event hub - Event Grid, Azure CLI
description Learn how to use Azure Event Grid and the Azure CLI to publish a topic and subscribe to that event, by using an event hub for the endpoint.
ms.date 06/24/2024
ms.topic quickstart
ms.custom devx-track-azurecli, mode-api

Quickstart: Route custom events to an event hub by using Event Grid and the Azure CLI

In this quickstart, you use the Azure CLI to create an Event Grid custom topic and an Event Hubs subscription for that topic. You then send sample events to the custom topic and verify that those events are delivered to an event hub.

Azure Event Grid is a highly scalable and serverless event broker that you can use to integrate applications via events. Event Grid delivers events to supported event handlers, such as Azure Event Hubs. Event handlers.

[!INCLUDE quickstarts-free-trial-note.md]

Create a resource group

Event Grid topics are Azure resources. Create them in an Azure resource group. The resource group is a logical collection in which Azure resources are deployed and managed.

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

Select Open Cloud Shell to open Azure Cloud Shell on the right pane. Select the Copy button to copy the command, paste it in Cloud Shell, and then select the Enter key to run the command.

az group create --name gridResourceGroup --location westus2

[!INCLUDE register-provider-cli.md]

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 a Domain Name System (DNS) entry represents it.

  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 --location westus2 --resource-group gridResourceGroup
    

Create an event hub

Before you subscribe to the custom topic, create the endpoint for the event message. You create an event hub for collecting the events.

  1. Specify a unique name for the Event Hubs namespace:

    namespace="<EVENT HUBS NAMESPACE NAME>"
    
  2. Run the following commands to create an Event Hubs namespace and an event hub named demohub in that namespace:

    hubname=demohub
    
    az eventhubs namespace create --name $namespace --resource-group gridResourceGroup
    az eventhubs eventhub create --name $hubname --namespace-name $namespace --resource-group gridResourceGroup
    

Subscribe to a custom topic

You subscribe to an Event Grid topic to tell Event Grid which events you want to track. The following example subscribes to the custom topic that you created. It passes the resource ID of the event hub for the endpoint. The endpoint is in this format:

/subscriptions/<AZURE SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.EventHub/namespaces/<NAMESPACE NAME>/eventhubs/<EVENT HUB NAME>

The following script gets the resource ID for the event hub and subscribes to an Event Grid topic. It sets the endpoint type to eventhub and uses the event hub ID for the endpoint.

hubid=$(az eventhubs eventhub show --name $hubname --namespace-name $namespace --resource-group gridResourceGroup --query id --output tsv)
topicid=$(az eventgrid topic show --name $topicname -g gridResourceGroup --query id --output tsv)

az eventgrid event-subscription create \
  --source-resource-id $topicid \
  --name subtoeventhub \
  --endpoint-type eventhub \
  --endpoint $hubid

The account that creates the event subscription must have write access to the event hub.

Send an event to your custom topic

Trigger an event to see how Event Grid distributes the message to your endpoint. First, 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)

For simplicity, this article uses sample event data to send to the custom topic. Typically, an application or an Azure service would send the event data.

The cURL tool 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

In the Azure portal, the Overview page for your Event Hubs namespace shows that Event Grid sent those three events to the event hub. You see the same chart on the Overview page for the demohub Event Hubs instance.

:::image type="content" source="./media/custom-event-to-eventhub/show-result.png" lightbox="./media/custom-event-to-eventhub/show-result.png" alt-text="Screenshot that shows the portal page with an incoming message count of 3.":::

Typically, you create an application that retrieves event messages from the event hub. For more information, see:

Clean up resources

If you plan to continue working with this custom topic and subscription, don't remove the resources that you created in this article. Otherwise, use the following command to delete the resources:

az group delete --name gridResourceGroup

Related content

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

To learn about publishing events to, and consuming events from, Event Grid by using various programming languages, see the following samples: