Skip to content

Instantly share code, notes, and snippets.

@ma2shita
Last active August 4, 2019 15:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ma2shita/7cfc919010bf9050a6edea2b741a31ee to your computer and use it in GitHub Desktop.
Save ma2shita/7cfc919010bf9050a6edea2b741a31ee to your computer and use it in GitHub Desktop.
AWS IoT Core's shadow updator (Invoke from 1-Click)
/* based on https://github.com/j3tm0t0/1-click/blob/master/functions/aws-iot-shadow/index.js */
/* required placement params in AWS IoT 1-click
- thingName: for AWS IoT Core's thingName
*/
const endpoint = process.env['ENDPOINT'];
const accessKeyId = process.env['ACCESS_KEY_ID'];
const secretAccessKey = process.env['SECRET_ACCESS_KEY'];
const AWS = require('aws-sdk');
const iotdata = new AWS.IotData({endpoint, accessKeyId, secretAccessKey, region:'us-west-2'});
exports.handler = async (event) => {
const clickType = event.deviceEvent.buttonClicked.clickType;
const v = clickType === 'SINGLE' ? 1 : 0;
const payload = JSON.stringify({state:{desired:{relay:v}}});
const thingName = event.placementInfo.attributes.thingName;
const params = {thingName, payload};
console.log(params);
try {
const result = await iotdata.updateThingShadow(params).promise();
console.log(result);
return {statusCode: 200, body: JSON.stringify('Success')};
} catch (err) {
console.log(err);
return {statusCode: 501, body: JSON.stringify(err)};
}
};
@j3tm0t0
Copy link

j3tm0t0 commented May 10, 2019

changing line 10 to specify region: 'us-west-2', attendees do not need to care which region to place lambda function ( for better latency, us-west-2 will be the best though)

10c10
< const iotdata = new AWS.IotData({endpoint, accessKeyId, secretAccessKey});
---
> const iotdata = new AWS.IotData({endpoint, accessKeyId, secretAccessKey, region:'us-west-2'});

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