Search code examples
amazon-web-servicesaws-iotaws-sdk-nodejs

aws javascript sdk registerThing template body does not accept valid json string


I am passing template json to the registerThing function as shown in below code part.

const templateJson = {"Parameters":{"Attr1":{"Type": "String"},"Attr2": {"Type": "String"},"AWS::IoT::Certificate::Id":{"Type": "String"}},"Resources":{"certificate":{"Properties": {"CertificateId":{"Ref": "AWS::IoT::Certificate::Id"},"Status": "Active"},"Type": "AWS::IoT::Certificate"},"policy":{"Properties": {"PolicyDocument": "<<Inline olicy document>>"},"Type": "AWS::IoT::Policy"},"thing": {"OverrideSettings":{"AttributePayload": "MERGE","ThingGroups": "DO_NOTHING","ThingTypeName": "REPLACE"},"Properties":{"AttributePayload":{},"ThingGroups":["mygroup"],"ThingName": {"Ref": "SerialNumber"},"ThingTypeName": "testname"},"Type": "AWS::IoT::Thing"}},"DeviceConfiguration":{}};

const provisionTemplateBody = JSON.stringify(templateJson);

And then using this to construct function parameters as

 var paramsRegisterThing = {
        templateBody: provisionTemplateBody,
        parameters: provisioningTemplateParams
    };

When calling registerThing with this, I get exception as below

"errorType": "InvalidRequestException",
  "errorMessage": "Invalid registration template. Template format error: unsupported type or structure...

I have already added JSON.stringify as suggested in some other questions here on stackoverflow, but it does not help.

How can I make this work? Or is there any other way to pass template name or ARN instead of body to register thing?


Solution

  • Few things I noticed:

    • Remove empty block of DeviceConfiguration
    • There is Ref to SerialNumber , which is missing in Parameter block.
    • You have two other attributes Attr1 & Attr2 in parameters which are not actually used in Resources block.

    If both template and parameters are two Javascript objects. templateBody should be a sting and parameters should be key-value map.

     iot.registerThing(
          {
            templateBody: JSON.stringify(template1),
            parameters: parms,
          },
          function (err, data) {}
     )