Skip to content

Commit aa8e5bd

Browse files
committed
Review tutorial-custom-allocation-policies
1 parent ae93851 commit aa8e5bd

File tree

1 file changed

+58
-48
lines changed

1 file changed

+58
-48
lines changed

articles/iot-dps/tutorial-custom-allocation-policies.md

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
title: Tutorial - Use custom allocation policies with Azure IoT Hub Device Provisioning Service
2+
title: Tutorial - Assign devices to multiple hubs using DPS
33
description: This tutorial shows how to provision devices using a custom allocation policy in your Azure IoT Hub Device Provisioning Service (DPS) instance.
44
author: kgremban
55
ms.author: kgremban
6-
ms.date: 09/13/2022
6+
ms.date: 03/21/2024
77
ms.topic: tutorial
88
ms.service: iot-dps
99
services: iot-dps
@@ -12,7 +12,7 @@ ms.custom: devx-track-csharp, devx-track-azurecli
1212

1313
# Tutorial: Use custom allocation policies with Device Provisioning Service (DPS)
1414

15-
Custom allocation policies give you more control over how devices are assigned to your IoT hubs. With custom allocation policies, you can define your own allocation policies when the policies provided by the Azure IoT Hub Device Provisioning Service (DPS) don't meet the requirements of your scenario. A custom allocation policy is implemented in a webhook hosted in [Azure functions](../azure-functions/functions-overview.md) and configured on one or more individual enrollments and/or enrollment groups. When a device registers with DPS using a configured enrollment entry, DPS calls the webhook to find out which IoT hub the device should be registered to and, optionally, its initial state. To learn more, see [Understand custom allocation policies](concepts-custom-allocation.md).
15+
Custom allocation policies give you more control over how devices are assigned to your IoT hubs. With custom allocation policies, you can define your own allocation policies when the policies provided by the Azure IoT Hub Device Provisioning Service (DPS) don't meet the requirements of your scenario. A custom allocation policy is implemented in a webhook hosted in [Azure Functions](../azure-functions/functions-overview.md) and configured on one or more individual enrollments and/or enrollment groups. When a device registers with DPS using a configured enrollment entry, DPS calls the webhook to find out which IoT hub the device should be registered to and, optionally, its initial state. To learn more, see [Understand custom allocation policies](concepts-custom-allocation.md).
1616

1717
This tutorial demonstrates a custom allocation policy using an Azure Function written in C#. Devices are assigned to one of two IoT hubs representing a *Contoso Toasters Division* and a *Contoso Heat Pumps Division*. Devices requesting provisioning must have a registration ID with one of the following suffixes to be accepted for provisioning:
1818

