Skip to content

Instantly share code, notes, and snippets.

@nicc777
Last active March 7, 2023 13:21
Show Gist options
  • Save nicc777/8c024f4a2928d89a77ffc7e427ea8cda to your computer and use it in GitHub Desktop.
Save nicc777/8c024f4a2928d89a77ffc7e427ea8cda to your computer and use it in GitHub Desktop.
AWS CLI and CloudWatch - Retrieve a log stream to a local file

Assuming you have LOG_GROUP_NAME and STREAM_NAME as environment variables, you can run the following:

aws logs get-log-events --log-group-name "$LOG_GROUP_NAME" --log-stream-name "$STREAM_NAME" | yq -o=json | jq '.events[] | .timestamp, .message' > stream.log

The log entries with timestamp and message will now be in the file stream.log

Another way to format the output (prevent splitting lines for timestamp and message fields):

aws logs get-log-events --log-group-name "$LOG_GROUP_NAME" --log-stream-name "$STREAM_NAME" | yq -o=json | jq '.events[] | "\(.timestamp)  \(.message)"'

Alternatively, to get the last hour:

aws logs tail $LOG_GROUP_NAME --since 1h

Or follow (similar to tail -f):

aws logs tail $LOG_GROUP_NAME --since 1h --follow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment