Quickstart: Create an event hub using Azure CLI

In this quickstart, you will create an event hub using Azure CLI.

If you don't have an Azure trial subscription, create an Azure trial subscription 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.

  • This article requires version 2.0.4 or later of the Azure CLI.

Create a resource group

Run the following command to create a resource group. A resource group is a logical collection of Azure resources. All resources are deployed and managed in a resource group.

Select Copy to copy the command and paste it into the local Shell or CLI window, and run it. Update the resource group name and the region if you like.

rgName="contosorg$RANDOM"
region="chinaeast"
az group create --name $rgName --location $region

You see the output similar to the following one. You see the resource group name in the name field with a random number replacing $RANDOM.

{
  "id": "/subscriptions/{subscription_id}/resourceGroups/contosorg32744",
  "location": "chinaeast",
  "managedBy": null,
  "name": "contosorg32744",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": "Microsoft.Resources/resourceGroups"
}

Create an Event Hubs namespace

Run the following command to create an Event Hubs namespace. An Event Hubs namespace provides a unique scoping container, referenced by its fully qualified domain name, in which you create one or more event hubs. Update the name of the namespace if you like.

# Create an Event Hubs namespace. Specify a name for the Event Hubs namespace.
namespaceName="contosoehubns$RANDOM"
az eventhubs namespace create --name $namespaceName --resource-group $rgName -l $region

You see the output similar to the following one. You see the name of the namespace in the name field.

{
  "createdAt": "2023-03-13T20:28:53.037Z",
  "disableLocalAuth": false,
  "id": "/subscriptions/{subscription_id}0/resourceGroups/contosorg32744/providers/Microsoft.EventHub/namespaces/contosoehubns17861",
  "isAutoInflateEnabled": false,
  "kafkaEnabled": true,
  "location": "China East",
  "maximumThroughputUnits": 0,
  "metricId": "{subscription_id}0:contosoehubns17861",
  "minimumTlsVersion": "1.2",
  "name": "contosoehubns17861",
  "provisioningState": "Succeeded",
  "publicNetworkAccess": "Enabled",
  "resourceGroup": "contosorg32744",
  "serviceBusEndpoint": "https://contosoehubns17861.servicebus.chinacloudapi.cn:443/",
  "sku": {
    "capacity": 1,
    "name": "Standard",
    "tier": "Standard"
  },
  "status": "Active",
  "tags": {},
  "type": "Microsoft.EventHub/Namespaces",
  "updatedAt": "2023-03-13T20:29:45.637Z",
  "zoneRedundant": false
}

Create an event hub

Run the following command to create an event hub. Update the name of the event hub if you like.

# Create an event hub. Specify a name for the event hub. 
eventhubName="contosoehub$RANDOM"
az eventhubs eventhub create --name $eventhubName --resource-group $rgName --namespace-name $namespaceName

You see the output similar to the following one. You see the name of the event hub in the name field.

{
  "captureDescription": null,
  "createdAt": "2023-03-13T20:32:04.457000+00:00",
  "id": "/subscriptions/000000000-0000-0000-0000-00000000000000/resourceGroups/contosorg32744/providers/Microsoft.EventHub/namespaces/contosoehubns17861/eventhubs/contosoehub23255",
  "location": "chinaeast",
  "messageRetentionInDays": 7,
  "name": "contosoehub23255",
  "partitionCount": 4,
  "partitionIds": [
    "0",
    "1",
    "2",
    "3"
  ],
  "resourceGroup": "contosorg32744",
  "status": "Active",
  "systemData": null,
  "type": "Microsoft.EventHub/namespaces/eventhubs",
  "updatedAt": "2023-03-13T20:32:04.727000+00:00"
}

Congratulations! You have used Azure CLI to create an Event Hubs namespace, and an event hub within that namespace.

Clean up resources

If you want to keep this event hub so that you can test sending and receiving events, ignore this section. Otherwise, run the following command to delete the resource group. This command deletes all the resources in the resource group and the resource group itself.

az group delete --name $rgName

Next steps

In this article, you created a resource group, an Event Hubs namespace, and an event hub. For step-by-step instructions to send events to (or) receive events from an event hub, see the Send and receive events tutorials: