The Device Provisioning Service (DPS) allows you to deploy thousands of devices without human interaction. In this lab, we will work with a single device in the DPS to better understand device deployment at scale.
Successfully complete this lab by achieving the following learning objectives:
- Set Up Device Provisioning Service
- Open the link for this lab in an incognito or private browser window.
- Enter the credentials assigned to this lab (they will be different from the credentials you see in the solution video; each student gets a unique lab session and credentials).
- Navigate to the resource group Overview page to confirm that 2 Azure IoT Hubs have been predeployed.
- Create a new Device Provisioning Service with a universally unique name in the resource group already created for the lab. Leave all other values as their defaults.
- Link 2 Hubs to the DPS and Set Allocation Policy
- Link the 2 predeployed hubs to the DPS.
- Set the default Hub Allocation Policy to Evenly weighted distribution.
- Return to the DPS Overview page.
- Create Individual Enrollment and Collect Metadata
Most of the rest of the lab will be completed in a Cloud Shell terminal.
- Launch Cloud Shell from within the Azure Portal, choosing the Bash environment in order to use Azure CLI.
- When prompted to initialize storage for Cloud Shell, select Show advanced settings, and add the storage account and file share. Note that the storage account name must be universally unique; you might want to use the random characters at the beginning of the resource group name as a part of your storage account name.
- Install the Azure IoT extension for Azure CLI:
az extension add --name azure-iot
- Using Azure CLI, add an individual enrollment to the DPS you created, in the same resource group, with a symmetric key attestation type. Allow the DPS to generate the primary and secondary keys. Use something simple for the enrollment ID; it does not have to be universally unique.
- In order for our simulated device to connect to the provisioning service, we’ll need to retrieve 4 values: 2 from the enrollment and 2 from the DPS.
- When the new enrollment metadata is echoed back in the terminal window, copy the registration ID and the primary key to a text document.
- Using the Azure CLI, use a
showcommand to view metadata for the DPS you created. When the metadata displays in the terminal window, copy the provisioning host (also known as global device endpoint) and ID scope, and paste them into your text document.
- Configure and Run Simulated Device
Remaining in Cloud Shell, download the sample Node.js application by cloning the Git repository below. This sample application acts as a simulated device. Once connected to the to the internet, the device will connect to the DPS, which will use the enrollment settings to assign the device to one of the 2 Azure IoT Hubs.
git clone https://github.com/Azure/azure-iot-sdk-node.git --recursive
Change to the directory for provisioning samples:
Install the Node dependencies for the sample application:
In the top menu of the Cloud Shell terminal window, select the curly brackets icon to open the editor. Navigate to this directory: azure-iot-sdk-node/provisioning/device/samples.
register_symkey.jsand find the 4 variables to replace from your text document:
- Enrollment primary key
- Registration ID
- Provisioning host (or global device endpoint)
- ID scope
Tip: Make sure string values are wrapped in single quotes.
Save your changes. Tip: Look for the "…" in the upper right of the editor window to save changes and close the editor.
Back at the command prompt, run the following command to launch the application and run your simulated device:
- Confirm Enrollment and Device Hub Registration in Azure Portal
- Go back to the Azure portal and confirm that your individual enrollment was created in the DPS.
- Navigate to the 2 Azure IoT Hubs to confirm that the device is registered in exactly 1 of the 2 hubs — the DPS will decide which hub based on the allocation policy. (Given that we have only 2 hubs and 1 device, the actual choice of hubs is trivial.)