Create custom topic and subscribe to events for an Azure subscription with Azure CLI

This article provides a sample Azure CLI script that shows how to create a custom topic and send an event to the custom topic using Azure CLI.

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

Prerequisites

Sample script

Launch Azure Cloud Shell

The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account.

To open the Cloud Shell, just select Try it from the upper right corner of a code block. You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com.

When Cloud Shell opens, verify that Bash is selected for your environment. Subsequent sessions will use Azure CLI in a Bash environment, Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press Enter to run it.

Sign in to Azure

Cloud Shell is automatically authenticated under the initial account signed-in with. Use the following script to sign in using a different subscription, replacing <Subscription ID> with your Azure Subscription ID. If you don't have an Azure subscription, create an Azure free account before you begin.

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

For more information, see set active subscription or log in interactively

Run the script

# Create Event Grid custom topic

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
subscriptionId="$(az account show --query id -o tsv)"
resourceGroup="msdocs-event-grid-rg-$randomIdentifier"
tag="event-grid"
topic="msdocs-event-grid-topic-$randomIdentifier"
site="msdocs-event-grid-site-$randomIdentifier"
eventSubscription="msdocs-event-subscription-$randomIdentifier"
webappEndpoint="https://$site.azurewebsites.net/api/updates"
storage="msdocsstorage$randomIdentifier"

# Create a resource group
echo "Creating  in "$location"..."
az group create --name $resourceGroup --location "$location" --tag $tag

# Enable and then show the Event Grid resource provider
az provider register --namespace Microsoft.EventGrid
az provider show --namespace Microsoft.EventGrid --query "registrationState"

# Create custom topic
echo "Creating $topic"
az eventgrid topic create \
--resource-group $resourceGroup \
--name $topic \
--location "$location"

# Create a message endpoint
echo "Creating $site"
az deployment group create \
  --resource-group $resourceGroup \
  --template-uri "https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/master/azuredeploy.json" \
  --parameters siteName=$site hostingPlanName=viewerhost

# To view your web app, navigate to https://<your-site-name>.azurewebsites.net

# Subscribe to custom topic
az eventgrid event-subscription create \
  --source-resource-id "/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.EventGrid/topics/$topic" \
  --name demoViewerSub \
  --endpoint $webappEndpoint

# View your web app again to see the subscription validation event.
# Select the eye icon to expand the event data
  
# Send an event to your custom topic
url=$(az eventgrid topic show --name $topic -g $resourceGroup --query "endpoint" --output tsv)
key=$(az eventgrid topic key list --name $topic -g $resourceGroup --query "key1" --output tsv)
echo $url
echo $key
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" $url

# View your web app again to see the event that you just sent

Clean up resources

Use the following command to remove the resource group and all resources associated with it using the az group delete command - unless you have an ongoing need for these resources. Some of these resources may take a while to create, as well as to delete.

az group delete --name $resourceGroup

Sample reference

This script uses the following command to create the event subscription. Each command in the table links to command-specific documentation.

Command Notes
az eventgrid event-subscription create Create an Event Grid subscription.
az eventgrid event-subscription create - extension version Create an Event Grid subscription.

Next steps