Skip to content

Instantly share code, notes, and snippets.

@MizukiSonoko
Last active November 13, 2015 16:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MizukiSonoko/7ffd05de6b0c42ad395d to your computer and use it in GitHub Desktop.
Save MizukiSonoko/7ffd05de6b0c42ad395d to your computer and use it in GitHub Desktop.
[Lambda's code] REST API using DynamoDB.
var AWS = require('aws-sdk');
var dynamo = new AWS.DynamoDB({region: 'ap-northeast-1'});
exports.handler = function(event, context) {
var id = event.id || "EXCEPT_VALUE";
delete event.id;
var params = {
TableName: 'Test',
"IndexName": "id-index",
"KeyConditionExpression": "#id = :id",
ExpressionAttributeNames : {
"#id":"id"
},
"ExpressionAttributeValues":{
":id":{"S":id}
}
};
var operation = event.operation;
delete event.operation;
dynamo.query(params, function(err, data){
context.succeed(JSON.parse(JSON.stringify(data, function(key, value) {
if (value.hasOwnProperty('S')) {
return value['S'];
}
if (value.hasOwnProperty('BOOL')) {
return value['BOOL'];
}
if (value.hasOwnProperty('N')) {
return value['N'];
}
return value;
})
));
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment