Senuda Jayalath
Senuda Jayalath

Reputation: 131

How to publish to AWS Shadow from lambda

This is the code Iam using,

var AWS = require('aws-sdk');

var iotdata = new AWS.IotData({
  endpoint: '###########.iot.ap-south-1.amazonaws.com'
});

exports.handler = async (event) => {

  var params = {
  payload: Buffer.from('...') || 'STRING_VALUE'  
  encoded on your behalf */, /* required */
  thingName: 'ESP32', /* required */
  //shadowName: 'STRING_VALUE'
 };
 iotdata.updateThingShadow(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
 });
  
};

I referred to this from the given link, https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/IotData.html#updateThingShadow-property

But I cant update my shadow. I also dont understand in which format I should include the payload.

I am running this on AWS Lambda function in Nodejs 12.x environment. I also dont receive any errors in cloudwatch . Cloudwatch tells me execution result has succeeded? Can you help me?

I have already given permission for Lambda to updateShadow.

Upvotes: 0

Views: 423

Answers (2)

Sarthak Jain
Sarthak Jain

Reputation: 2096

The issue here is that you are using the async Lambda handler. So while your updateThingShadow() call is in progress, the Lambda function completes and exits. You need to wait for that call to complete. There are two ways to do that -

Approach 1: (A simpler async-await syntax)

var AWS = require('aws-sdk');

var iotdata = new AWS.IotData({
  endpoint: '###########.iot.ap-south-1.amazonaws.com'
});

exports.handler = async (event) => {

  var params = {
  // Define params
  };
  try {
        const data = await iotdata.updateThingShadow(params).promise();
        console.log(data);
  } catch (err) {
        console.log(err);
  }
};

Approach 2: (If you prefer using the callback approach)

var AWS = require('aws-sdk');

var iotdata = new AWS.IotData({
  endpoint: '###########.iot.ap-south-1.amazonaws.com'
});

exports.handler = async (event) => {

  var params = {
  // Define params
  };
  await new Promise((resolve, reject) => {
      iotdata.updateThingShadow(params, function(err, data) {
         if (err) { 
            console.log(err, err.stack); // an error occurred
            reject(err); // End the promise with an error
         } else {
            console.log(data); // successful response
            resolve(data); // End the promise with success
         }
      });
   }) 
};

Upvotes: 0

Senuda Jayalath
Senuda Jayalath

Reputation: 131

This the final correct code .....

var AWS = require('aws-sdk');

var iotdata = new AWS.IotData({
  endpoint: '#######.iot.ap-south-1.amazonaws.com'
});

exports.handler = async (event) => {

var POST_DATA = JSON.stringify({"state":{"desired":{"state":10}}});
await new Promise((resolve, reject) => {
   var params = {
  payload:POST_DATA , 
  
  thingName: 'ESP32', /* required */
  //shadowName: 'STRING_VALUE'
 };
  iotdata.updateThingShadow(params, function(err, data) {
      if (err) { 
         console.log(err, err.stack); 
         console.log("error................")// an error occurred
         reject(err);
      } else {
         console.log(data);           // successful response
         resolve(data)
      }
 });
})

}


Upvotes: 1

Related Questions