0
votes

I have a DynamoDB table with a integer value, which needs to be incremented using Python/Boto3/AWS-Lambda environment.

userMetrics
{
  "userid": 1234567890,
  "likemetrics": 0,
  "lasttimestamp": 1604553995
}

I am trying to increment the likemetrics integer counter using atomic increment method dynamodb: how to increment a value in map, but fails during testing.

   responseupd = dydb_usertable.update_item(
        Key = {
            'userid': 1234567890
        },
        ExpressionAttributeValues: { ":inc": {N: "1"} },
        UpdateExpression: "ADD likemetrics :inc"
        })

The error thrown by console is

{
  "errorMessage": "Syntax error in module 'lambda_function': unindent does not match any outer indentation level (lambda_function.py, line 34)",
  "errorType": "Runtime.UserCodeSyntaxError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\" Line 34\n           responseupd = dydb_usertable.update_item(\n"
  ]
}
2

2 Answers

2
votes

Please carefuly look at the documentation here:

You are using mixed, Javascript and Python syntax. It should be corrected as below:

dydb_usertable.update_item(
    Key = {
        'userid': {
            'N': 1234567890
        }
    },
    ExpressionAttributeValues = { ":inc": {"N": "1"} },
    UpdateExpression = "ADD likemetrics :inc"
})
0
votes

See updated syntax:

dydb_usertable.update_item(
        Key={
            'userid': 1234567890
        },
        UpdateExpression="ADD likemetrics :inc",
        ExpressionAttributeValues={
            ':inc': 1
        },
        ReturnValues="UPDATED_NEW"
    )