Use the Azure CLI to Create an Event Hub
You're now ready to create a new Event Hub. After creating the Event Hub, you'll use the Azure portal to view your new hub.
Make sure you activate the sandbox before you continue.
Set some defaults in the Azure CLI
Let's start by providing some default values for the Azure CLI in the Cloud Shell. This will keep you from having to type these in every time. In particular, let's set the resource group and location. Select a location from the following list.
The free sandbox allows you to create resources in a subset of Azure's global regions. Select a region from the following list when creating any resources:
Then type the following command into the Azure CLI, make sure to replace the location with one close to you.
az configure --defaults group=<rgn>[sandbox Resource Group]</rgn> location=westus2
Create an Event Hubs namespace
Use the following steps to create an Event Hubs namespace using bash shell supported by Azure Cloud shell:
Create the Event Hubs namespace using the
az eventhubs namespace createcommand. Use the following parameters.
Parameter Description --name (required) Enter a 6-50 characters-long unique name for your Event Hubs namespace. The name should contain only letters, numbers, and hyphens. It should start with a letter and end with a letter or number. --resource-group (required) This will be the pre-created Azure sandbox resource group supplied from the defaults. --l (optional) Enter the location of your nearest Azure datacenter, this will use your default. --sku (optional) The pricing tier for the namespace [Basic Standard], defaults to Standard. This determines the connections and consumer thresholds.
Set the name into an environment variable so we can reuse it.
You can use the Copy button to copy commands to the clipboard. To paste, right click on a new line in the Cloud Shell window and select Paste or use the Shift+Insert keyboard shortcut (⌘+V on macOS).
az eventhubs namespace create --name $NS_NAME
Azure is very picky about the name and the CLI returns Bad Request if the name exists or is invalid. Try a different name by changing your environment variable and reissuing the command.
Fetch the connection string for your Event Hubs namespace using the following command. You'll need this to configure applications to send and receive messages using your Event Hub.
az eventhubs namespace authorization-rule keys list --name RootManageSharedAccessKey --namespace-name $NS_NAME
Parameter Description --resource-group (required) This will be the pre-created Azure sandbox resource group supplied from the defaults. --namespace-name (required) Enter the name of the namespace you created.
This command returns a JSON block with the connection string for your Event Hubs namespace that you'll use later to configure your publisher and consumer applications. Save the value of the following keys for later use.
Create an Event Hub
Use the following steps to create your new Event Hub:
Create a new Event Hub using the
eventhub createcommand. It needs the following parameters:
Parameter Description --name (required) Enter a name for your Event Hub. --resource-group (required) Resource group owner. --namespace-name (required) Enter the namespace you created.
Let's define the Event Hub name in an environment variable first.
az eventhubs eventhub create --name $HUB_NAME --namespace-name $NS_NAME
View the details of your Event Hub using the
eventhub showcommand. It takes:
Parameter Description --resource-group (required) Resource group owner. --namespace-name (required) Enter the namespace you created. --name (required) Name of the Event Hub.
az eventhubs eventhub show --namespace-name $NS_NAME --name $HUB_NAME
View the Event Hub in the Azure portal
Next, let's see what this looks like in the Azure portal.
Sign into the Azure portal using the same account you activated the sandbox with.
Find your Event Hubs namespace using the Search bar at the top of portal.
Select your namespace to open it.
Select Event Hubs namespace under the ENTITIES section.
Click Event Hubs.
Your Event Hub displays with a status of Active, and default values for Message Retention (7) and Partition Count of (4).
You've now created a new Event Hub, and you've all the necessary information ready to configure your publisher and consumer applications.