Skip to content

Instantly share code, notes, and snippets.

@doomhz
Last active July 21, 2016 15:32
Show Gist options
  • Save doomhz/153cc01b1b1f6185f63f3accdd224ccf to your computer and use it in GitHub Desktop.
Save doomhz/153cc01b1b1f6185f63f3accdd224ccf to your computer and use it in GitHub Desktop.
Lambda example with NodeJS and API Gateway, pulled from AWS
'use strict';
console.log('Loading function');
let doc = require('dynamodb-doc');
let dynamo = new doc.DynamoDB();
/**
* Provide an event that contains the following keys:
*
* - operation: one of the operations in the switch statement below
* - tableName: required for operations that interact with DynamoDB
* - payload: a parameter to pass to the operation being performed
*/
exports.handler = (event, context, callback) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
const operation = event.operation;
if (event.tableName) {
event.payload.TableName = event.tableName;
}
switch (operation) {
case 'create':
dynamo.putItem(event.payload, callback);
break;
case 'read':
dynamo.getItem(event.payload, callback);
break;
case 'update':
dynamo.updateItem(event.payload, callback);
break;
case 'delete':
dynamo.deleteItem(event.payload, callback);
break;
case 'list':
dynamo.scan(event.payload, callback);
break;
case 'echo':
callback(null, event.payload);
break;
case 'ping':
callback(null, 'pong');
break;
default:
callback(new Error(`Unrecognized operation "${operation}"`));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment