How to Implement Dynamic Registration of IoT Devices Using Alibaba Cloud Function Compute

In this article, we will discuss how we can implement dynamic registration of Internet of Things (IoT) devices the serverless way using Alibaba Cloud Function Compute.

The figure above only demonstrates the core process of dynamically registering devices through the API, but the production environment must add security verification logic based on the enterprise situation to ensure the identity security of the devices.

Technical Implementation

  1. FC calls the RegåisterDevice interface of the IoT Platform and passes the productKey and deviceId
  2. If it succeeds, return the device trituples
  3. If it fails, call the QueryDeviceDetail interface of the IoT Platform, pass the productKey and deviceId, and obtain the device trituples
  4. After obtaining the trituples, the device connects the Alibaba Cloud IoT Platform through MQTT.

This involves the use of the RegisterDevice and QueryDeviceDetail APIs.



Implementation on Function Compute

const getRawBody = require('raw-body');
const co = require('co');
const RPCClient = require('@alicloud/pop-core'). RPCClient;
const options = {
accessKey: "AK of the cloud account",
accessKeySecret: "AK Secret of the cloud account",
//1. Create a client
const iotClient = new RPCClient({
accessKeyId: options.accessKey,
secretAccessKey: options.accessKeySecret,
endpoint: options.endpoint || '',
apiVersion: options.apiVersion || '2018-01-20'
const productKey = 'productKey of the product';module.exports.handler = function(req, resp, context) {

getRawBody(req, function(err, body) {
body = JSON.parse(decodeURIComponent(body.toString()));
const deviceId = body.deviceId;
co(function* () { try {
//Create a device
const registerResponse = yield iotClient.request('RegisterDevice', {
productKey: productKey,
deviceName: deviceId
resp.send(JSON.stringify({ success: true, data: registerResponse.Data }));
} catch (err) {
//The device already exists; check the device details
const queryResponse = yield iotClient.request('QueryDeviceDetail', {
productKey: productKey,
deviceName: deviceId
const returnJson = { success: true } = {
DeviceName: queryResponse.Data.DeviceName,
ProductKey: queryResponse.Data.ProductKey,
DeviceSecret: queryResponse.Data.DeviceSecret,
IotId: queryResponse.Data.IotId
}); });};

Triggering Function Compute from the Device

To learn more about Alibaba Cloud’s Internet of Things (IoT) Platform, visit


Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

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