简体   繁体   中英

AWS IoT create things automatically

I am wondering how one creates many "things" in the AWS IoT solution via API without using the AWS web interface since this is not realistic in case I want thousands or millions of things. I guess you could write some script utilizing the "aws" client described here " http://docs.aws.amazon.com/iot/latest/developerguide/thing-registry.html " but thats not optimal if I want to control it from another service.

I assumed there would be a RESTish API to do this but it doesn't seem like it if I read the docs: "You use the AWS IoT console or the AWS CLI to interact with the registry."

Anyone who created thousands/millions of things - how did you interact with AWS IoT?

Ok, so I found it. Here it is possible to manage all the AWS IoT things:

http://docs.aws.amazon.com/iot/latest/apireference/API_Operations.html

anujdeshpande has given some headsup on this area.

"Automatically" means you can look at the following steps:

  1. When do you want to create a thing automatically? What is that triggers that create-thing process?
  2. Check this link for Just In Time registration of the devices
  3. You can create a Lambda function mapped to the AWS API Gateway which can invoke the AWS-IOT SDK to create a thing.
  4. Look at this page where they have created 50+ things using Javascript using aws-sdk library using serverless architecture approach

Another interesting way to do this is to use Just-in-Time registration - basically you upload the CA certificate that you used to sign keys while manufacturing.

This has some awesome advantages -

  • Your thing won't be created till your device first tries to connect to AWS IoT. So no blanks on your AWS account.
  • You won't have to mix your AWS IoT thing creation with the manufacturing process. Which is great because you don't want to give your Chinese (most likely scenario) manufacturers direct access to your AWS account

More about this

Link below is an example of creating 50 Iot devices, and implementing a Serverless AWS IoT Backend with AWS Lambda and Amazon DynamoDB. You can vary the devices to the number you like; It is done using the earlier version of AWS Iot Platform. Have a look.

https://aws.amazon.com/blogs/compute/implementing-a-serverless-aws-iot-backend-with-aws-lambda-and-amazon-dynamodb/

Here is the code to create 50 Iot Things

var AWS = require('aws-sdk');
AWS.config.region = 'ap-northeast-1';

var crypto = require('crypto');
var endpoint = "<endpoint prefix>.iot.<region>.amazonaws.com";
var iot = new AWS.Iot();
var iotdata = new AWS.IotData({endpoint: endpoint});
var topic = "registration";
var type = "MySmartIoTDevice"


//Create 50 AWS IoT Things
for(var i = 1; i < 51; i++) {
  var serialNumber = "SN-"+crypto.randomBytes(Math.ceil(12/2)).toString('hex').slice(0,15).toUpperCase();
  var clientId = "ID-"+crypto.randomBytes(Math.ceil(12/2)).toString('hex').slice(0,12).toUpperCase();
  var activationCode = "AC-"+crypto.randomBytes(Math.ceil(20/2)).toString('hex').slice(0,20).toUpperCase();
  var thing = "myThing"+i.toString();
  var thingParams = {
    thingName: thing
  };

  iot.createThing(thingParams).on('success', function(response) {
    //Thing Created!
  }).on('error', function(response) {
    console.log(response);
  }).send();



//Checking all devices were created

iot.listThings().on('success', function(response) {
  var things = response.data.things;
  var myThings = [];
  for(var i = 0; i < things.length; i++) {
    if (things[i].thingName.includes("myThing")){
      myThings[i]=things[i].thingName;
    }
  }

  if (myThings.length == 50){
    console.log("myThing1 to 50 created and registered!");
  }
}).on('error', function(response) {
  console.log(response);
}).send();

You can do that by using AWS IoT SDKs. You can find supported AWS IoT SDKs here . For doing that you will need proper credentials and policies which the simplest one would be AWS account credentials (Access key along with secret key from IAM or Cognito user credentials, ...).

For creating things automatically, you can use AWS IoT Device SDK for Python which is Boto3. You can find more information about it here . The following codes show the example of creating a thing in AWS IoT automatically using python and AWS credentials:

import boto3

client = boto3.client('iot')

response = client.create_thing(
    thingName=[NameOfThing],
    thingTypeName=[ThingType],
    attributePayload={
        'attributes': {
            'string': 'string'
        },
        'merge': True|False
    },
    billingGroupName='string'
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM