我使用lambda函数来处理dynamodb表。该函数是用python编写的,我刚刚使用了aws教程中的函数(这是一个学习AWS的个人项目)。它对"read“(get_item)和"create”(put_item)非常有效,但是"update“(update_item)抛出了这个错误,我不知道为什么。我使用的是使用NoSQL工作台构建的测试查询。当我在Workbench中为相同的查询点击"run“时,它可以工作,当我从命令行调用它时,它也可以工作。每当我遍历lambda函数时,它都会抛出这个错误,但就像我说过的,该函数在其他调用中运行良好。
lambda代码:
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))测试查询:
{
"operation": "update",
"tableName": "CCDynamoDB",
"payload": {
"Key": {
"id": {"S":"visitorCount"}
},
"UpdateExpression": "SET #7c680 = #7c680 + :7c680",
"ExpressionAttributeNames": {"#7c680":"currentCount"},
"ExpressionAttributeValues": {":7c680": {"N":"1"}}
}
}谢谢你的帮忙!
发布于 2021-10-16 14:30:37
您的有效负载有问题。尝试如下所示。为了更好地理解,建议您查看此document。
CCDynamoDB.update_item(
Key={
'id': 'visitorCount'
},
UpdateExpression='SET currentCount = :val1',
ExpressionAttributeValues={
':val1': 1
}
)https://stackoverflow.com/questions/69596269
复制相似问题