ExportAwsCloudWatchLogStreams

nicolaw 29th July 2022 at 9:00am
AWS

This is a very poor ad-hoc method, but it works:

(
  set -exv
  export AWS_PROFILE=testing AWS_DEFAULT_REGION=eu-west-2
  export LOG_STREAM_NAME_PREFIX=frobnicator-
  export LOG_GROUP_NAME=/eks/my-k8s-cluster/containers
  
  while read -r logStreamName firstEventTimestamp
  do
    if [[ $(date -r $(($firstEventTimestamp/1000)) '+%Y%m%d') -lt 20220726 ]]
    then
      continue
    fi

    aws logs get-log-events \
      --log-group-name "$LOG_GROUP_NAME" \
      --log-stream-name "$logStreamName" \
      > "${LOG_STREAM_NAME_PREFIX}$(date -r $(($firstEventTimestamp/1000)) '+%Y-%m-%d_%H%M%S').log.json"

  done < <(aws logs describe-log-streams \
    --log-group-name "$LOG_GROUP_NAME" \
    --log-stream-name-prefix "$LOG_STREAM_NAME_PREFIX" \
    --query 'logStreams[*].[logStreamName,firstEventTimestamp]' \
    --output text)
)