@@ -24,12 +24,12 @@ Devices will be simulated using a provisioning sample included in the [Azure IoT
2424
In this tutorial, you'll do the following:
2525

2626
> [!div class="checklist"]
27-
> * Use the Azure CLI to create a DPS instance and to create and link two Contoso division IoT hubs (**Contoso Toasters Division** and **Contoso Heat Pumps Division**) to it
28-
> * Create an Azure Function that implements the custom allocation policy
29-
> * Create a new enrollment group uses the Azure Function for the custom allocation policy
30-
> * Create device symmetric keys for two simulated devices
31-
> * Set up the development environment for the Azure IoT C SDK
32-
> * Simulate the devices and verify that they are provisioned according to the example code in the custom allocation policy
27+
> * Use the Azure CLI to create a DPS instance and to create and link two Contoso division IoT hubs (**Contoso Toasters Division** and **Contoso Heat Pumps Division**) to it.
28+
> * Create an Azure Function that implements the custom allocation policy.
29+
> * Create a new enrollment group uses the Azure Function for the custom allocation policy.
30+
> * Create device symmetric keys for two simulated devices.
31+
> * Set up the development environment for the Azure IoT C SDK.
32+
> * Simulate the devices and verify that they are provisioned according to the example code in the custom allocation policy.
3333
3434
[!INCLUDE [quickstarts-free-trial-note](../../includes/quickstarts-free-trial-note.md)]
3535

@@ -39,81 +39,91 @@ The following prerequisites are for a Windows development environment. For Linux
3939

4040
- [Visual Studio](https://visualstudio.microsoft.com/vs/) 2022 with the ['Desktop development with C++'](/cpp/ide/using-the-visual-studio-ide-for-cpp-desktop-development) workload enabled. Visual Studio 2015 and Visual Studio 2017 are also supported.
4141

42-
- Latest version of [Git](https://git-scm.com/download/) installed.
42+
- Git installed. For more information, see [Git downloads](https://git-scm.com/download/).
4343

44-
[!INCLUDE [azure-cli-prepare-your-environment-no-header.md](~/reusable-content/azure-cli/azure-cli-prepare-your-environment-no-header.md)]
44+
- Azure CLI installed. For more information, see [How to install the Azure CLI](/cli/azure/install-azure-cli). Or, you can run the commands in this tutorial in the Bash environment in [Azure Cloud Shell](/azure/cloud-shell/overview).
4545

46-
## Create the provisioning service and two divisional IoT hubs
46+
## Create the provisioning service and two IoT hubs
4747

4848
In this section, you use the Azure Cloud Shell to create a provisioning service and two IoT hubs representing the **Contoso Toasters Division** and the **Contoso Heat Pumps division**.
4949

50-
> [!TIP]
51-
> The commands used in this tutorial create the provisioning service and other resources in the West US location. We recommend that you create your resources in the region nearest you that supports Device Provisioning Service. You can view a list of available locations by running the command `az provider show --namespace Microsoft.Devices --query "resourceTypes[?resourceType=='ProvisioningServices'].locations | [0]" --out table` or by going to the [Azure Status](https://azure.microsoft.com/status/) page and searching for "Device Provisioning Service". In commands, locations can be specified either in one word or multi-word format; for example: westus, West US, WEST US, etc. The value is not case sensitive. If you use multi-word format to specify location, enclose the value in quotes; for example, `-- location "West US"`.
52-
>
50+
1. First, set environment variables in your workspace to simplify the commands in this tutorial.
5351

54-
1. Use the Azure Cloud Shell to create a resource group with the [az group create](/cli/azure/group#az-group-create) command. An Azure resource group is a logical container into which Azure resources are deployed and managed.
52+
The DPS and IoT Hub names must be globally unique. Replace the `SUFFIX` placeholder with your own value.
5553

56-
The following example creates a resource group named *contoso-us-resource-group* in the *westus* region. We recommend that you use this group for all resources created in this tutorial. This approach will make clean up easier after you're finished.
54+
Also, the Azure Function code you create later in this tutorial looks for IoT hubs that have either `-toasters-` or `-heatpups-` in their names. If you change the suggested values, make sure to use names that contain the required substrings.
5755

58-
```azurecli-interactive
59-
az group create --name contoso-us-resource-group --location westus
60-
```
56+
### [Bash](#tab/bash)
6157

62-
2. Use the Azure Cloud Shell to create a device provisioning service (DPS) with the [az iot dps create](/cli/azure/iot/dps#az-iot-dps-create) command. The provisioning service will be added to *contoso-us-resource-group*.
58+
```bash
59+
export RESOURCE_GROUP="contoso-us-resource-group"
60+
export LOCATION="westus"
61+
export DPS="contoso-provisioning-service-SUFFIX"
62+
export TOASTER_HUB="contoso-toasters-hub-SUFFIX"
63+
export HEATPUMP_HUB="contoso-heatpumps-hub-SUFFIX"
64+
```
6365

64-
The following example creates a provisioning service named *contoso-provisioning-service-1098* in the *westus* location. You must use a unique service name. Make up your own suffix in the service name in place of **1098**.
66+
### [PowerShell](#tab/powershell)
6567

66-
```azurecli-interactive
67-
az iot dps create --name contoso-provisioning-service-1098 --resource-group contoso-us-resource-group --location westus
68-
```
68+
```powershell
69+
$env:RESOURCE_GROUP = "contoso-us-resource-group"
70+
$env:LOCATION = "westus"
71+
$env:DPS = "contoso-provisioning-service-SUFFIX"
72+
$env:TOASTER_HUB = "contoso-toasters-hub-SUFFIX"
73+
$env:HEATPUMP_HUB = "contoso-heatpumps-hub-SUFFIX"
74+
```
6975

70-
This command may take a few minutes to complete.
76+
---
7177

72-
3. Use the Azure Cloud Shell to create the **Contoso Toasters Division** IoT hub with the [az iot hub create](/cli/azure/iot/hub#az-iot-hub-create) command. The IoT hub will be added to *contoso-us-resource-group*.
78+
> [!TIP]
79+
> The commands used in this tutorial create resources in the West US location by default. We recommend that you create your resources in the region nearest you that supports Device Provisioning Service. You can view a list of available locations by going to the [Azure Status](https://azure.microsoft.com/status/) page and searching for "Device Provisioning Service". In commands, locations can be specified either in one word or multi-word format; for example: westus, West US, WEST US, etc. The value is not case sensitive.
7380
74-
The following example creates an IoT hub named *contoso-toasters-hub-1098* in the *westus* location. You must use a unique hub name. Make up your own suffix in the hub name in place of **1098**.
81+
1. Use the [az group create](/cli/azure/group#az-group-create) command to create an Azure resource group. An Azure resource group is a logical container into which Azure resources are deployed and managed.
7582

76-
> [!CAUTION]
77-
> The example Azure Function code for the custom allocation policy requires the substring `-toasters-` in the hub name. Make sure to use a name containing the required toasters substring.
83+
The following example creates a resource group. We recommend that you use a single group for all resources created in this tutorial. This approach will make clean up easier after you're finished.
84+
85+
```azurecli-interactive
86+
az group create --name $RESOURCE_GROUP --location $LOCATION
87+
```
88+
89+
2. Use the [az iot dps create](/cli/azure/iot/dps#az-iot-dps-create) command to create an instance of the Device Provisioning Service (DPS). The provisioning service is added to *contoso-us-resource-group*.
7890

7991
```azurecli-interactive
80-
az iot hub create --name contoso-toasters-hub-1098 --resource-group contoso-us-resource-group --location westus --sku S1
92+
az iot dps create --name $DPS --resource-group $RESOURCE_GROUP --location $LOCATION
8193
```
8294
83-
This command may take a few minutes to complete.
95+
This command might take a few minutes to complete.
8496
85-
4. Use the Azure Cloud Shell to create the **Contoso Heat Pumps Division** IoT hub with the [az iot hub create](/cli/azure/iot/hub#az-iot-hub-create) command. This IoT hub will also be added to *contoso-us-resource-group*.
97+
3. Use the [az iot hub create](/cli/azure/iot/hub#az-iot-hub-create) command to create the **Contoso Toasters Division** IoT hub. The IoT hub is added to *contoso-us-resource-group*.
8698
87-
The following example creates an IoT hub named *contoso-heatpumps-hub-1098* in the *westus* location. You must use a unique hub name. Make up your own suffix in the hub name in place of **1098**.
99+
```azurecli-interactive
100+
az iot hub create --name $TOASTER_HUB --resource-group $RESOURCE_GROUP --location $LOCATION --sku S1
101+
```
102+
103+
This command may take a few minutes to complete.
88104
89-
> [!CAUTION]
90-
> The example Azure Function code for the custom allocation policy requires the substring `-heatpumps-` in the hub name. Make sure to use a name containing the required heatpumps substring.
105+
4. Use the [az iot hub create](/cli/azure/iot/hub#az-iot-hub-create) command to create the **Contoso Heat Pumps Division** IoT hub. This IoT hub also is added to *contoso-us-resource-group*.
91106
92107
```azurecli-interactive
93-
az iot hub create --name contoso-heatpumps-hub-1098 --resource-group contoso-us-resource-group --location westus --sku S1
108+
az iot hub create --name HEATPUMP_HUB --resource-group $RESOURCE_GROUP --location $LOCATION --sku S1
94109
```
95110
96111
This command may take a few minutes to complete.
97112
98-
5. The IoT hubs must be linked to the DPS resource.
99-
100-
Run the following two commands to get the connection strings for the hubs you created. Replace the hub resource names with the names you chose in each command:
113+
5. Run the following two commands to get the connection strings for the hubs you created.
101114
102115
```azurecli-interactive
103-
hubToastersConnectionString=$(az iot hub connection-string show --hub-name contoso-toasters-hub-1098 --key primary --query connectionString -o tsv)
104-
hubHeatpumpsConnectionString=$(az iot hub connection-string show --hub-name contoso-heatpumps-hub-1098 --key primary --query connectionString -o tsv)
116+
TOASTER_HUB_CONNECTION_STRING=$(az iot hub connection-string show --hub-name $TOASTER_HUB --key primary --query connectionString -o tsv)
117+
HEATPUMP_HUB_CONNECTION_STRING=$(az iot hub connection-string show --hub-name HEATPUMP_HUB --key primary --query connectionString -o tsv)
105118
```
106119
107-
Run the following commands to link the hubs to the DPS resource. Replace the DPS resource name with the name you chose in each command:
120+
6. Run the following commands to link the hubs to the DPS resource. Replace the DPS resource name with the name you chose in each command:
108121
109122
```azurecli-interactive
110-
az iot dps linked-hub create --dps-name contoso-provisioning-service-1098 --resource-group contoso-us-resource-group --connection-string $hubToastersConnectionString --location westus
111-
az iot dps linked-hub create --dps-name contoso-provisioning-service-1098 --resource-group contoso-us-resource-group --connection-string $hubHeatpumpsConnectionString --location westus
123+
az iot dps linked-hub create --dps-name $DPS --resource-group $RESOURCE_GROUP --connection-string $TOASTER_HUB_CONNECTION_STRING --location $LOCATION
124+
az iot dps linked-hub create --dps-name $DPS --resource-group $RESOURCE_GROUP --connection-string $HEATPUMP_HUB_CONNECTION_STRING --location $LOCATION
112125
```
113126
114-
115-
116-
117127
## Create the custom allocation function
118128
119129
In this section, you create an Azure function that implements your custom allocation policy. This function decides which divisional IoT hub a device should be registered to based on whether its registration ID contains the string **-contoso-tstrsd-007** or **-contoso-hpsd-088**. It also sets the initial state of the device twin based on whether the device is a toaster or a heat pump.

0 commit comments

Comments
 (0)