Created
February 20, 2019 14:55
-
-
Save questsin/dc939d5f0b479af5968fef68e88e10ef to your computer and use it in GitHub Desktop.
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(); | |
/** | |
* 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', | |
}, | |
}); | |
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}"`)); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment