# aws --profile localstack dynamodb create-table \
--table-name Users \
--attribute-definitions \
AttributeName=UserId,AttributeType=S \
AttributeName=Email,AttributeType=S \
--key-schema \
AttributeName=UserId,KeyType=HASH \
AttributeName=Email,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
{
"TableDescription": {
"AttributeDefinitions": [
{
"AttributeName": "UserId",
"AttributeType": "S"
},
{
"AttributeName": "Email",
"AttributeType": "S"
}
],
"TableName": "Users",
"KeySchema": [
{
"AttributeName": "UserId",
"KeyType": "HASH"
},
{
"AttributeName": "Email",
"KeyType": "RANGE"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": 1759124331.517,
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"TableSizeBytes": 0,
"ItemCount": 0,
"TableArn": "arn:aws:dynamodb:ap-northeast-1:000000000000:table/Users",
"TableId": "efef1a7c-e959-443d-b77f-5d537926a03d",
"DeletionProtectionEnabled": false
}
}
# aws --profile localstack dynamodb list-tables
{
"TableNames": [
"Users"
]
}
# aws --profile localstack dynamodb describe-table --table-name Users
{
"Table": {
"AttributeDefinitions": [
{
"AttributeName": "UserId",
"AttributeType": "S"
},
{
"AttributeName": "Email",
"AttributeType": "S"
}
],
"TableName": "Users",
"KeySchema": [
{
"AttributeName": "UserId",
"KeyType": "HASH"
},
{
"AttributeName": "Email",
"KeyType": "RANGE"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": 1759124331.517,
"ProvisionedThroughput": {
"LastIncreaseDateTime": 0.0,
"LastDecreaseDateTime": 0.0,
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"TableSizeBytes": 0,
"ItemCount": 0,
"TableArn": "arn:aws:dynamodb:ap-northeast-1:000000000000:table/Users",
"TableId": "efef1a7c-e959-443d-b77f-5d537926a03d",
"DeletionProtectionEnabled": false
}
}
# aws --profile localstack dynamodb put-item \
--table-name Users \
--item '{"UserId": {"S": "u1"}, "Name": {"S": "User1"}, "Email": {"S": "u1@example.com"}}'
# aws --profile localstack dynamodb get-item \
--table-name Users \
--key '{"UserId": {"S": "u1"}, "Email": {"S": "u1@example.com"}}'
{
"Item": {
"UserId": {
"S": "u1"
},
"Name": {
"S": "User1"
},
"Email": {
"S": "u1@example.com"
}
}
}
# aws dynamodb --profile localstack scan --table-name Users
{
"Items": [
{
"UserId": {
"S": "u1"
},
"Name": {
"S": "User1"
},
"Email": {
"S": "u1@example.com"
}
}
],
"Count": 1,
"ScannedCount": 1,
"ConsumedCapacity": null
}
# aws --profile localstack dynamodb put-item \
--table-name Users \
--item '{"UserId": {"S": "u2"}, "Name": {"S": "User2"}, "Email": {"S": "u2@example.com"}}'
# aws dynamodb --profile localstack scan --table-name Users
{
"Items": [
{
"UserId": {
"S": "u1"
},
"Name": {
"S": "User1"
},
"Email": {
"S": "u1@example.com"
}
},
{
"UserId": {
"S": "u2"
},
"Name": {
"S": "User2"
},
"Email": {
"S": "u2@example.com"
}
}
],
"Count": 2,
"ScannedCount": 2,
"ConsumedCapacity": null
}
トランザクション
# aws dynamodb --profile localstack transact-write-items \
--transact-items '[
{
"Put": {
"TableName": "Users",
"Item": {"UserId": {"S": "u3"}, "Email": {"S":"u3@example.com"}, "Name": {"S": "Alice"}}
}
},
{
"Update": {
"TableName": "Users",
"Key": {"UserId": {"S": "u2"}, "Email":{"S":"u2@example.com"}},
"UpdateExpression": "SET #N = :n",
"ExpressionAttributeNames": {"#N": "Name"},
"ExpressionAttributeValues": {":n":{"S":"User2 Updated"}}
}
}
]'
# aws dynamodb --profile localstack scan --table-name Users
{
"Items": [
{
"UserId": {
"S": "u1"
},
"Name": {
"S": "User1"
},
"Email": {
"S": "u1@example.com"
}
},
{
"UserId": {
"S": "u3"
},
"Email": {
"S": "u3@example.com"
},
"Name": {
"S": "Alice"
}
},
{
"UserId": {
"S": "u2"
},
"Email": {
"S": "u2@example.com"
},
"Name": {
"S": "User2 Updated"
}
}
],
"Count": 3,
"ScannedCount": 3,
"ConsumedCapacity": null
}
バッチ
# aws dynamodb --profile localstack batch-write-item \
--request-items '{
"Users": [
{"PutRequest": {"Item": {"UserId": {"S":"u4"}, "Email":{"S":"u4@example.com"}, "Name":{"S":"Bob"}}}},
{"PutRequest": {"Item": {"UserId": {"S":"u5"}, "Email":{"S":"u5@example.com"}, "Name":{"S":"Carol"}}}}
]
}'
{
"UnprocessedItems": {}
}
# aws dynamodb --profile localstack scan --table-name Users
{
"Items": [
{
"UserId": {
"S": "u1"
},
"Name": {
"S": "User1"
},
"Email": {
"S": "u1@example.com"
}
},
{
"UserId": {
"S": "u4"
},
"Email": {
"S": "u4@example.com"
},
"Name": {
"S": "Bob"
}
},
{
"UserId": {
"S": "u5"
},
"Email": {
"S": "u5@example.com"
},
"Name": {
"S": "Carol"
}
},
{
"UserId": {
"S": "u3"
},
"Email": {
"S": "u3@example.com"
},
"Name": {
"S": "Alice"
}
},
{
"UserId": {
"S": "u2"
},
"Email": {
"S": "u2@example.com"
},
"Name": {
"S": "User2 Updated"
}
}
],
"Count": 5,
"ScannedCount": 5,
"ConsumedCapacity": null
}
ページング
データ準備
# u1_1@example.com ~ u1_10@example.com の Email
for i in $(seq 1 100); do
aws dynamodb --profile localstack \
put-item \
--table-name Users \
--item "{\"UserId\": {\"S\": \"u1\"}, \"Email\": {\"S\": \"u1_$i@example.com\"}, \"Name\": {\"S\": \"User$i\"}}"
done
# aws dynamodb --profile localstack scan --table-name Users
{
"Items": [
{
"UserId": {
"S": "u1"
},
"Name": {
"S": "User1"
},
"Email": {
"S": "u1@example.com"
}
},
{
"UserId": {
"S": "u1"
},
"Email": {
"S": "u1_10@example.com"
},
"Name": {
"S": "User10"
}
},
..省略
{
"UserId": {
"S": "u2"
},
"Email": {
"S": "u2@example.com"
},
"Name": {
"S": "User2 Updated"
}
}
],
"Count": 100,
"ScannedCount": 100,
"ConsumedCapacity": null
}
Query ページング例
1回目
# aws dynamodb \
--profile localstack \
query \
--table-name Users \
--key-condition-expression "UserId = :uid" \
--expression-attribute-values '{":uid":{"S":"u1"}}' \
--limit 3
出力
- 続きがあるので、
LastEvaluatedKeyが含まれる
{
"Items": [
{
"UserId": {
"S": "u1"
},
"Name": {
"S": "User1"
},
"Email": {
"S": "u1@example.com"
}
},
{
"UserId": {
"S": "u1"
},
"Email": {
"S": "u1_100@example.com"
},
"Name": {
"S": "User100"
}
},
{
"UserId": {
"S": "u1"
},
"Email": {
"S": "u1_10@example.com"
},
"Name": {
"S": "User10"
}
}
],
"Count": 3,
"ScannedCount": 3,
"LastEvaluatedKey": {
"UserId": {
"S": "u1"
},
"Email": {
"S": "u1_10@example.com"
}
}
}
2回目
- exclusive-start-key:1回目の出力のLastEvaluatedKeyを指定
# aws dynamodb --profile localstack \
query \
--table-name Users \
--key-condition-expression "UserId = :uid" \
--expression-attribute-values '{":uid":{"S":"u1"}}' \
--limit 99 \
--exclusive-start-key '{"UserId":{"S": "u1"}, "Email": {"S": "u1_10@example.com"}}'
出力
- 続きがないので、
LastEvaluatedKeyが含まれない
{
"Items": [
{
"UserId": {
"S": "u1"
},
"Email": {
"S": "u1_11@example.com"
},
"Name": {
"S": "User11"
}
},
{
"UserId": {
"S": "u1"
},
"Email": {
"S": "u1_12@example.com"
},
"Name": {
"S": "User12"
}
},
..省略
{
"UserId": {
"S": "u1"
},
"Email": {
"S": "u1_99@example.com"
},
"Name": {
"S": "User99"
}
},
{
"UserId": {
"S": "u1"
},
"Email": {
"S": "u1_9@example.com"
},
"Name": {
"S": "User9"
}
}
],
"Count": 98,
"ScannedCount": 98
}
アトミックカウント検証
# aws dynamodb \
--profile localstack \
put-item \
--table-name Users \
--item '{"UserId": {"S": "u_counter"}, "Email": {"S":"counter@example.com"}, "Count": {"N": "0"}}'
# aws dynamodb \
--profile localstack \
query \
--table-name Users \
--key-condition-expression "UserId = :uid" \
--expression-attribute-values '{":uid":{"S":"u_counter"}}'
{
"Items": [
{
"UserId": {
"S": "u_counter"
},
"Email": {
"S": "counter@example.com"
},
"Count": {
"N": "0"
}
}
],
"Count": 1,
"ScannedCount": 1,
"ConsumedCapacity": null
}
# aws dynamodb \
--profile localstack \
update-item \
--table-name Users \
--key '{"UserId": {"S": "u_counter"}, "Email": {"S":"counter@example.com"}}' \
--update-expression "ADD #C :inc" \
--expression-attribute-names '{"#C":"Count"}' \
--expression-attribute-values '{":inc":{"N":"1"}}' \
--return-values ALL_NEW
{
"Attributes": {
"UserId": {
"S": "u_counter"
},
"Email": {
"S": "counter@example.com"
},
"Count": {
"N": "1"
}
}
}
# aws dynamodb \
--profile localstack \
update-item \
--table-name Users \
--key '{"UserId": {"S": "u_counter"}, "Email": {"S":"counter@example.com"}}' \
--update-expression "ADD #C :dec" \
--expression-attribute-names '{"#C":"Count"}' \
--expression-attribute-values '{":dec":{"N":"-1"}}' \
--return-values ALL_NEW
{
"Attributes": {
"UserId": {
"S": "u_counter"
},
"Email": {
"S": "counter@example.com"
},
"Count": {
"N": "0"
}
}
}