3
votes

I'm new at AWS, and I'm trying to update an item attribute in DynamoDB via Lambda. I have tried code in Node.JS and Python and both attempts return:

module initialization error: An error occurred (ValidationException) when calling the UpdateItem operation: The provided key element does not match the schema

Why is this happenning? Here is my table info:

  • Table name: High_Holiday_Auctions
  • Primary partition key: Aliyah_ID (String)
  • Primary sort key: HighestBidder (String)

So I'm using both the partition key and the sort key in my code, so I can't figure out what is causing the error

Lambda attempts with Python:

    from __future__ import print_function # Python 2/3 compatibility
            import boto3
    import json
    import decimal

    # Helper class to convert a DynamoDB item to JSON.
    class DecimalEncoder(json.JSONEncoder):
        def default(self, o):
            if isinstance(o, decimal.Decimal):
                if o % 1 > 0:
                    return float(o)
                else:
                    return int(o)
            return super(DecimalEncoder, self).default(o)

    dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

            table = dynamodb.Table('High_Holiday_Auctions')

            AliyahID = "YK_MaftirHaftarah"
            HighestBidder = "unclaimed"

            response = table.update_item(
                Key={
                    'AliyahID': AliyahID,
                    'HighestBidder': HighestBidder
                },
                UpdateExpression="set HighestBidder = :h, CurrentHighBid =:c,                              
    LastHighestBidder=:l",
                ExpressionAttributeValues={
                    ':h': "Harry S Truman",
                    ':c': "100",
                    ':l': "unclaimed"
                },
                ReturnValues="UPDATED_NEW"
            )

            print("UpdateItem succeeded:")
            print(json.dumps(response, indent=4, cls=DecimalEncoder))

Lambda attempts with Node.JS:

    const AWS = require('aws-sdk');
    const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});

    exports.handler = function index(event, context, callback) {
        var table = 'High_Holiday_Auctions';
        var   AliyahID = "YK_MaftirHaftarah";
        var  HighestBidder ="unclaimed"

            //updaate item unconditinoally
        var params ={
            TableName:table,
            Key:{
                "AliyahID": AliyahID,
            "HighestBidder": HighestBidder
            },
            UpdateExpression: "set HighestBidder =:h, CurrentHighBid =:c, LastHighestBidder=:l",
            ExpressionAttributeValues:{
                ":h":"George Burdell",
                ":c": "1000",
                ":l": "moe syslak"
            },

                ReturnValues:"UPDATED_NEW"
    };

    console.log("Updating the item...");
    docClient.update(params, function(err, data) {
        if (err) {
            console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
        } else {
            console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
        }
    });
    };

Similar existing questions that don't quite answer my issue:

1

1 Answers

1
votes

You need to use only the primary partition key in the Key field to get the item to update unless your table uses composite primary key.

My guess is that the table you're trying to update that Item is configured with a simple primary key.