Manage Devices with the Azure IoT Device Provisioning Service

45 minutes
  • 5 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Set Up Device Provisioning Service
  1. Open the link for this lab in an incognito or private browser window.
  2. 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).
  3. Navigate to the resource group Overview page to confirm that 2 Azure IoT Hubs have been predeployed.
  4. 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
  1. Link the 2 predeployed hubs to the DPS.
  2. Set the default Hub Allocation Policy to Evenly weighted distribution.
  3. 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.

  1. Launch Cloud Shell from within the Azure Portal, choosing the Bash environment in order to use Azure CLI.
  2. 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.
  3. Install the Azure IoT extension for Azure CLI:
     az extension add --name azure-iot
  4. 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.
  5. 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 show command 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
  1. 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
  2. Change to the directory for provisioning samples:

    cd azure-iot-sdk-node/provisioning/device/samples
  3. Install the Node dependencies for the sample application:

    npm install
  4. 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.

  5. Open register_symkey.js and find the 4 variables to replace from your text document:

    1. Enrollment primary key
    2. Registration ID
    3. Provisioning host (or global device endpoint)
    4. ID scope
      Tip: Make sure string values are wrapped in single quotes.
  6. Save your changes. Tip: Look for the "…" in the upper right of the editor window to save changes and close the editor.

  7. Back at the command prompt, run the following command to launch the application and run your simulated device:

    node register_symkey.js
Confirm Enrollment and Device Hub Registration in Azure Portal
  1. Go back to the Azure portal and confirm that your individual enrollment was created in the DPS.
  2. 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.)

Additional Resources

Imagine you are on a team investigating the use of DPS for device provisioning at scale.

You have already demonstrated DPS features in the Azure portal, but your manager wants to see some code to be better convinced that the process can be automated.

You are tasked with setting up a DPS and showing how an individual enrollment works. For this example, you’ve chosen Azure CLI in Cloud Shell for simplicity and an easy to demonstrate the workflow.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?