Skip to content

Instantly share code, notes, and snippets.

@groteworld
Last active June 5, 2020 14:49
Show Gist options
  • Save groteworld/af0577ea5c59e98843ef47325407da71 to your computer and use it in GitHub Desktop.
Save groteworld/af0577ea5c59e98843ef47325407da71 to your computer and use it in GitHub Desktop.
Example aws/aws-sdk-js IotData API Lambda usage
// An usage of AWS.IotData in Lambdas
//
// This example assumes some things
// 1. That you have a environment variable AWS_IOT_ENDPOINT. This is the url that you can find in AWS IoT dashboard settings
// 2. The lambda and your aws iot devices are on the same account and region
const AWS = require('aws-sdk');
const iotData = new AWS.IotData({ endpoint: process.env.AWS_IOT_ENDPOINT });
const handler = (event, context) => {
const params = {
topic: 'my/cool/topic',
payload: JSON.stringify(event)
}
iotData.publish(params, (err, res) => {
if (err) return context.fail(err);
console.log(res);
return context.succeed();
});
};
exports.handler = handler;
@CuyiGuaton
Copy link

Hi, I get the follow error. What should I do?

Response:
{
"errorMessage": "Forbidden",
"errorType": "ForbiddenException",
"stackTrace": [
"Object.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:48:27)",
"Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/rest_json.js:52:8)",
"Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20)",
"Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:77:10)",
"Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14)",
"Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)",
"AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)",
"/var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10",
"Request. (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)",
"Request. (/var/runtime/node_modules/aws-sdk/lib/request.js:685:12)"
]
}

Request ID:
"fbe5153b-fe07-11e7-b3ef-f5817081ded5"

Function Logs:
START RequestId: fbe5153b-fe07-11e7-b3ef-f5817081ded5 Version: $LATEST
2018-01-20T17:33:11.619Z fbe5153b-fe07-11e7-b3ef-f5817081ded5 {"errorMessage":"Forbidden","errorType":"ForbiddenException","stackTrace":["Object.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:48:27)","Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/rest_json.js:52:8)","Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20)","Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:77:10)","Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14)","Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)","AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)","/var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10","Request. (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)","Request. (/var/runtime/node_modules/aws-sdk/lib/request.js:685:12)"]}
END RequestId: fbe5153b-fe07-11e7-b3ef-f5817081ded5
REPORT RequestId: fbe5153b-fe07-11e7-b3ef-f5817081ded5 Duration: 679.24 ms Billed Duration: 700 ms Memory Size: 256 MB Max Memory Used: 33 MB

@CuyiGuaton
Copy link

nevermind, I solved adding this to roles

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "iot:*" ], "Resource": [ "*" ] } ] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment