Skip to content

Instantly share code, notes, and snippets.

@datahaikuninja
Last active October 15, 2023 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datahaikuninja/eef0d50b9551ee0ce60de06b62e41772 to your computer and use it in GitHub Desktop.
Save datahaikuninja/eef0d50b9551ee0ce60de06b62e41772 to your computer and use it in GitHub Desktop.
My aws cli logs command cheat sheet
# CloudWatchLogsのAWS CLIチートシート
#!/bin/bash
# log_group_list.csvにロググループ名と保持期間を書く
set -euxo pipefail
while IFS=, read -r log_group_name retension_in_days
do
aws logs put-retention-policy \
--log-group-name "${log_group_name}" \
--retention-in-days "${retension_in_days}" \
--region ap-northeast-1
done < log_group_list.csv
# CloudWatchのコスト削減のために、データ量が多い順にロググループをソートするコマンド
# スプレッドシートに貼り付ける想定でcsvで出力している
# --query の配列の後ろの辞書はなくても構わない
# reverse(sort_by())によって降順でソートした配列をjqに渡しても結果のcsvは変わらない
aws logs describe-log-groups \
--no-paginate \
--query 'reverse(sort_by(logGroups, &storedBytes))[].{"logGroupName": logGroupName, "storedBytes": storedBytes, "retentionInDays": retentionInDays}' \
--output json | jq -r ".[] | [.logGroupName, .storedBytes, .retentionInDays] | @csv" > log-groups.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment