Skip to content

Instantly share code, notes, and snippets.

@ottokruse
Created March 27, 2024 12:29
Show Gist options
  • Save ottokruse/8846c5279d6e17735aeee884203b0213 to your computer and use it in GitHub Desktop.
Save ottokruse/8846c5279d6e17735aeee884203b0213 to your computer and use it in GitHub Desktop.
Download full AWS CloudWatch log stream
#!/bin/bash -e
log_group_name="/aws/sagemaker/TrainingJobs"
log_stream_name=${1}
output_file=$(echo "${1}.txt" | sed 's/\//_/g')
next_token=""
echo "Storing full logs to: $output_file"
# Clear output file
>"$output_file"
# Loop to handle pagination
page=1
last_token=""
while true; do
if [ -z "$next_token" ]; then
response=$(aws logs get-log-events --log-group-name "$log_group_name" --log-stream-name "$log_stream_name" --start-from-head)
else
response=$(aws logs get-log-events --log-group-name "$log_group_name" --log-stream-name "$log_stream_name" --next-token "$next_token")
fi
echo "$response" | jq -r '.events[] | @text "\(.timestamp) \(.message)"' >>"$output_file"
next_token=$(echo "$response" | jq -r '.nextForwardToken')
if [ "$next_token" == "null" ] || [ "$next_token" == "$last_token" ]; then
break
fi
((page++))
echo "Fetching page ${page} (next token: $next_token)"
last_token="$next_token"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment