Skip to content

Instantly share code, notes, and snippets.

@Asimov4
Forked from markusklems/lambda-dynamo
Last active November 7, 2015 05:28
Show Gist options
  • Save Asimov4/a890eccabe6f5bc578d7 to your computer and use it in GitHub Desktop.
Save Asimov4/a890eccabe6f5bc578d7 to your computer and use it in GitHub Desktop.
Short aws lambda sample program that puts an item into dynamodb
// create an IAM Lambda role with access to dynamodb
// Launch Lambda in the same region as your dynamodb region
// (here: us-east-1)
// dynamodb table with hash key = user and range key = datetime
console.log('Loading event');
var doc = require('dynamodb-doc');
var dynamodb = new doc.DynamoDB();
exports.handler = function(event, context) {
console.log(JSON.stringify(event, null, ' '));
dynamodb.listTables(function(err, data) {
console.log(JSON.stringify(data, null, ' '));
});
var tableName = "chat";
var datetime = new Date().getTime().toString();
dynamodb.putItem({
"TableName": tableName,
"Item" : {
"user": event.user,
"date": datetime,
"msg": event.msg
}
}, function(err, data) {
if (err) {
context.done('error','putting item into dynamodb failed: '+err);
}
else {
console.log('great success: '+JSON.stringify(data, null, ' '));
context.done('K THX BY');
}
});
};
// sample event
//{
// "user": "bart",
// "msg": "hey otto man"
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment