Create an IoT hub using the Azure CLI

This article shows you how to create an IoT hub using Azure CLI.

Prerequisites

To complete this how-to, you need an Azure subscription. If you don't have an Azure subscription, create a free account before you begin.

Use Azure Cloud Shell

Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.

To start Azure Cloud Shell:

Option Example/Link
Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell. Example of Try It for Azure Cloud Shell
Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. Launch Cloud Shell in a new window
Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. Cloud Shell button in the Azure portal

To run the code in this article in Azure Cloud Shell:

  1. Start Cloud Shell.

  2. Select the Copy button on a code block to copy the code.

  3. Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.

  4. Select Enter to run the code.

Sign in and set your Azure account

If you are running Azure CLI locally instead of using Cloud Shell, you need to sign in to your Azure account.

At the command prompt, run the login command:

az login

Follow the instructions to authenticate using the code and sign in to your Azure account through a web browser.

Create an IoT Hub

Use the Azure CLI to create a resource group and then add an IoT hub.

  1. 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 westus
    

    Tip

    The previous example creates the resource group in the West US location. You can view a list of available locations by running this command:

    az account list-locations -o table
    
  2. 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 are billed. For more information, see Azure IoT Hub pricing.

Remove an IoT Hub

You can use Azure CLI to delete an individual resource, such as an IoT hub, or delete a resource group and all its resources, including any IoT hubs.

To delete an IoT hub, run the following command:

az iot hub delete --name {your iot hub name} -\
  -resource-group {your resource group name}

To delete a resource group and all its resources, run the following command:

az group delete --name {your resource group name}

Next steps

To learn more about using an IoT hub, see the following articles: