Provisioning an Event Hubs Namespace

Preview

Sign in during Public Preview to get the Team plan free, plus an early-adopter discount when we launch. Sign in

Provisioning an Event Hubs Namespace

This guide shows you how to provision an Event Hubs Namespace and an event hub against Locally. As with the other guides we're going to use the Azure CLI, but the same resources can be provisioned with HashiCorp Terraform, Pulumi or Bicep too.

Before you start

Plugin required

This requires the Microsoft.EventHub plugin, which you can install with:

$ locally plugin install --name Microsoft.EventHub

1. Start Locally

Firstly, we need to launch Locally which we can do from a terminal by running:

$ locally build

Once Locally has started, the Locally Dashboard will open automatically:

Screenshot of the Locally Dashboard

2. Create a Resource Group

Next we can create the Resource Group to hold the namespace:

$ locally run az group create -n sample-resources -l berlin

There's two things to note here:

  1. The Azure CLI supports Automatic Configuration, meaning that it can automatically be configured to work against Locally just by prefixing commands with locally run.
  2. Locally intentionally uses a different set of locations to Azure as a safety precaution, so that you can be confident you're deploying against Locally rather than regular Azure. You can also configure Locally to use the Azure locations too, but you'll want to be extra sure that you're prefixing commands with locally run when you do.

3. Create the Event Hubs Namespace

With the Resource Group in place, we can create the Event Hubs Namespace:

$ locally run az eventhubs namespace create --name samplehubs1 --resource-group sample-resources --location berlin

Note

Event Hubs Namespace names are globally unique in Azure, and Locally keeps the same rule - so if you're following along with more than one namespace you'll want to pick a different name.
{
  "note": "some fields skipped for brevity",

  "location": "berlin",
  "name": "samplehubs1",
  "provisioningState": "Succeeded",
  "resourceGroup": "sample-resources",
  "status": "Active",
  "type": "Microsoft.EventHub/Namespaces"
}

4. Create an event hub

A namespace is just a container, so next we'll create an event hub inside it. Event Hubs partitions its stream, and the partition count is fixed at creation, so it's the one setting worth thinking about up front:

$ locally run az eventhubs eventhub create --name telemetry --namespace-name samplehubs1 --resource-group sample-resources --partition-count 4

Which gives us the hub along with the IDs of its partitions:

{
  "note": "some fields skipped for brevity",

  "name": "telemetry",
  "partitionCount": 4,
  "partitionIds": [
    "0",
    "1",
    "2",
    "3"
  ],
  "status": "Active",
  "type": "Microsoft.EventHub/Namespaces/EventHubs"
}

We can list the hubs in the namespace at any point:

$ locally run az eventhubs eventhub list --namespace-name samplehubs1 --resource-group sample-resources --query "[].{Name:name, Partitions:partitionCount, Status:status}" -o table
Name       Partitions    Status
---------  ------------  --------
telemetry  4             Active

5. Create a consumer group

Consumers read from an event hub through a consumer group, which is how two independent readers can each track their own position in the stream. Every hub starts with one called $Default, and we can add our own:

$ locally run az eventhubs eventhub consumer-group create --name processor --eventhub-name telemetry --namespace-name samplehubs1 --resource-group sample-resources
{
  "name": "processor",
  "type": "Microsoft.EventHub/namespaces/eventhubs/consumerGroups"
}

Locally has a built-in helper for listing them, which shows the default alongside ours:

$ locally eventhub consumer-group list --namespace samplehubs1 --hub telemetry
NAME
$Default
processor

6. Get the connection string

To have an application publish and consume events, we need the namespace's connection string. Locally has a helper for this too:

$ locally eventhub connection-string --namespace samplehubs1
Endpoint=sb://samplehubs1.eventhub.locally;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=...

Note

locally eventhub is part of Locally itself rather than a tool being configured, so it doesn't take the locally run prefix. Every az command does.

That eventhub.locally hostname is served by Locally's own DNS server and points at the Event Hubs Emulator running on your machine.

Locally also lets you look at what's landed in a partition without consuming it, which is handy while you're debugging a producer. Since Event Hubs is partitioned, you peek one partition at a time:

$ locally eventhub hub peek --namespace samplehubs1 --name telemetry --partition 0

As we haven't published anything yet:

No events in event hub "telemetry" partition 0.

We can see the Event Hubs Namespace in the Locally Dashboard too:

Screenshot of the Event Hubs Namespace in the Locally Dashboard

7. Tidy up

Finally, we can tidy up. To remove the Resource Group and everything within it:

$ locally run az group delete -n sample-resources --yes

There's nothing billable to clean up, since everything ran on your machine, but it's still worth checking your teardown scripts work here before you run them against Azure.

Doing this with other tooling

Whilst this guide used the Azure CLI, Event Hubs works the same way through any of the tooling that Locally supports - Microsoft.EventHub/namespaces and Microsoft.EventHub/namespaces/eventhubs resources in HashiCorp Terraform or OpenTofu, Pulumi, Bicep or an ARM Template all provision against Locally in the same way, with only the location changed.

Next steps

The Event Hubs Emulator lets you send events from the browser and pause a hub. To consume events in code, a Function App can use an Event Hubs trigger, and IoT Hub telemetry arrives through Event Hubs too.

Should you encounter any issues, please take a look at the troubleshooting section.

Preview

Sign in during Public Preview to get the Team plan free, plus an early-adopter discount when we launch. Sign in

A local cloud for you and your AI agents.

Your Azure infrastructure, running on your machine. Deploy in seconds, break things freely, and ship to Azure when you're ready.