We will build a Lambda function to filter an AWS IoT Button message. This message is parsed on an event from the handler function. To store a message into AWS DynamoDB, we use the putItem() function from the DynamoDB object with the passing JSON message.
The following is the complete program for the AWS Lambda function:
var AWS = require('aws-sdk');
var ddb = new AWS.DynamoDB();
exports.handler = (event, context, callback) => {
var params = {
TableName: 'aws-iot-button-db',
Item: {
'msg-id': {S:new Date().getTime().toString()},
'CLICKED' : {S:event.clickType},
'BUTTON_ID' : {S:event.serialNumber},
}
};
// Call DynamoDB to add the item to the table
ddb.putItem(params, function(err, data) {
if (err) {
callback(err, 'Error');
} else {
callback(null, 'Success');
}
});
};
Once done, please save all the programs.
We are done writing a program for AWS Lambda. Next, we will test it using AWS IoT Button.