I am trying to update a current item in a dynamoDB table from a lambda function and am getting a syntax error with no information. I am baffled as I followed numerous online forums and blog posts to get to this stage, and the code seems identical. My put_item snippet works as intended so dynamoDB has been connected to the lambda function correctly. Any help would be really appreciated.
My code is as follows:
update = client.update_item(
TableName='sample',
Key={'id': {'S': body["id"]},
UpdateExpression="set cancelled = :g",
ConditionExpression = 'attribute_exists(id)',
ExpressionAttributeValues={
':g': "yes"
},
ReturnValues="UPDATED_NEW"
)
The logs I get are as follows:
{
"errorMessage": "Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 89)",
"errorType": "Runtime.UserCodeSyntaxError",
"requestId": "86785a61-c526-4647-9a2c-b51466ddb8a6",
"stackTrace": [
" File \"/var/task/lambda_function.py\" Line 89\n UpdateExpression=\"set canceled = :g\",\n"
]
}
After resolving this, I get the following error
{
"errorMessage": "Parameter validation failed:\nInvalid type for parameter ExpressionAttributeValues.:g, value: yes, type: <class 'str'>, valid types: <class 'dict'>",
"errorType": "ParamValidationError",
"requestId": "b7710541-b398-4f2f-9f67-7eaafb1426bc",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 84, in lambda_handler\n update = client.update_item(\n",
" File \"/var/runtime/botocore/client.py\", line 391, in _api_call\n return self._make_api_call(operation_name, kwargs)\n",
" File \"/var/runtime/botocore/client.py\", line 691, in _make_api_call\n request_dict = self._convert_to_request_dict(\n",
" File \"/var/runtime/botocore/client.py\", line 739, in _convert_to_request_dict\n request_dict = self._serializer.serialize_to_request(\n",
" File \"/var/runtime/botocore/validate.py\", line 360, in serialize_to_request\n raise ParamValidationError(report=report.generate_report())\n"
]
}
Looks like you're choosing to interact with DynamoDB at the lowest wire level so you need to pass the "S" string type hint:
":g": { "S": "yes" },
It can be confusing because you can interact with DynamoDB at various abstraction levels. I suggest going up a level and using .resource
instead of .client
.
Here's a sample where you don't need the "S" hints and things like that: https://github.com/aws-samples/aws-dynamodb-examples/blob/master/DynamoDB-SDK-Examples/python/WorkingWithItems/updating_item.py
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.