Create Event Grid custom topic with Azure CLI

This script creates an Event Grid custom topic.

To run this sample, install the latest version of the Azure CLI. To start, run az login to create a connection with Azure.

Samples for the Azure CLI are written for the bash shell. To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

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

Sample script

#!/bin/bash

# Give your custom topic a unique name
myTopic=demoContosoTopic

# Provice name for resource group
myResourceGroup=demoResourceGroup

# Select the Azure subscription to contain the custom topic.
az account set --subscription "<name or ID of the subscription>"

# Create resource group
az group create --name $myResourceGroup --location westus2

# Create custom topic
az eventgrid topic create --resource-group $myResourceGroup --name $myTopic --location westus2

# Retrieve endpoint and key to use when publishing to the topic
endpoint=$(az eventgrid topic show --name $myTopic -g $myResourceGroup --query "endpoint" --output tsv)
key=$(az eventgrid topic key list --name $myTopic -g $myResourceGroup --query "key1" --output tsv)

echo $endpoint
echo $key

Script explanation

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

Command Notes
az eventgrid topic create Create an Event Grid custom topic.

Next steps