ローカルで環境構築するにあたりJSONファイルで管理したかったため、そのネタを作るためのワンライナーです
この手順では以下のコマンドを利用します。
必要に応じてインストールしてください。
- aws
- jq
既存のテーブルリストから各テーブルに関する情報を取得する
適当な作業用ディレクトリを用意して実行してください。
リージョン、プロファイルは任意です。
aws dynamodb list-tables --region ap-northeast-1 --profile default \
| jq -r '.TableNames[]' \
| xargs -L 1 -I @ sh -c \
'aws dynamodb describe-table --region ap-northeast-1 --profile default --table-name @ | jq ".Table"> @.json'
既存のテーブル情報からcreate-table用のJSONファイルを作る
上で取得したJSONファイルを元に以下の加工を行います。
-
BillingMode
キーの追加-
BillingModeSummary
キーが存在しないことを考慮し、RCU/WCUが0の場合はPAY_PER_REQUEST
、以外はPROVISIONED
に設定
-
- 不要なキーの削除
-
BillingMode
がPAY_PER_REQUEST
の場合、GSI/LSIの`ProvisionedThroughputキーを削除 - 任意のタグを追加
mkdir -p ./definitions && \
for filename in $(find . -type f -name \*.json -maxdepth 1); do jq . ${filename} |
jq "if has(\"BillingModeSummary\") then . + { BillingMode: .BillingModeSummary.BillingMode} else . + { BillingMode: (if has(\"ProvisionedThroughput\") and .ProvisionedThroughput.ReadCapacityUnits == 0 and .ProvisionedThroughput.WriteCapacityUnits == 0 then \"PAY_PER_REQUEST\" else \"PROVISIONED\" end)} end" |
jq "del(
.TableStatus
, .CreationDateTime
, (.ProvisionedThroughput? | .NumberOfDecreasesToday, .LastIncreaseDateTime, .LastDecreaseDateTime)
, .TableSizeBytes
, .ItemCount
, .TableArn
, .TableId
, (.GlobalSecondaryIndexes[]? | .IndexStatus, .IndexSizeBytes, .ItemCount, .IndexArn, (.ProvisionedThroughput? | .NumberOfDecreasesToday, .LastIncreaseDateTime, .LastDecreaseDateTime))
, (.LocalSecondaryIndexes[]? | .IndexStatus, .IndexSizeBytes, .ItemCount, .IndexArn, (.ProvisionedThroughput? | .NumberOfDecreasesToday, .LastIncreaseDateTime, .LastDecreaseDateTime))
, .LatestStreamLabel
, .LatestStreamArn
, .BillingModeSummary
)" |
jq "if .BillingMode == \"PAY_PER_REQUEST\" then del(.ProvisionedThroughput, .GlobalSecondaryIndexes[]?.ProvisionedThroughput, .LocalSecondaryIndexes[]?.ProvisionedThroughput) else . end" |
jq ". + { Tags: [ { Key: \"Name\", Value: .TableName }, { Key: \"Env\", Value: \"local\" } ]}" \
> definitions/${filename} ; done