You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/iot-dps/tutorial-custom-allocation-policies.md
+58-48Lines changed: 58 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
---
2
-
title: Tutorial - Use custom allocation policies with Azure IoT Hub Device Provisioning Service
2
+
title: Tutorial - Assign devices to multiple hubs using DPS
3
3
description: This tutorial shows how to provision devices using a custom allocation policy in your Azure IoT Hub Device Provisioning Service (DPS) instance.
# Tutorial: Use custom allocation policies with Device Provisioning Service (DPS)
14
14
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).
16
16
17
17
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:
18
18
@@ -24,12 +24,12 @@ Devices will be simulated using a provisioning sample included in the [Azure IoT
24
24
In this tutorial, you'll do the following:
25
25
26
26
> [!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.
@@ -39,81 +39,91 @@ The following prerequisites are for a Windows development environment. For Linux
39
39
40
40
-[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.
41
41
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/).
- 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).
45
45
46
-
## Create the provisioning service and two divisional IoT hubs
46
+
## Create the provisioning service and two IoT hubs
47
47
48
48
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**.
49
49
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.
53
51
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.
55
53
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.
57
55
58
-
```azurecli-interactive
59
-
az group create --name contoso-us-resource-group --location westus
60
-
```
56
+
### [Bash](#tab/bash)
61
57
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*.
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)
65
67
66
-
```azurecli-interactive
67
-
az iot dps create --name contoso-provisioning-service-1098 --resource-group contoso-us-resource-group --location westus
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.
73
80
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.
75
82
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*.
az iot dps create --name $DPS --resource-group $RESOURCE_GROUP --location $LOCATION
81
93
```
82
94
83
-
This command may take a few minutes to complete.
95
+
This command might take a few minutes to complete.
84
96
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*.
86
98
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**.
> 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*.
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.
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