1
0

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からテーブル一覧CSVを作る

Posted at

既存テーブル一覧の情報を表形式に出力したかったため、そのネタを作るためのワンライナーです。

この手順では以下のコマンドを利用します。
必要に応じてインストールしてください。

  • 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'

既存のテーブル情報からCSV形式で一覧を作成する

上で取得したJSONファイルを元に以下の加工を行います。
あとは出来上がったCSVをエクセルなりなんなりにコピペで出来上がりです。

  • 列定義は以下
    • テーブル物理名
    • AttributeDefinitions
      • データ型 | 名称
      • 1属性毎に改行表示
    • HASH
    • RANNGE
    • BillingMode
      • BillingModeSummaryキーが存在しないことを考慮し、RCU/WCUが0の場合はPAY_PER_REQUEST、以外はPROVISIONEDに設定
    • RCU
    • WCU
    • GSI
      • [インデックス名]\nHASH | RANGE
    • LSI
      • [インデックス名]\nHASH | RANGE
    • StreamViewType
find ./ -name \*.json -maxdepth 1 -type f \
| xargs -L 1 -I @ cat @ \
| jq -r '
[.TableName]
+ [[.AttributeDefinitions[]? | [.AttributeType, .AttributeName] | join(" | ") ] | join("\n")]
+ [.KeySchema[0] | .AttributeName]
+ [.KeySchema[1] | .AttributeName]
+ [if has("BillingMode") then .BillingMode else (. | if has("ProvisionedThroughput") and .ProvisionedThroughput.ReadCapacityUnits == 0 and .ProvisionedThroughput.WriteCapacityUnits == 0 then "PAY_PER_REQUEST" else "PROVISIONED" end) end]
+ [.ProvisionedThroughput? | .ReadCapacityUnits, .WriteCapacityUnits]
+ [[.GlobalSecondaryIndexes[]? | ("[" + .IndexName + "]\n" + ([.KeySchema[]?.AttributeName] | join(" | ")) )] | join("\n")]
+ [[.LocalSecondaryIndexes[]?  | ("[" + .IndexName + "]\n" + ([.KeySchema[]?.AttributeName] | join(" | ")) )] | join("\n")]
+ [if has("StreamSpecification") and .StreamSpecification.StreamEnabled == true then .StreamSpecification.StreamViewType else "" end]
| @csv'
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?