DynamoDB put-item ConditionalCheckFailedException

Your goal is to save both records. There are 2 issues here

  1. With your choice of hash and range key it is impossible to save both records

The combination of hash and range key make a record unique. Event 1 and event 2 have the same values for hash and range key. Therefore the second put-item wil simply overwrite the first record.

  1. Your ConditionExpression prevents the replacement of record 1 by record 2

The ConditionExpression is evaluated just before putting a record. Your expression fails because when your event 2 is about to be inserted, DynamoDB discovers that a record with aggregateId “id1” already exists. The condition fails on "attribute_not_exists(aggregateId)", and you receive the ConditionalCheckFailedException This expression prevents overwriting of record 1 by record 2.

If you want to save both records you will have to come up with another choice of hash key and/or range key that better represents the unicity of your data item. You can not solve that with a ConditionExpression.


I received similar error.

The conditional request failed (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ConditionalCheckFailedException; Request ID: SMNKMSKJNSHBSGHVGHSB)

In my case I had not added sort key in my table and my second item had the same primary key as of first item. It worked after I added sort key.