Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
lambda play
/**
* AWS Module: Action: Modularized Code
*/
// Export For Lambda Handler
module.exports.run = function(event, context, cb) {
return cb(null, action());
};
// Your Code
var action = function() {
console.log('Loading function');
var aws = require('aws-sdk');
var ddb = new aws.DynamoDB({
region: "us-east-1",
params: {TableName: "messages"}});
var params = {
"KeyConditions": {
"channel": {
"AttributeValueList": [{
"S": "default"
}],
"ComparisonOperator": "EQ"
}
},
"Limit": 20,
"ScanIndexForward":false
}
console.log("Querying DynamoDB");
var messageData = {
messages: []
};
ddb.query(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
} else {
console.log('result: ' + JSON.stringify(response)); // successful response
for (var i = response.Items.length - 1; i >= 0; i--) {
ii = response.Items[i];
var message = {};
message['name'] = ii.name['S'];
message['message'] = ii.message['S'];
message['channel'] = ii.channel['S'];
message['timestamp'] = Number(ii.timestamp['N']);
messageData.messages.push(message);
}
}
});
return messageData;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment