forked from codurance/serverless-gfd-it1-posts-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
49 lines (38 loc) · 1.18 KB
/
handler.js
File metadata and controls
49 lines (38 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
module.exports.hello = async event => {
var AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-1' });
var parse = AWS.DynamoDB.Converter.output;
// Create the DynamoDB service object
var ddb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });
var params = {
TableName: 'serverless-gfd-it1-posts-database',
Key: {
'id': { S: '1234' }
},
ProjectionExpression: 'posts'
};
return await new Promise((resolve, reject) => {
ddb.getItem(params, function (err, data) {
if (err) {
console.log("Error", err);
} else {
resolve({
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
},
statusCode: 200,
body: JSON.stringify(
parse({ "M": data.Item}).posts,
null,
2
),
})
}
});
});
// Call DynamoDB to read the item from the table
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};