Upgrade to Pro — share decks privately, control downloads, hide ads and more …

jq を駆使して aws cli の運用を最適化

Sho
October 29, 2024

jq を駆使して aws cli の運用を最適化

#midosuji_tech

Sho

October 29, 2024
Tweet

More Decks by Sho

Other Decks in Programming

Transcript

  1. jqとは • jq is a lightweight and flexible command-line JSON

    processor. ◦ 翻訳: jqは軽量で柔軟なコマンドラインJSONプロセッサーです。 ◦ 引用: https://jqlang.github.io/jq/ • A jq program is a "filter": it takes an input, and produces an output. ◦ 翻訳: jqプログラムは「フィルター」であり、入力を受け取り、出力を生成する。 ◦ 引用: https://jqlang.github.io/jq/manual/
  2. jqの使い方 { "Items": [ { "order_id": {"S": "1001"}, "customer": {"M":

    {"name": {"S": "Alice"}}}, "status": {"S": "shipped"} }, { "order_id": {"S": "1002"}, "customer": {"M": {"name": {"S": "Bob"}}}, "status": {"S": "processing"} }, { "order_id": {"S": "1003"}, "customer": {"M": {"name": {"S": "Charlie"}}}, "status": {"S": "delivered"} } ] } aws dynamodb scan --table-name orders_table ¥ > items.json aws cliによるリソースの操作や情報の取得は 料金が発生することが多いので、実行結果は 保存することをお勧めします
  3. jqの使い方 { "Items": [ { "order_id": {"S": "1001"}, "customer": {"M":

    {"name": {"S": "Alice"}}}, "status": {"S": "shipped"} }, { "order_id": {"S": "1002"}, "customer": {"M": {"name": {"S": "Bob"}}}, "status": {"S": "processing"} }, { "order_id": {"S": "1003"}, "customer": {"M": {"name": {"S": "Charlie"}}}, "status": {"S": "delivered"} } ] } 特定の要素を抽出 cat items.json | jq '.Items[].order_id.S' "1001" "1002" "1003" 実行結果
  4. jqの使い方 { "Items": [ { "order_id": {"S": "1001"}, "customer": {"M":

    {"name": {"S": "Alice"}}}, "status": {"S": "shipped"} }, { "order_id": {"S": "1002"}, "customer": {"M": {"name": {"S": "Bob"}}}, "status": {"S": "processing"} }, { "order_id": {"S": "1003"}, "customer": {"M": {"name": {"S": "Charlie"}}}, "status": {"S": "delivered"} } ] } 条件に基づくフィルタリング cat items.json | jq '.Items[] | select(.status.S == "shipped")' { "order_id": { "S": "1001" }, "customer": { "M": { "name": { "S": "Alice" } } }, "status": { "S": "shipped" } } 実行結果
  5. jqの使い方 { "Items": [ { "order_id": {"S": "1001"}, "customer": {"M":

    {"name": {"S": "Alice"}}}, "status": {"S": "shipped"} }, { "order_id": {"S": "1002"}, "customer": {"M": {"name": {"S": "Bob"}}}, "status": {"S": "processing"} }, { "order_id": {"S": "1003"}, "customer": {"M": {"name": {"S": "Charlie"}}}, "status": {"S": "delivered"} } ] } csvの作成 cat items.json | jq -r '.Items[] | [.order_id.S, .customer.M.name.S, .status.S] | @csv' "1001","Alice","shipped" "1002","Bob","processing" "1003","Charlie","delivered" 実行結果
  6. sed { "Items": [ { "order_id": {"S": "1001"}, "customer": {"M":

    {"name": {"S": "Alice"}}}, "status": {"S": "shipped"} }, { "order_id": {"S": "1002"}, "customer": {"M": {"name": {"S": "Bob"}}}, "status": {"S": "processing"} }, { "order_id": {"S": "1003"}, "customer": {"M": {"name": {"S": "Charlie"}}}, "status": {"S": "delivered"} } ] } JSONデータから抽出した文字列の一部を置換する cat items.json | jq -r '.Items[].customer.M.name.S' | sed 's/Alice/Alicia/' Alicia Bob Charlie 実行結果
  7. awk { "Items": [ { "order_id": {"S": "1001"}, "customer": {"M":

    {"name": {"S": "Alice"}}}, "status": {"S": "shipped"} }, { "order_id": {"S": "1002"}, "customer": {"M": {"name": {"S": "Bob"}}}, "status": {"S": "processing"} }, { "order_id": {"S": "1003"}, "customer": {"M": {"name": {"S": "Charlie"}}}, "status": {"S": "delivered"} } ] } JSONデータから抽出した名前の長さを計算する cat items.json | jq -r '.Items[].customer.M.name.S' | awk '{ print $0, length($0) }' Alice 5 Bob 3 Charlie 7 実行結果
  8. sort { "Items": [ { "order_id": {"S": "1001"}, "customer": {"M":

    {"name": {"S": "Alice"}}}, "status": {"S": "shipped"} }, { "order_id": {"S": "1002"}, "customer": {"M": {"name": {"S": "Bob"}}}, "status": {"S": "processing"} }, { "order_id": {"S": "1003"}, "customer": {"M": {"name": {"S": "Charlie"}}}, "status": {"S": "delivered"} } ] } JSONデータから抽出した名前をアルファベット順にソートする cat items.json | jq -r '.Items[].customer.M.name.S' | sort Alice Bob Charlie 実行結果
  9. uniq { "Items": [ { "order_id": {"S": "1001"}, "customer": {"M":

    {"name": {"S": "Alice"}}}, "status": {"S": "shipped"} }, { "order_id": {"S": "1002"}, "customer": {"M": {"name": {"S": "Bob"}}}, "status": {"S": "processing"} }, { "order_id": {"S": "1003"}, "customer": {"M": {"name": {"S": "Charlie"}}}, "status": {"S": "delivered"} } ] } JSONデータから抽出した名前の重複を取り除く cat items.json | jq -r '.Items[].customer.M.name.S' | sort | uniq Alice Bob Charlie 実行結果
  10. wc { "Items": [ { "order_id": {"S": "1001"}, "customer": {"M":

    {"name": {"S": "Alice"}}}, "status": {"S": "shipped"} }, { "order_id": {"S": "1002"}, "customer": {"M": {"name": {"S": "Bob"}}}, "status": {"S": "processing"} }, { "order_id": {"S": "1003"}, "customer": {"M": {"name": {"S": "Charlie"}}}, "status": {"S": "delivered"} } ] } JSONデータから抽出した顧客の名前の数をカウントする cat items.json | jq -r '.Items[].customer.M.name.S' | sort | uniq | wc -l 3 実行結果
  11. • CloudWatch Logs の 保持期間が設定されていないグループの一覧を抽出する • まずは aws cli コマンド

    ◦ aws logs describe-log-groups 明日みんなに使ってほしいサンプル { "logGroups": [ { "logGroupName": "/aws/lambda/us-east-1.test2", "creationTime": 1598840774656, "metricFilterCount": 0, "arn": "arn:aws:logs:ap-northeast-1:xxxxxxxxxxxx:log-group:/EC2/Session/:*", "storedBytes": 58315421, "logGroupClass": "STANDARD", "logGroupArn": "arn:aws:logs:ap-northeast-1:xxxxxxxxxxxx:log-group:/EC2/Session/" }, ︙ { "logGroupName": "/aws-glue/crawlers", "creationTime": 1567491367253, "metricFilterCount": 0, "arn": "arn:aws:logs:ap-northeast-1:xxxxxxxxxxxx:log-group:/aws-glue/crawlers:*", "storedBytes": 10970538, "logGroupClass": "STANDARD", "logGroupArn": "arn:aws:logs:ap-northeast-1:xxxxxxxxxxxx:log-group:/aws- glue/crawlers" } ] }
  12. • CloudWatch Logs の 保持期間が設定されていないグループの一覧を出す方法 • aws logs describe-log-groups |

    jq -r '.logGroups | map(select(.retentionInDays == null or .retentionInDays == "")) | sort_by(.storedBytes) | .[] | [.logGroupName, .logGroupClass, .retentionInDays, .storedBytes] | @csv' 明日みんなに使ってほしいサンプル "/aws/codebuild/xxxxx", "STANDARD", , 86 "/aws/lambda/yyyyy", "STANDARD", , 106 "/aws/OpenSearchService/domains/zzzzz/search-logs", "STANDARD", , 239 ︙ "/aws/lambda/aaaaa", "STANDARD", , 510708032 "/aws/lambda/bbbbb", "STANDARD", , 521204122 "/aws/lambda/ccccc", "STANDARD", , 532498400