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 a trial account before you begin.

Prerequisites

  • If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.

    • If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.

    • When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.

    • Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.

Sample script

Run the script

# Create Event Grid custom topic

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="chinanorth2"
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.chinacloudsites.cn/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>.chinacloudsites.cn

# 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.

Next steps