Created
October 8, 2015 09:26
-
-
Save sebykrueger/9970f4325afc6caea5e0 to your computer and use it in GitHub Desktop.
lambda play
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
/** | |
* 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