The purpose of this code will be to increment the value of visitor_count by 1 each time:
import json
import boto3
dynamodb = boto3.client('dynamodb')
def lambda_handler(event, context):
response = dynamodb.update_item(
TableName='VisitorCount',
Key={
'id': {
'S': 'count'
}
},
UpdateExpression='SET visitor_count = visitor_count + :val',
ExpressionAttributeValues={
':val': {
'N': '1'
}
},
ReturnValues="UPDATED_NEW"
)
Error response:
{
"errorMessage": "An error occurred (ValidationException) when calling the UpdateItem operation: The provided key element does not match the schema",
"errorType": "ClientError",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 17, in lambda_handler\n response = dynamodb.update_item(\n",
" File \"/var/runtime/botocore/client.py\", line 386, in _api_call\n return self._make_api_call(operation_name, kwargs)\n",
" File \"/var/runtime/botocore/client.py\", line 705, in _make_api_call\n raise error_class(parsed_response, operation_name)\n"
]
}
My DynamoDB Table(id is partition key and visitor_count is sort key):
I'm not sure what I'm missing here, do you have any ideas? Thanks.