-
-
Save 85degree/b80a8e485ce9a92ec9621e406c03575e to your computer and use it in GitHub Desktop.
Writing data to Dynamodb from Serverless
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'; | |
const doc = require('dynamodb-doc'); | |
const dynamo = new doc.DynamoDB(); | |
const Q = require('kew'); | |
module.exports.handler = (event, context, callback) => { | |
function putItem() { | |
const defer = Q.defer(); | |
const query = event.queryStringParameters; | |
const params = { | |
TableName: 'test', | |
Item: { | |
customerId: query.customerId, | |
durationInHours: parseInt(query.durationInHours), | |
} | |
}; | |
dynamo.putItem(params, defer.makeNodeResolver()); | |
return defer.promise; | |
} | |
function response(data, statusCode) { | |
const response = { | |
statusCode: statusCode, | |
body: JSON.stringify({ | |
data: data, | |
input: event, | |
}), | |
}; | |
callback(null, response); | |
} | |
putItem() | |
.then(data => response(data, 200)) | |
.fail(err => response(err, 500)); | |
}; |
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
customerId=abc&durationInHours=447 |
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
service: aws-nodejs | |
provider: | |
name: aws | |
runtime: nodejs4.3 | |
stage: dev | |
region: ap-southeast-2 | |
iamRoleStatements: | |
- Effect: "Allow" | |
Action: | |
- dynamodb:GetItem | |
- dynamodb:BatchGetItem | |
- dynamodb:Query | |
- dynamodb:PutItem | |
- dynamodb:UpdateItem | |
- dynamodb:DeleteItem | |
- dynamodb:BatchWriteItem | |
Resource: "arn:aws:dynamodb:ap-southeast-2:*:*" | |
functions: | |
hello: | |
handler: index.handler | |
events: | |
- http: | |
path: index | |
method: post |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment