DynamoDB put-item ConditionalCheckFailedException
<p>Given the table schema defined below (<code>create-table.json</code>) I am getting the following error after I call <code>put-item</code> using <code>add-event1.json</code> followed by <code>add-event2.json</code>:</p>

<blockquote>
  <p>A client error (ConditionalCheckFailedException) occurred when calling the PutItem operation: The conditional request failed</p>
</blockquote>

<p>Why doesn't the ConditionExpression allow me to write both records? (I expect to have 2 records after the second operation)</p>

<p>I suspected that it was because of the non-key conditions used, but I don't see anything in the <a href="http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.SpecifyingConditions.html" rel="noreferrer">docs</a> that indicates a lack of support for non-key conditions.</p>

<p><strong>Create Table</strong></p>

<pre><code>$ aws dynamodb create-table --cli-input-json file://create-table.json
</code></pre>

<p><em>create-table.json</em></p>

<pre><code>{
    "TableName": "EVENTS_TEST",
    "KeySchema": [
      { "AttributeName": "aggregateId", "KeyType": "HASH" },
      { "AttributeName": "streamRevision", "KeyType": "RANGE" }
    ],
    "AttributeDefinitions": [
      { "AttributeName": "aggregateId", "AttributeType": "S" },
      { "AttributeName": "streamRevision", "AttributeType": "N" }
    ],
    "ProvisionedThroughput": {
      "ReadCapacityUnits": 10,
      "WriteCapacityUnits": 10
    }
 }
</code></pre>

<p><strong>Add First Record</strong></p>

<pre><code>$ aws dynamodb put-item --cli-input-json file://add-event1.json
</code></pre>

<p><em>add-event1.json</em></p>

<pre><code>{
  "TableName": "EVENTS_TEST",
  "Item": {
    "aggregateId": { "S": "id" },
    "id": { "S": "119" },
    "context": { "S": "*" },
    "aggregate": { "S": "*" },
    "streamRevision": { "N": "0" },
    "commitId": { "S": "1119" },
    "commitSequence": { "N": "0" },
    "commitStamp": { "N": "1470185631511" },
    "dispatched": { "BOOL": false },
    "payload": { "S": "{ \"event\": \"bla\" }" }
  },
  "ExpressionAttributeNames": { "#name": "aggregate" },
  "ConditionExpression": "attribute_not_exists(aggregateId) and attribute_not_exists(streamRevision) and #name &lt;&gt; :name and context &lt;&gt; :ctx",
  "ExpressionAttributeValues": {
    ":name": { "S": "*" },
    ":ctx": { "S": "*" }
  }
}
</code></pre>

<p><strong>Add Second Record</strong></p>

<pre><code>$ aws dynamodb put-item --cli-input-json file://add-event2.json
</code></pre>

<p><em>add-event2.json</em></p>

<pre><code>{
  "TableName": "EVENTS_TEST",
  "Item": {
    "aggregateId": { "S": "id" },
    "id": { "S": "123" },
    "context": { "S": "myCtx" },
    "aggregate": { "S": "myAgg" },
    "streamRevision": { "N": "0" },
    "commitId": { "S": "1123" },
    "commitSequence": { "N": "0" },
    "commitStamp": { "N": "1470185631551" },
    "dispatched": { "BOOL": false },
    "payload": { "S": "{ \"event\": \"bla2\" }" }
  },
  "ExpressionAttributeNames": { "#name": "aggregate" },
  "ConditionExpression": "aggregateId &lt;&gt; :id and streamRevision &lt;&gt; :rev and #name &lt;&gt; :name and context &lt;&gt; :ctx",
  "ExpressionAttributeValues": {
     ":id": { "S": "id" },
     ":rev": { "N": "0" },
     ":name": { "S": "myAgg" },
     ":ctx": { "S": "myCtx" }
  }
}
</code></pre>