0

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 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.