Skip to content

Instantly share code, notes, and snippets.

@lhahne lhahne/serverless.js
Last active Jan 29, 2016

Embed
What would you like to do?
var AWS = require("aws-sdk");
AWS.config.update({
region: "eu-west-1",
});
var dynamodbDoc = new AWS.DynamoDB.DocumentClient()
var table = "serverless-counter";
// Increment an atomic counter
var params = {
TableName: table,
Key: {
"value": "counter",
},
UpdateExpression: "set kounter = kounter + :val",
ExpressionAttributeValues:{
":val": 1
},
ReturnValues:"UPDATED_NEW"
};
exports.handler = function(event, context) {
console.log("Updating the item...");
dynamodbDoc.update(params, function(err, data) {
if (err) {
console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
context.succeed({counter: data.Attributes.kounter});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.