My Lambda function is supposed to increment a value in a dynamodb table, I'm using the .update API as an atomic counter. The value is incremented twice! I tried to change the increment variable value and it always increments twice. For example: the value in my DynamoDB table is 2, and the increment value is 1. I test the Lambda function, and the dynamoDB value is 4 (instead of 3).
The same code (without the handler function) works successfully from outside Lambda (executed from VSCode using the JS SDK) What could be the issue?
'use strict';
const AWS = require("aws-sdk");
AWS.config.update({ region: "me-central-1" });
const docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event) => {
await docClient.update(
{
TableName: "visitor-counter-table",
Key: {
"visitor-counter": "counter",
},
UpdateExpression: "set #num = #num + :incr",
ExpressionAttributeNames: {
"#num": "number",
},
ExpressionAttributeValues: {
":incr": 1,
},
},
(err, data) => {
err ? console.log(err) : console.log(data);
}
).promise();
return {"statusCode": 200, "body": "number added to DDB Success"}
};
Here is the code i ran on VSCode:
const AWS = require("aws-sdk");
AWS.config.update({ region: "me-central-1" });
const docClient = new AWS.DynamoDB.DocumentClient();
docClient.update(
{
TableName: "visitor-counter-tbl",
Key: {
"visitor-counter": "counter",
},
UpdateExpression: "set #num = #num + :incr",
ExpressionAttributeNames: {
"#num": "number",
},
ExpressionAttributeValues: {
":incr": 1,
},
},
(err, data) => {
err ? console.log(err) : console.log(data);
}
);