概要
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"}
}
}
}
]'
参考