Create an IoT hub using the Azure CLI 2.0

Introduction

You can use Azure CLI 2.0 (az.py) to create and manage Azure IoT hubs programmatically. This article shows you how to use the Azure CLI 2.0 (az.py) to create an IoT hub.

You can complete the task using one of the following CLI versions:

  • Azure CLI (azure.js) – the CLI for the classic and resource management deployment models.
  • Azure CLI 2.0 (az.py) - the next generation CLI for the resource management deployment model as described in this article.

To complete this tutorial, you need the following:

  • An active Azure account. If you don't have an account, you can create a free account in just a couple of minutes.
  • Azure CLI 2.0.

Sign in and set your Azure account

Sign in to your Azure account and select your subscription.

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

  2. If you have multiple Azure subscriptions, signing in to Azure grants you access to all the Azure accounts associated with your credentials. Use the following command to list the Azure accounts available for you to use:

    az account list 
    

    Use the following command to select subscription that you want to use to run the commands to create your IoT hub. You can use either the subscription name or ID from the output of the previous command:

    az account set --subscription {your subscription name or id}
    

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 the 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

    The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming it.

Note

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 the 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 developing for IoT Hub, see the following articles:

To further explore the capabilities of IoT Hub, see: