下面是一个使用AWS Lambda在DynamoDB中更新映射中项目的示例代码:
import boto3
def lambda_handler(event, context):
# 获取DynamoDB资源
dynamodb = boto3.resource('dynamodb')
# 获取表格对象
table = dynamodb.Table('your-dynamodb-table-name')
# 更新映射中的项目
response = table.update_item(
Key={
'key': 'item-key' # 替换为你的项目的键
},
UpdateExpression='SET attribute1 = :val1, attribute2 = :val2', # 替换为你想要更新的属性和值
ExpressionAttributeValues={
':val1': 'new-value-for-attribute1',
':val2': 'new-value-for-attribute2'
}
)
# 打印响应结果
print(response)
# 返回响应结果
return response
在上面的代码中,你需要替换your-dynamodb-table-name
为你的DynamoDB表格的名称,item-key
为你要更新的项目的键,attribute1
和attribute2
为你要更新的属性名称,new-value-for-attribute1
和new-value-for-attribute2
为你要更新的属性的新值。
请确保你的Lambda函数有足够的权限来访问DynamoDB表格,可以通过在IAM角色中添加适当的策略来实现。