0

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):

Table

I'm not sure what I'm missing here, do you have any ideas? Thanks.

Dan
  • 25
  • 4
  • 1
    https://stackoverflow.com/a/36507469/17896613 Does this answer your question? – Kaustubh Khavnekar Jan 22 '22 at 19:26
  • 1
    It did give me some insight; I was using 'visitor_count' as a sort key when it was not necessary to do so. I made a new table with only one key and made visitor_count an attribute and the code worked. Beginner's mistake, I'm fairly new at working with NoSQL databases. – Dan Jan 22 '22 at 19:48
  • 3
    An important thing to recognize is that in order to update (or delete) an item in a NoSQL table, you need to present the entire key of the item because it uniquely identifies the item. If the item has a composite key, comprised of partition key and sort key, then you must present both. – jarmod Jan 22 '22 at 20:46

0 Answers0