-
-
Save ma2shita/7cfc919010bf9050a6edea2b741a31ee to your computer and use it in GitHub Desktop.
AWS IoT Core's shadow updator (Invoke from 1-Click)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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)}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)