Home > OS >  Lambda calling update_item, error: Invalid UpdateExpression: Incorrect operand type for operator or
Lambda calling update_item, error: Invalid UpdateExpression: Incorrect operand type for operator or

Time:10-17

I'm using a lambda function to work with a dynamodb table. The function is written in python, and I just used the one from an aws tutorial (this is for a personal project to learn AWS). It's working great for "read" (get_item) and "create" (put_item), but "update" (update_item) is throwing this error, and I can't figure out why. I'm using a test query I built with NoSQL Workbench. When I hit "run" for the same query in Workbench, it works, and it also works when I call it from the command line. Whenever I go through the lambda function it throws this error, but like I said, the function works fine for the other calls.

The lambda code:

import boto3
import json


def handler(event, context):
    '''Provide an event that contains the following keys:

      - operation: one of the operations in the operations dict below
      - tableName: required for operations that interact with DynamoDB
      - payload: a parameter to pass to the operation being performed
    '''

    operation = event['operation']

    if 'tableName' in event:
        dynamo = boto3.resource('dynamodb').Table(event['tableName'])

    operationsDict = {
        'create': lambda x: dynamo.put_item(**x),
        'read': lambda x: dynamo.get_item(**x),
        'update': lambda x: dynamo.update_item(**x),
        'delete': lambda x: dynamo.delete_item(**x),
        'list': lambda x: dynamo.scan(**x),
        'echo': lambda x: x,
        'ping': lambda x: 'pong'
    }

    if operation in operationsDict:
        return operationsDict[operation](event.get('payload'))
    else:
        raise ValueError('Unrecognized operation "{}"'.format(operation))

The test query:

{
  "operation": "update",
  "tableName": "CCDynamoDB",
  "payload": {
    "Key": {
            "id": {"S":"visitorCount"}
        },
        "UpdateExpression": "SET #7c680 = #7c680   :7c680",
        "ExpressionAttributeNames": {"#7c680":"currentCount"},
        "ExpressionAttributeValues": {":7c680": {"N":"1"}}
  }
}

Thanks for your help!

CodePudding user response:

You have a problem in your payload. Try like below. For better understanding, suggesting you to take a look at this document.

CCDynamoDB.update_item(
    Key={
        'id': 'visitorCount'
    },
    UpdateExpression='SET currentCount = :val1',
    ExpressionAttributeValues={
        ':val1': 1
    }
)

  • Related