Provisioning an IoT Hub

Preview

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

Provisioning an IoT Hub

This guide shows you how to provision an IoT 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.Devices plugin, which you can install with:

$ locally plugin install --name Microsoft.Devices

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:

$ 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 IoT Hub

With the Resource Group in place, we can create the IoT Hub. The partition count is fixed at creation, so it's worth thinking about up front:

$ locally run az iot hub create -g sample-resources -n sampleiothub1 -l berlin --sku F1 --partition-count 2
{
  "hostname": "sampleiothub1.iothub.locally",
  "name": "sampleiothub1",
  "sku": "F1",
  "state": "Active"
}

Note

IoT Hub names are globally unique in Azure, and Locally keeps the same rule - so if you're following along with more than one hub you'll want to pick a different name.

That iothub.locally hostname is served by Locally's own DNS server and points at the IoT Hub Emulator running on your machine.

4. Get the connection string

To connect anything to the hub we need its connection string:

$ locally run az iot hub connection-string show -n sampleiothub1 --query connectionString -o tsv

5. Register a device

A hub with no devices doesn't do much, and Locally has built-in helpers for managing them:

$ locally iothub device create --hub sampleiothub1 --device sensor-01 $ locally iothub device list --hub sampleiothub1

Note

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

Each device has a twin - the document holding its desired and reported state - which you can inspect and update:

$ locally iothub twin show --hub sampleiothub1 --device sensor-01

We can also list the hubs in the Resource Group:

$ locally run az iot hub list -g sample-resources --query "[].{Name:name, Sku:sku.name, Hostname:properties.hostName}" -o table

6. 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, IoT Hub works the same way through any of the tooling that Locally supports - a Microsoft.Devices/IotHubs resource 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

Device telemetry lands in the hub's built-in endpoint, served by Event Hubs. The IoT Hub Emulator covers what else devices can do.

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.