[Azure IoT]How-to-use-custom-allocation-policies in DPS

Photo by chuttersnap on Unsplash

Azure device provisioning service supports registration of devices using below allocation policies.

Lowest latency

This policy assigns devices to the linked IoT Hub which will result in the lowest latency communications between device and IoT Hub

Evenly weighted distribution

This policy evenly distributes devices across the linked IoT Hubs. If you are provisioning devices to only one IoT Hub, we recommend this setting. The default setting.

Static configuration

This policy requires a desired IoT Hub be listed in the enrollment entry for a device to be provisioned.

Custom allocation policy

A custom allocation policy gives you more control over how devices are assigned to an IoT hub. This is accomplished by using custom code in an Azure function to assign devices to an IoT hub. The device provisioning service calls your Azure Function code providing all relevant information about the device and the enrollment. Your function code is executed and returns the IoT hub information used to provisioning the device.

We will now implement the custom allocation policy using Node.js Azure function. Azure has provided the same implementation in C# here.

For, configuring the DPS enrollment groups you can follow this link. Make sure in allocation policy you select the custom allocation policy and further select the function app and function name.

Action time now. Below code is for Azure function in Node.js

Some observations in above code

  1. The returned object from the function has a body field in which we set the hubName and the Initial twin properties.
  2. D is caps in the Desired field under properties field of initial twin
  3. The returned object from function is assigned to context.res

Here is the official video from Azure guys.

If this post was helpful, please click the clap 👏 button below a few times to show your support! ⬇⬇

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store