Quickstart: Set up the IoT Hub Device Provisioning Service with Azure CLI

The Azure CLI is used to create and manage Azure resources from the command line or in scripts. This QuickStart details using the Azure CLI to create an IoT hub and an IoT Hub Device Provisioning Service and to link the two services together.

If you don't have an Azure subscription, create a free account before you begin.

Important

Both the IoT hub and the provisioning service you create in this QuickStart will be publicly discoverable as DNS endpoints. Make sure to avoid any sensitive information if you decide to change the names used for these resources.

Use Azure Cloud Shell

Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.

To start Azure Cloud Shell:

Option Example/Link
Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell. Example of Try It for Azure Cloud Shell
Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser. Launch Cloud Shell in a new window
Select the Cloud Shell button on the top-right menu bar in the Azure portal. Cloud Shell button in the Azure portal

To run the code in this article in Azure Cloud Shell:

  1. Start Cloud Shell.

  2. Select the Copy button on a code block to copy the code.

  3. Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.

  4. Select Enter to run the code.

Create a resource group

Create a resource group with the az group create command. An Azure resource group is a logical container into which Azure resources are deployed and managed.

The following example creates a resource group named my-sample-resource-group in the westus location.

az group create --name my-sample-resource-group --location westus

Tip

The example creates the resource group in the West US location. You can view a list of available locations by running the command az account list-locations -o table.

Create an IoT hub

Create an IoT hub with the az iot hub create command.

The following example creates an IoT hub named my-sample-hub in the westus location.

az iot hub create --name my-sample-hub --resource-group my-sample-resource-group --location westus

Create a provisioning service

Create a provisioning service with the az iot dps create command.

The following example creates an provisioning service named my-sample-dps in the westus location.

az iot dps create --name my-sample-dps --resource-group my-sample-resource-group --location westus

Tip

The example creates the provisioning service in the West US location. 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 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".

Get the connection string for the IoT hub

You need your IoT hub's connection string to link it with the Device Provisioning Service. Use the az iot hub show-connection-string command to get the connection string and use its output to set a variable that you will use when you link the two resources.

The following example sets the hubConnectionString variable to the value of the connection string for the primary key of the hub's iothubowner policy. You can specify a different policy with the --policy-name parameter. The command uses the Azure CLI query and output options to extract the connection string from the command output.

hubConnectionString=$(az iot hub show-connection-string --name my-sample-hub --key primary --query connectionString -o tsv)

You can use the echo command to see the connection string.

echo $hubConnectionString

Note

These two commands are valid for a host running under Bash. If you are using a local Windows/CMD shell or a PowerShell host, you need to modify the commands to use the correct syntax for that environment.

Link the IoT hub and your provisioning service with the az iot dps linked-hub create command.

The following example links an IoT hub named my-sample-hub in the westus location and a Device Provisioning service named my-sample-dps. It uses the connection string for my-sample-hub stored in the hubConnectionString variable in the previous step.

az iot dps linked-hub create --dps-name my-sample-dps --resource-group my-sample-resource-group --connection-string $hubConnectionString --location westus

Verify the provisioning service

Get the details of your provisioning service with the az iot dps show command.

The following example gets the details of a provisioning service named my-sample-dps. The linked IoT hub is shown in the properties.iotHubs collection.

az iot dps show --name my-sample-dps

Clean up resources

Other Quickstarts in this collection build upon this Quickstart. If you plan to continue on to work with subsequent Quickstarts or with the tutorials, do not clean up the resources created in this Quickstart. If you do not plan to continue, you can use the following commands to delete the provisioning service, the IoT hub or the resource group and all of its resources.

To delete the provisioning service, run the az iot dps delete command:

az iot dps delete --name my-sample-dps --resource-group my-sample-resource-group

To delete the IoT hub, run the az iot hub delete command:

az iot hub delete --name my-sample-hub --resource-group my-sample-resource-group

To delete a resource group and all its resources, run the az group delete command:

az group delete --name my-sample-resource-group

Next steps

In this Quickstart, you’ve deployed an IoT hub and a Device Provisioning Service instance, and linked the two resources. To learn how to use this set up to provision a simulated device, continue to the Quickstart for creating simulated device.