Skip to content

Instantly share code, notes, and snippets.

@markusklems
Last active November 24, 2024 01:48
Show Gist options
  • Select an option

  • Save markusklems/1e7218d76d7583f1f7b3 to your computer and use it in GitHub Desktop.

Select an option

Save markusklems/1e7218d76d7583f1f7b3 to your computer and use it in GitHub Desktop.

Revisions

  1. markusklems renamed this gist Jan 19, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. markusklems revised this gist Jan 19, 2015. No changes.
  3. markusklems created this gist Jan 19, 2015.
    39 changes: 39 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    // 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 AWS = require('aws-sdk');
    var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

    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": {"S": event.user },
    "date": {"S": datetime },
    "msg": {"S": 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"
    //}