Create an IoT hub using the Azure CLI
This article shows you how to create an IoT hub using Azure CLI.
Prerequisites
You can use the local Azure CLI.
If you prefer, install the Azure CLI to run CLI reference commands.
Local Azure CLI, see how to 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.
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.
When you create an IoT hub, you must create it in a resource group. Either use an existing resource group, or run the following command to create a resource group:
az group create --name {your resource group name} --location chinanorth
Tip
The previous example creates the resource group in the China North location. You can view a list of available locations by running this command:
az account list-locations -o table
Create an IoT hub
Use the Azure CLI to create a resource group and then add an IoT hub.
Run the following command to create an IoT hub in your resource group, using a globally unique name for your IoT hub:
az iot hub create --name {your iot hub name} \
--resource-group {your resource group name} --sku S1
Important
Because the IoT hub will be publicly discoverable as a DNS endpoint, be sure to avoid entering any sensitive or personally identifiable information when you name it.
The previous command creates an IoT hub in the S1 pricing tier for which you're billed. For more information, see Azure IoT Hub pricing.
For more information on Azure IoT Hub commands, see the az iot hub
reference article.
Update the IoT hub
You can change the settings of an existing IoT hub after it's created. Here are some properties you can set for an IoT hub:
Pricing and scale: Migrate to a different tier or set the number of IoT Hub units.
IP Filter: Specify a range of IP addresses that will be accepted or rejected by the IoT hub.
Properties: A list of properties that you can copy and use elsewhere, such as the resource ID, resource group, location, and so on.
For a complete list of options to update an IoT hub, see the az iot hub update commands reference page.
Register a new device in the IoT hub
In this section, you create a device identity in the identity registry in your IoT hub. A device can't connect to a hub unless it has an entry in the identity registry. For more information, see Understand the identity registry in your IoT hub. This device identity is IoT Edge enabled.
Run the following command to create a device identity. Use your IoT hub name and create a new device ID name in place of {iothub_name}
and {device_id}
. This command creates a device identity with default authorization (shared private key).
az iot hub device-identity create -n {iothub_name} -d {device_id} --ee
The result is a JSON printout which includes your keys and other information.
Alternatively, there are several options to register a device using different kinds of authorization. To explore the options, see Examples on the az iot hub device-identity reference page.
Remove an IoT hub
There are various commands to delete an individual resource, such as an IoT hub.
To delete an IoT hub, run the following command:
az iot hub delete --name {your iot hub name} -\
-resource-group {your resource group name}
Next steps
Learn more about the commands available in the Azure IoT extension for Azure CLI: