Lambda python boto3 update_item series error

0

Issue Lambda Python Issues executing an update_item.

Code responseSeQ = table.update_item( //this is line79 Key={ 'ID': '1', 'Highest' : '1' }, UpdateExpression='Set LargestSeQNo = SeqNo', ExpressionAttributeValues={ 'LargestSeQNo' : SeqNo } )

The table consists of:

  • ID // the PK
  • Highest // Sortkey
  • LargestSeQNo // String

Objective LargestSeQNo only ever has numbers in it starting with 1. The intent is to read this with get_item (works) then increment the number (works) then change the number back to a string and update the attribute in the table. The variable SeqNo is that string variable in Python.

Error "errorMessage": "An error occurred (ValidationException) when calling the UpdateItem operation: ExpressionAttributeValues contains invalid key: Syntax error; key: "LargestSeQNo"", "errorType": "ClientError", "requestId": "be7af4e0-040d-4387-911d-bc6a3be9e47b", "stackTrace": [ " File "/var/task/lambda_function.py", line 79, in lambda_handler\n responseSeQ = table.update_item(\n",

Syntax error; key: "LargestSeQNo" - In the code, LargestSeQNo is addressed as an attribute, not a key - ExpressionAttributeValues={ 'LargestSeQNo' : SeqNo } SeqNo - the python variable, a string, containing the that to update in the DynamoDB table, attribute LargestSeQNo, a string

Help, please!

  • I've gotten a little further, but no cigar yet....Here's the current code

    responseSeQ = table.update_item( //line 79
        Key={ 'ID': '1', 'Highest' : '1' },
        ExpressionAttributeNames='LargestSeQNo',
        UpdateExpression='Set LargestSeQNo = SeqNo',
        ExpressionAttributeValues={ 'LargestSeQNo' : SeqNo }
    )
    

    Error "errorMessage": "'str' object has no attribute 'update'", "errorType": "AttributeError", "requestId": "f646ba1a-8665-4332-93cd-0f8562988a0c", "stackTrace": [ " File "/var/task/lambda_function.py", line 79, in lambda_handler\n responseSeQ = table.update_item(\n",

profile picture
Petrus
asked a month ago174 views
2 Answers
1
Accepted Answer

For ExpressionAttributeNames, map a placeholder to the actual attribute name, e.g., {'#lsq': 'LargestSeQNo'}, and for ExpressionAttributeValues, map a placeholder to the new value, e.g., {':val1': SeqNo}.

ℹ️ ExpressionAttributeNames and ExpressionAttributeValues are dictionaries not string.

ℹ️ Use the # character in an expression to dereference an attribute name.

ℹ️ Use the : (colon) character in an expression to dereference an attribute value.


🔗 For more information about ExpressionAttributeNames and ExpressionAttributeValues check update_item in Boto3.

profile picture
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
  • Good points - documented!

0

First, the error message in your comment "'str' object has no attribute 'update'" tells me that you're calling a method update but I don't see that in your code anywhere. I can see table.update_item but that's not the same thing. Have you deployed your Lambda before you are testing it?

Second, to the error message in the original question: ExpressionAttributeValues contains invalid key you might try this instead:

responseSeQ = table.update_item( //line 79
    Key={ 'ID': '1', 'Highest' : '1' },
    ExpressionAttributeNames={'#LargestSequenceNumber':'LargestSeQNo'},
    UpdateExpression='set #LargestSequenceNumber = :SequenceNumber',
    ExpressionAttributeValues={ ':SequenceNumber':SeqNo }
)

Note that you can also (sometimes) go simpler depending on whether your attributes are DynamoDB keywords or not:

responseSeQ = table.update_item( //line 79
    Key={ 'ID': '1', 'Highest' : '1' },
    AttributeUpdate={'LargestSeQNo':{''Value':SeqNo'}}
)
profile pictureAWS
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
  • The 2nd simplier approach complains about a syntax error in the AttributeUpdats={'LargestSeQNo':{''Value':SeqNo'}} line.

    BUT, Good news, the 1st approach worked! Thank you for your help.

  • Thanks - fixed. I was transcribing from another computer rather than copy/pasting.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions