Last active
March 24, 2025 06:42
-
-
Save gholker/88639858d53ae3ccec7ddd306cd8ba22 to your computer and use it in GitHub Desktop.
Script that uses the aws cli to download cloudwatch logs to a file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
IFS=$'\n\t' | |
LOG_GROUP_NAME="" | |
LOG_STREAM_NAME="" | |
REGION="" | |
OUTPUT_FILE="$(date +"%Y%m%d").log" | |
result=$(aws logs get-log-events \ | |
--start-from-head \ | |
--log-group-name=${LOG_GROUP_NAME} \ | |
--log-stream-name=${LOG_STREAM_NAME} \ | |
--region=${REGION}) | |
echo ${result} | jq -r .events[].message >> ${OUTPUT_FILE} | |
nextToken=$(echo $result | jq -r .nextForwardToken) | |
while [ -n "$nextToken" ]; do | |
echo ${nextToken} | |
result=$(aws logs get-log-events \ | |
--start-from-head \ | |
--log-group-name=${LOG_GROUP_NAME} \ | |
--log-stream-name=${LOG_STREAM_NAME} \ | |
--region=${REGION} \ | |
--next-token="${nextToken}") | |
if [[ $(echo ${result} | jq -e '.events == []') == "true" ]]; then | |
echo "response with empty events found -> exiting." | |
exit | |
fi | |
echo ${result} | jq -r .events[].message >> ${OUTPUT_FILE} | |
nextToken=$(echo ${result} | jq -r .nextForwardToken) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you having problem like
jq: parse error: Invalid numeric literal at line 1, column 61