Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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?

like image 741
Stéphane Bruckert Avatar asked Jan 30 '16 00:01

Stéphane Bruckert


People also ask

What does the error ProvisionedThroughputExceededException mean in DynamoDB?

ProvisionedThroughputExceededException. Message: You exceeded your maximum allowed provisioned throughput for a table or for one or more global secondary indexes. To view performance metrics for provisioned throughput vs. consumed throughput, open the Amazon CloudWatch console . Example: Your request rate is too high.

What is DynamoDBMapper?

The DynamoDBMapper class is the entry point to Amazon DynamoDB. It provides access to a DynamoDB endpoint and enables you to access your data in various tables. It also enables you to perform various create, read, update, and delete (CRUD) operations on items, and run queries and scans against tables.

What is DynamoDB item?

In Amazon DynamoDB, an item is a collection of attributes. Each attribute has a name and a value. An attribute value can be a scalar, a set, or a document type. For more information, see Amazon DynamoDB: How it works. DynamoDB provides four operations for basic create, read, update, and delete (CRUD) functionality.


1 Answers

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.

like image 88
Arthur Ulfeldt Avatar answered Sep 22 '22 04:09

Arthur Ulfeldt