This is the code I'm currently using
var AWS = require('aws-sdk');
var iotdata = new AWS.IotData({
endpoint: '######.iot.ap-south-1.amazonaws.com'
});
exports.handler = async (event) => {
await new Promise((resolve, reject) => {
var params = {
payload: Buffer.from('...') || 'STRING_VALUE' ,
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)
}
});
})
}
I have two questions regarding this,
There seems to be some mistake in my code. Can you please help me to correct this.
In which format should I give the payload. I dont really understand the buffer type.
Somehow when I run the code error........... is seen in the execution result.(This is from the console log)
I am running this code inside a AWS lambda in a Node js 12.x environemnt. I have given proper permission for lambda to publish data into the shadow.
Thank you..
This is the error message,
Response:
{
"errorType": "InvalidRequestException",
"errorMessage": "Payload contains invalid json",
"trace": [
"InvalidRequestException: Payload contains invalid json",
" at Object.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:51:27)",
" at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/rest_json.js:55:8)",
" at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
" at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)",
" at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:688:14)",
" at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)",
" at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)",
" at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10",
" at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)",
" at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:690:12)"
]
}
Request ID:
"2574b62-4b-41e7-a725-644e8d78"
Function logs:
START RequestId: 24574b62-4be3-41e7-a725-694a444e8d78 Version: $LATEST
2020-11-14T04:39:40.303Z 24574b62-4be3-41e7-a725-694a444e8d78 INFO InvalidRequestException: Payload contains invalid json
at Object.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:51:27)
at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/rest_json.js:55:8)
at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:688:14)
at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:690:12) {
code: 'InvalidRequestException',
time: 2020-11-14T04:39:40.243Z,
requestId: '219f8c5f-2bf1-9ad0-ff5d-589940833983',
statusCode: 400,
retryable: false,
retryDelay: 68.01088760246567
} InvalidRequestException: Payload contains invalid json
at Object.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:51:27)
at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/rest_json.js:55:8)
at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:688:14)
at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:690:12)
2020-11-14T04:39:40.340Z 24574b62-4be3-41e7-a725-694a444e8d78 INFO error................
2020-11-14T04:39:40.342Z 24574b62-4be3-41e7-a725-694a444e8d78 ERROR Invoke Error {"errorType":"InvalidRequestException","errorMessage":"Payload contains invalid json","code":"InvalidRequestException","message":"Payload contains invalid json","time":"2020-11-14T04:39:40.243Z","requestId":"219f8c5f-2bf1-9ad0-ff5d-589940833983","statusCode":400,"retryable":false,"retryDelay":68.01088760246567,"stack":["InvalidRequestException: Payload contains invalid json"," at Object.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:51:27)"," at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/rest_json.js:55:8)"," at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)"," at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)"," at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:688:14)"," at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)"," at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)"," at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10"," at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)"," at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:690:12)"]}
END RequestId: 24574b62-4be3-41e7-a725-694a444e8d78
REPORT RequestId: 24574b62-4be3-41e7-a725-694a444e8d78 Duration: 736.20 ms Billed Duration: 800 ms Memory Size: 128 MB Max Memory Used: 85 MB Init Duration: 392.36 ms