0
votes

I'm working on setting up a Python function in AWS Lambda to update a simple counter table in DynamoDB. Each time the function run, it should update the counter by 1. I'm hoping to make use of the Atomic Counter feature, but I currently keep getting the following error: Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: +, operand type: M"

My DynamoDB table only has two fields: SiteURL (partition key) and Site (the number that I want to increase with each update)

Here's the code - any ideas on what I am missing?

import decimal
import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('resumecounter')

response = table.update_item(
    Key = {'SiteURL': 'https://website.com', 'Site': 'N'},
    ExpressionAttributeNames = {'#Site': 'Site'},
    ExpressionAttributeValues = {':increase': {'N': '1'},},
    UpdateExpression = "set site = site + :increase",
    ReturnValues="UPDATED_NEW"
)
print("UPDATING ITEM")
print(response)
1

1 Answers

1
votes

I've fixed few things, your search key and attribute values. I have tested it on my env and this should work.

import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('resumecounter')

response = table.update_item(
    Key={'SiteURL': 'https://website.com'},
    ExpressionAttributeValues={':increase': 1},
    UpdateExpression="set site = site + :increase",
    ReturnValues="UPDATED_NEW"
)
print("UPDATING ITEM")
print(response)