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.

Open Azure Cloud Shell

Azure Cloud Shell is an interactive shell environment hosted in Azure and used through your browser. Azure Cloud Shell allows you to use either bash or PowerShell shells to run a variety of tools to work with Azure services. Azure Cloud Shell comes pre-installed with the commands to allow you to run the content of this article without having to install anything on your local environment.

To run any code contained in this article on Azure Cloud Shell, open a Cloud Shell session, use the Copy button on a code block to copy the code, and paste it into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS. Pasted text is not automatically executed, so press Enter to run code.

You can launch Azure Cloud Shell with:

Option Example/Link
Select Try It in the upper-right corner of a code block. This doesn't automatically copy text to Cloud Shell. Example of Try It for Azure Cloud Shell
Open Azure Cloud Shell in your browser.
Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. Cloud Shell button in the Azure portal

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: