0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DynamoDB describe-tableからcreate-tableに渡すJSONファイルを作る

Posted at

ローカルで環境構築するにあたり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に設定
  • 不要なキーの削除
  • BillingModePAY_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
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?