2
votes

I am trying to provision my device into Azure IoT using Azure IoT Device Provisioning Service in which I am using the custom allocation policy, under manage enrollment group.

When the device registers to DPS (device provisioning service), it triggers an Azure Function in which I will decide which IoT Hub the device will be assigned to.

In my function, I have to take this decision based on some custom payload which I need to send from the device at the time of registering itself to DPS.

How will I send that custom payload from the device to the DPS at the time of registering itself, so I can get that payload in my Azure Function and can take a decision?

I am using Java as a programming language for the same.

3
Did you try using custom allocation policies? See here as a reference - on that doc it explains how to provision a device to a specific IoTHub based on it's registration ID suffixes.asergaz
Thanks @sergaz for replying, I tried that custom allocation policy, but my question is I have to provision a device to a specific IoT hub based on some other keys that should be my keys like customerId, customerName, etc. it's not registration ID. So is there any way that I can send my custom payload to DPS while registering my device?Anil Agrawal

3 Answers

2
votes

If you are using X.509 based authentication, your Azure Function is getting the actual certificate as part of the request (in the clientCertificate field).

Therefore, you may want to use custom fields in your certificate that you can read in your function, and then allocate your IoT Hub of choice based on their contents.

Another option, and probably more elegant as having custom fields in your certificate might expose information that'd better remain private, would be to maintain the mappings between the registrationId, customerId, customerId, etc. in a separate store, which you can query in your Azure Function.

0
votes

I have achieved the above functionality in Node.js by sending the custom payload separately and not in custom fields of certificate. Here is the link to handle the custom payload in Azure function in node.js. This feature is available in C, C#, JAVA and Node.js client SDKs as per the Azure doc here.

Regarding sending the payload during device registration here is the method you should use in Java.

Using above approach you can avoid exposing data in custom fields in your certs.

0
votes

Actually, Azure SDK supports sending a custom payload when registering a device through the Device Provisioning Client. See the ProvisioningRegistrationAdditionalData Class in .NET SDK https://docs.microsoft.com/es-es/dotnet/api/microsoft.azure.devices.provisioning.client.provisioningregistrationadditionaldata?view=azure-dotnet

I think that the equivalent one in Java SKD is AdditionalData Class https://docs.microsoft.com/es-es/java/api/com.microsoft.azure.sdk.iot.provisioning.device.additionaldata?view=azure-java-stable

Moreover, you can also use the DPS REST API to provide the custom payload when registering a device. In the answer to this post (Register Device using rest API of Azure device provisioning service?) you can find an example of a request with curl for provisioning a device with a custom payload.

curl -L -i -X PUT --cert ./chain.pem --key ./iot-device-1.key.pem -H 'Content-Type: application/json' -H 'Content-Encoding:  utf-8' -d '{"registrationId": "iot-device-1", "payload": {"CustomProperty": "CustomValue"}}' https://global.azure-devices-provisioning.net/XXXXXXXXXXX/registrations/iot-device-1/register?api-version=2019-03-31

Check also this post (http://busbyland.com/azure-iot-device-provisioning-service-via-rest-part-1/) on which that answer is based and the REST API documentation (https://docs.microsoft.com/es-es/rest/api/iot-dps/runtimeregistration/registerdevice).