Skip to content

Instantly share code, notes, and snippets.

@sebykrueger
Created October 8, 2015 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebykrueger/9970f4325afc6caea5e0 to your computer and use it in GitHub Desktop.
Save sebykrueger/9970f4325afc6caea5e0 to your computer and use it in GitHub Desktop.
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