Search code examples
amazon-web-servicesclojureamazon-dynamodbaws-cloudformationamazonica

"The conditional request failed" whereas the condition works


I am using Amazonica, a Clojure library to write to DynamoDB.

The following inserts an item into DynamoDB and updates its content if called a second time, which is expected.

(ddb/put-item cred
   :table-name table-name
   :item payload)

Now, the following inserts an item only the first time. Calling it a second time doesn't do anything, which is what I need.

(ddb/put-item cred
   :table-name table-name
   :condition-expression "attribute_not_exists(clientId)"
   :item payload)

However with the latest I am getting an error:

The conditional request failed (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ConditionalCheckFailedException;

... which doesn't really make the code deliverable. My CloudFormation template is very simple:

"Resources": {
  "ClientTable": {
    "Type": "AWS::DynamoDB::Table",
    "Properties": {
      "AttributeDefinitions": [
        { "AttributeName": "clientId", "AttributeType": "S" }
      ],
      "KeySchema": [
        { "AttributeName": "clientId", "KeyType": "HASH" }
      ],
      "ProvisionedThroughput": {
        "ReadCapacityUnits": { "Ref": "ReadCapacityUnits" },
        "WriteCapacityUnits": { "Ref": "WriteCapacityUnits" }
      },
      "TableName": "ClientTable"
    }
  }
}

Am I missing something?


Solution

  • Amazonica is not silently swallowing the fact that the item was not added. It's throwing an exception that you can catch in case you want to do something about it. Perhaps you want to:

    1. catch the exception
    2. make sure it's error code matches that one
    3. ignore it if it does.

    To test this, make sure that the same call does indeed work for new keys.