Skip to content

Instantly share code, notes, and snippets.

@ronreiter
Created April 1, 2017 07:55
Show Gist options
  • Save ronreiter/eab7f8408de334de18bc6be840bfe0cd to your computer and use it in GitHub Desktop.
Save ronreiter/eab7f8408de334de18bc6be840bfe0cd to your computer and use it in GitHub Desktop.
Lambda + API Gateway + DynamoDB example
'use strict';
console.log('Loading function');
const doc = require('dynamodb-doc');
const dynamo = new doc.DynamoDB();
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',
},
});
switch (event.queryStringParameters.action) {
case 'delete':
dynamo.deleteItem({
TableName: 'mytable',
Key: { id: event.queryStringParameters.id }
}, done);
break;
case 'get':
dynamo.scan({
TableName: 'mytable'
}, done);
break;
case 'put':
dynamo.putItem({
TableName: 'mytable',
Item: {
id: event.queryStringParameters.id,
value: event.queryStringParameters.value
} }, done);
break;
case 'update':
dynamo.updateItem({
TableName: 'mytable',
Item: {
id: event.queryStringParameters.id,
value: event.queryStringParameters.value
} }, done);
break;
default:
done(new Error(`Unsupported method "${event.queryStringParameters.action}"`));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment