Skip to content

Instantly share code, notes, and snippets.

@ybouhjira
Created September 12, 2018 23:22
Show Gist options
  • Select an option

  • Save ybouhjira/d4978b2fdb0626941614a8c0b17f4f02 to your computer and use it in GitHub Desktop.

Select an option

Save ybouhjira/d4978b2fdb0626941614a8c0b17f4f02 to your computer and use it in GitHub Desktop.

Revisions

  1. ybouhjira created this gist Sep 12, 2018.
    47 changes: 47 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    console.log('Loading function');

    const doc = require('dynamodb-doc');

    const dynamo = new doc.DynamoDB();


    /**
    * Demonstrates a simple HTTP endpoint using API Gateway. You have full
    * access to the request and response payload, including headers and
    * status code.
    *
    * To scan a DynamoDB table, make a GET request with the TableName as a
    * query string parameter. To put, update, or delete an item, make a POST,
    * PUT, or DELETE request respectively, passing in the payload to the
    * DynamoDB API as a JSON body.
    */
    exports.handler = (event, context, callback) => {
    //console.log('Received event:', JSON.stringify(event, null, 2));

    const done = (err, res) => callback(null, {
    statusCode: err ? '400' : '200',
    body: err ? err.message : JSON.stringify(res),
    headers: {
    'Content-Type': 'application/json',
    },
    });

    done({message: 'This is a fucking AWS Lambda function'});

    switch (event.httpMethod) {
    case 'DELETE':
    dynamo.deleteItem(JSON.parse(event.body), done);
    break;
    case 'GET':
    dynamo.scan({ TableName: event.queryStringParameters.TableName }, done);
    break;
    case 'POST':
    dynamo.putItem(JSON.parse(event.body), done);
    break;
    case 'PUT':
    dynamo.updateItem(JSON.parse(event.body), done);
    break;
    default:
    done(new Error(`Unsupported method "${event.httpMethod}"`));
    }
    };