|
| 1 | +--- |
| 2 | +title: Create an Azure IoT hub |
| 3 | +titleSuffix: Azure IoT Hub |
| 4 | +description: How to create, manage, and delete Azure IoT hubs through the Azure portal, CLI, and PowerShell. Includes information about retrieving the service connection string. |
| 5 | +author: kgremban |
| 6 | + |
| 7 | +ms.author: kgremban |
| 8 | +ms.service: iot-hub |
| 9 | +ms.topic: how-to |
| 10 | +ms.date: 07/10/2024 |
| 11 | +ms.custom: ['Role: Cloud Development'] |
| 12 | +--- |
| 13 | + |
| 14 | +# Create and manage Azure IoT hubs |
| 15 | + |
| 16 | +This article describes how to create and manage an IoT hub. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +Prepare the following prerequisites, depending on which tool you use. |
| 21 | + |
| 22 | +### [Azure portal](#tab/portal) |
| 23 | + |
| 24 | +* Access to the [Azure portal](https://portal.azure.com). |
| 25 | + |
| 26 | +### [Azure CLI](#tab/cli) |
| 27 | + |
| 28 | +* The Azure CLI installed on your development machine. If you don't have the Azure CLI, follow the steps to [Install the Azure CLI](/cli/azure/install-azure-cli). |
| 29 | + |
| 30 | +* A resource group in your Azure subscription. If you want to create a new resource group, use the [az group create](/cli/azure/group#az-group-create) command: |
| 31 | + |
| 32 | + ```azurecli-interactive |
| 33 | + az group create --name <RESOURCE_GROUP_NAME> --location <REGION> |
| 34 | + ``` |
| 35 | + |
| 36 | +### [Azure PowerShell](#tab/powershell) |
| 37 | + |
| 38 | +* Azure PowerShell installed on your development machine. If you don't have Azure PowerShell, follow the steps to [Install Azure PowerShell](/powershell/azure/install-azure-powershell). |
| 39 | + |
| 40 | +* A resource group in your Azure subscription. If you want to create a new resource group, use the [New-AzResourceGroup](/powershell/module/az.Resources/New-azResourceGroup) command: |
| 41 | + |
| 42 | + ```azurepowershell-interactive |
| 43 | + New-AzResourceGroup -Name <RESOURCE_GROUP_NAME> -Location "<REGION>" |
| 44 | + ``` |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## Create an IoT hub |
| 49 | + |
| 50 | +### [Azure portal](#tab/portal) |
| 51 | + |
| 52 | +[!INCLUDE [iot-hub-include-create-hub](../../includes/iot-hub-include-create-hub.md)] |
| 53 | + |
| 54 | +### [Azure CLI](#tab/cli) |
| 55 | + |
| 56 | +Use the [az iot hub create](/cli/azure/iot/hub#az-iot-hub-create) command to create an IoT hub in your resource group, using a globally unique name for your IoT hub. For example: |
| 57 | + |
| 58 | +```azurecli-interactive |
| 59 | +az iot hub create --name <NEW_NAME_FOR_YOUR_IOT_HUB> --resource-group <RESOURCE_GROUP_NAME> --sku S1 |
| 60 | +``` |
| 61 | + |
| 62 | +[!INCLUDE [iot-hub-pii-note-naming-hub](../../includes/iot-hub-pii-note-naming-hub.md)] |
| 63 | + |
| 64 | +The previous command creates an IoT hub in the S1 pricing tier. For more information, see [Azure IoT Hub pricing](https://azure.microsoft.com/pricing/details/iot-hub/). |
| 65 | + |
| 66 | +### [Azure PowerShell](#tab/powershell) |
| 67 | + |
| 68 | +Use the [New-AzIotHub](/powershell/module/az.IotHub/New-azIotHub) command to create an IoT hub in your resource group. The name of the IoT hub must be globally unique. For example: |
| 69 | + |
| 70 | +```azurepowershell-interactive |
| 71 | +New-AzIotHub ` |
| 72 | + -ResourceGroupName <RESOURCE_GROUP_NAME> ` |
| 73 | + -Name <NEW_NAME_FOR_YOUR_IOT_HUB> ` |
| 74 | + -SkuName S1 -Units 1 ` |
| 75 | + -Location "<REGION>" |
| 76 | +``` |
| 77 | + |
| 78 | +[!INCLUDE [iot-hub-pii-note-naming-hub](../../includes/iot-hub-pii-note-naming-hub.md)] |
| 79 | + |
| 80 | +The previous command creates an IoT hub in the S1 pricing tier. For more information, see [Azure IoT Hub pricing](https://azure.microsoft.com/pricing/details/iot-hub/). |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +## Connect to an IoT hub |
| 85 | + |
| 86 | +Provide access permissions to applications and services that use IoT Hub functionality. |
| 87 | + |
| 88 | +### Connect with a connection string |
| 89 | + |
| 90 | +Connection strings are tokens that grant devices and services permissions to connect to IoT Hub based on shared access policies. Connection strings are an easy way to get started with IoT Hub, and are used in many samples and tutorials, but aren't recommended for production scenarios. |
| 91 | + |
| 92 | +For most sample scenarios, the **service** policy is sufficient. The service policy grants **Service Connect** permissions to access service endpoints. For more information about the other built-in shared access policies, see [IoT Hub permissions](./iot-hub-dev-guide-sas.md#access-control-and-permissions). |
| 93 | + |
| 94 | +To get the IoT Hub connection string for the **service** policy, follow these steps: |
| 95 | + |
| 96 | +#### [Azure portal](#tab/portal) |
| 97 | + |
| 98 | +1. In the [Azure portal](https://portal.azure.com), select **Resource groups**. Select the resource group where your hub is located, and then select your hub from the list of resources. |
| 99 | + |
| 100 | +1. On the left-side pane of your IoT hub, select **Shared access policies**. |
| 101 | + |
| 102 | +1. From the list of policies, select the **service** policy. |
| 103 | + |
| 104 | +1. Copy the **Primary connection string** and save the value. |
| 105 | + |
| 106 | +#### [Azure CLI](#tab/cli) |
| 107 | + |
| 108 | +Use the [az iot hub connection-string show](/cli/azure/iot/hub/connection-string#az-iot-hub-connection-string-show) command to get a connection string for your IoT hub that grants the service policy permissions: |
| 109 | + |
| 110 | +```azurecli-interactive |
| 111 | +az iot hub connection-string show --hub-name <YOUR_IOT_HUB_NAME> --policy-name service |
| 112 | +``` |
| 113 | + |
| 114 | +The service connection string should look similar to the following example: |
| 115 | + |
| 116 | +```text |
| 117 | +"HostName=<IOT_HUB_NAME>.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=<SHARED_ACCESS_KEY>" |
| 118 | +``` |
| 119 | + |
| 120 | +#### [Azure PowerShell](#tab/powershell) |
| 121 | + |
| 122 | +Use the [Get-AzIotHubConnectionString](/powershell/module/az.iothub/get-aziothubconnectionstring) command to get a connection string for your IoT hub that grants the service policy permissions. |
| 123 | + |
| 124 | +```azurepowershell-interactive |
| 125 | +Get-AzIotHubConnectionString -ResourceGroupName "<YOUR_RESOURCE_GROUP>" -Name "<YOUR_IOT_HUB_NAME>" -KeyName "service" |
| 126 | +``` |
| 127 | + |
| 128 | +The service connection string should look similar to the following example: |
| 129 | + |
| 130 | +```text |
| 131 | +"HostName=<IOT_HUB_NAME>.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=<SHARED_ACCESS_KEY>" |
| 132 | +``` |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +### Connect with role assignments |
| 137 | + |
| 138 | +Authenticating access by using Microsoft Entra ID and controlling permissions by using Azure role-based access control (RBAC) provides improved security and ease of use over security tokens. To minimize potential security issues inherent in security tokens, we recommend that you enforce Microsoft Entra authentication whenever possible. For more information, see [Control access to IoT Hub by using Microsoft Entra ID](./authenticate-authorize-azure-ad.md). |
| 139 | + |
| 140 | +## Delete an IoT hub |
| 141 | + |
| 142 | +When you delete an IoT hub, you lose the associated device identity registry. If you want to move or upgrade an IoT hub, or delete an IoT hub but keep the devices, consider [migrating an IoT hub using the Azure CLI](./migrate-hub-state-cli.md). |
| 143 | + |
| 144 | +### [Azure portal](#tab/portal) |
| 145 | + |
| 146 | +To delete an IoT hub, open your IoT hub in the Azure portal, then choose **Delete**. |
| 147 | + |
| 148 | +:::image type="content" source="./media/create-hub/delete-iot-hub.png" alt-text="Screenshot showing where to find the delete button for an IoT hub in the Azure portal." lightbox="./media/create-hub/delete-iot-hub.png"::: |
| 149 | + |
| 150 | +### [Azure CLI](#tab/cli) |
| 151 | + |
| 152 | +To delete an IoT hub, run the [az iot hub delete](/cli/azure/iot/hub#az-iot-hub-delete) command: |
| 153 | + |
| 154 | +```azurecli-interactive |
| 155 | +az iot hub delete --name <IOT_HUB_NAME> --resource-group <RESOURCE_GROUP_NAME> |
| 156 | +``` |
| 157 | + |
| 158 | +### [Azure PowerShell](#tab/powershell) |
| 159 | + |
| 160 | +To delete the IoT hub, use the [Remove-AzIotHub](/powershell/module/az.iothub/remove-aziothub) command. |
| 161 | + |
| 162 | +```azurepowershell-interactive |
| 163 | +Remove-AzIotHub ` |
| 164 | + -ResourceGroupName MyIoTRG1 ` |
| 165 | + -Name MyTestIoTHub |
| 166 | +``` |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +## Other tools for managing IoT hubs |
| 171 | + |
| 172 | +In addition to the Azure portal and CLI, the following tools are available to help you work with IoT hubs in whichever way supports your scenario: |
| 173 | + |
| 174 | +* **IoT Hub resource provider REST API** |
| 175 | + |
| 176 | + Use the [IoT Hub Resource](/rest/api/iothub/iot-hub-resource) set of operations. |
| 177 | + |
| 178 | +* **Azure resource manager templates, Bicep, or Terraform** |
| 179 | + |
| 180 | + Use the [Microsoft.Devices/IoTHubs](/azure/templates/microsoft.devices/iothubs) resource type. For examples, see [IoT Hub sample templates](/samples/browse/?terms=iot%20hub&languages=bicep%2Cjson). |
| 181 | + |
| 182 | +* **Visual Studio Code** |
| 183 | + |
| 184 | + Use the [Azure IoT Hub extension for Visual Studio Code](./reference-iot-hub-extension.md). |
0 commit comments