LoginSignup
1
0

More than 1 year has passed since last update.

AWS CLIでDynamoDBのtransact-write-itemsとConditionCheckを試した時にハマったこと

Last updated at Posted at 2022-06-09

概要

AWS CLIでDynamoDBのトランザクション機能を検証していますが、ConditionCheckの使い所でハマったので、簡単にまとめます。

ハマったことを時系列に書いてみる

以下のコマンドを実行

aws dynamodb transact-write-items --transact-items '[
{
    "Put": {
        "TableName" : "tableA",
        "Item" : {
            "ID" : {"S": "001"},
            "Attribute" : {"N": "1"}
        }
    },
    "ConditionCheck": {
        "TableName" : "tableA",
        "Key" : {
            "ID" : {"S": "001"}
        },
        "ConditionExpression" : "Attribute > :limit",
        "ExpressionAttributeValues" : {
            ":limit" : {"N": "0"}
        }        
    }
}
]'

すると、以下エラーが発生

An error occurred (ValidationException) when calling the TransactWriteItems operation: TransactItems can only contain one of Check, Put, Update or Delete

以下を参考にコマンドを修正し、上記エラーはなくなる
https://stackoverflow.com/questions/57685182/using-dynamodb-transact-write-items-how-to-do-a-conditioncheck-for-an-existing-i

aws dynamodb transact-write-items --transact-items '[
{
    "Put": {
        "TableName" : "tableA",
        "Item" : {
            "ID" : {"S": "001"},
            "Attribute" : {"N": "1"}
        }
    }
},
{
    "ConditionCheck": {
        "TableName" : "tableA",
        "Key" : {
            "ID" : {"S": "001"}
        },
        "ConditionExpression" : "Attribute > :limit",
        "ExpressionAttributeValues" : {
            ":limit" : {"N": "0"}
        }        
    }
}
]'

しかし、以下のエラーが発生

An error occurred (ValidationException) when calling the TransactWriteItems operation: Transaction request cannot include multiple operations on one item

Putなどのコマンド内でConditionExpressionやExpressionAttributeValuesを入れる必要があった。
(参考に記載の公式ドキュメントを見る限り、このようになっていないが、、)

以下コマンドで解決

aws dynamodb transact-write-items --profile sandbox --transact-items '[
{
    "Put": {
        "TableName" : "tableA",
        "Item" : {
            "ID" : {"S": "001"},
            "Attribute" : {"N": "1"}
        },
        "ConditionExpression" : "Attribute > :limit",
        "ExpressionAttributeValues" : {
            ":limit" : {"N": "0"}
        }
    }
}
]'

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0