title: Provision simulated X.509 device to Azure IoT Hub using Node.js description: Create and provision a simulated X.509 device using Node.js device SDK for Azure IoT Hub Device Provisioning Service (DPS).This quickstart uses individual enrollments. author: wesmc7777 ms.author: wesmc ms.date: 11/08/2018 ms.topic: quickstart ms.service: iot-dps services: iot-dps ms.devlang: nodejs ms.custom: mvc
Quickstart: Create and provision an X.509 simulated device using Node.js device SDK for IoT Hub Device Provisioning Service
[!INCLUDE iot-dps-selector-quick-create-simulated-device-x509]
In this quickstart, you create a simulated X.509 device on a Windows computer. You use device sample Node.js code to connect this simulated device with your IoT hub using an individual enrollment with the Device Provisioning Service (DPS).
- Review of Auto-provisioning concepts.
- Completion of Set up IoT Hub Device Provisioning Service with the Azure portal.
- An Azure account with an active subscription. Create one for free.
- Node.js v4.0+.
- Git.
- OpenSSL.
[!INCLUDE IoT Device Provisioning Service basic]
-
Complete the steps in the Setup IoT Hub Device Provisioning Service with the Azure portal before you proceed.
-
Make sure you have Node.js v4.0 or above installed on your machine.
-
Make sure Git is installed on your machine and is added to the environment variables accessible to the command window.
-
Make sure OpenSSL is installed on your machine and is added to the environment variables accessible to the command window. This library can either be built and installed from source or downloaded and installed from a third party such as this.
[!NOTE] If you have already created your root, intermediate, and/or leaf X.509 certificates, you may skip this step and all following steps regarding certificate generation.
In this section you, will use a self-signed X.509 certificate, it is important to keep in mind the following:
- Self-signed certificates are for testing only, and should not be used in production.
- The default expiration date for a self-signed certificate is one year.
You will use sample code from the Azure IoT SDK for Node.js to create the certificate to be used with the individual enrollment entry for the simulated device.
The Azure IoT Device Provisioning Service supports two types of enrollments:
- Enrollment groups: Used to enroll multiple related devices.
- Individual enrollments: Used to enroll a single device.
This article demonstrates individual enrollments.
-
Open a command prompt. Clone the GitHub repo for the code samples:
git clone https://github.com/Azure/azure-iot-sdk-node.git --recursive
-
Navigate to the certificate generator script and build the project.
cd azure-iot-sdk-node/provisioning/tools npm install
-
Create a leaf X.509 certificate by running the script using your own certificate-name. The leaf certificate's common name becomes the Registration ID so be sure to only use lower-case alphanumerics and hyphens.
node create_test_cert.js device {certificate-name}
-
Sign in to the Azure portal, select the All resources button on the left-hand menu and open your Device Provisioning Service instance.
-
From the Device Provisioning Service menu, select Manage enrollments. Select Individual Enrollments tab and select the Add individual enrollment button at the top.
-
In the Add Enrollment panel, enter the following information:
-
Select X.509 as the identity attestation Mechanism.
-
Under the Primary certificate .pem or .cer file, choose Select a file to select the certificate file {certificate-name}_cert.pem created in the previous steps.
-
Optionally, you may provide the following information:
- Select an IoT hub linked with your provisioning service.
- Enter a unique device ID. Make sure to avoid sensitive data while naming your device.
- Update the Initial device twin state with the desired initial configuration for the device.
- Once complete, press the Save button.
On successful enrollment, your X.509 device appears as {certificatename} under the Registration ID column in the Individual Enrollments tab. Note this value for later.
-
The Azure IoT Hub Node.js Device SDK provides an easy way to simulate a device. For further reading, see Device concepts.
-
In the Azure portal, select the Overview blade for your Device Provisioning service and note the GLobal Device Endpoint and ID Scope values.
-
Copy your certificate and key to the sample folder.
copy .\{certificate-name}_cert.pem ..\device\samples\{certificate-name}_cert.pem copy .\{certificate-name}_key.pem ..\device\samples\{certificate-name}_key.pem
-
Navigate to the device test script and build the project.
cd ..\device\samples npm install
-
Edit the register_x509.js file. Save the file after making the following changes.
- Replace
provisioning host
with the Global Device Endpoint noted in Step 1 above. - Replace
id scope
with the ID Scope noted in Step 1 above. - Replace
registration id
with the Registration ID noted in the previous section. - Replace
cert filename
andkey filename
with the files you copied in Step 2 above.
- Replace
-
Execute the script and verify the device was provisioned successfully.
node register_x509.js
-
In the portal, navigate to the IoT hub linked to your provisioning service and open the IoT devices blade. On successful provisioning of the simulated X.509 device to the hub, its device ID appears on the IoT devices blade, with STATUS as enabled. You might need to press the Refresh button at the top if you already opened the blade prior to running the sample device application.
If you changed the initial device twin state from the default value in the enrollment entry for your device, it can pull the desired twin state from the hub and act accordingly. For more information, see Understand and use device twins in IoT Hub.
If you plan to continue working on and exploring the device client sample, do not clean up the resources created in this quickstart. If you do not plan to continue, use the following steps to delete all resources created by this quickstart.
- Close the device client sample output window on your machine.
- From the left-hand menu in the Azure portal, select All resources and then select your Device Provisioning service. Open the Manage Enrollments blade for your service, and then select the Individual Enrollments tab. Select the check box next to the REGISTRATION ID of the device you enrolled in this quickstart, and press the Delete button at the top of the pane.
- From the left-hand menu in the Azure portal, select All resources and then select your IoT hub. Open the IoT devices blade for your hub, select the check box next to the DEVICE ID of the device you registered in this quickstart, and then press the Delete button at the top of the pane.
In this quickstart, you’ve created a simulated X.509 device and provisioned it to your IoT hub using the Azure IoT Hub Device Provisioning Service on the portal. To learn how to enroll your X.509 device programmatically, continue to the quickstart for programmatic enrollment of X.509 devices.
[!div class="nextstepaction"] Azure quickstart - Enroll X.509 devices to Azure IoT Hub Device Provisioning Service