Created
April 1, 2017 07:55
-
-
Save ronreiter/eab7f8408de334de18bc6be840bfe0cd to your computer and use it in GitHub Desktop.
Lambda + API Gateway + DynamoDB example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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