Skip to content

Instantly share code, notes, and snippets.

@meyju
Created May 5, 2015 09:11
Show Gist options
  • Save meyju/4c05b0564bd5cb9721d4 to your computer and use it in GitHub Desktop.
Save meyju/4c05b0564bd5cb9721d4 to your computer and use it in GitHub Desktop.
Script downloads all logs from aws cloudwatch logs
#!/usr/bin/env bash
#
# Copyright 2015 Julian Meyer
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Info: Script downloads all logs from aws cloudwatch logs
# Version: 0.1
#
# USE AT YOUR OWN RISK!
# Settings START
LOGGROUPNAME="/aws/lambda/<functionname>"
AWS_REGION="eu-west-1"
STREAM_NAME_PREFIX="2015/05/05/"
LOGDIR="."
# Settings START
A_STEAM_NAME=()
get_steam_name (){
if [ -n "$nextToken" ]; then
WITH_nextToken="--next-token $nextToken"
unset nextToken
fi
AWSOUTPUT=$( aws logs describe-log-streams --log-group-name $LOGGROUPNAME --region $AWS_REGION --limit 50 --log-stream-name-prefix $STREAM_NAME_PREFIX $WITH_nextToken | egrep 'nextToken|logStreamName' | awk '{print $1 $2}' | sed 's/[",]//g')
for NAME in ${AWSOUTPUT//\\n/ };
do
if [[ $NAME == *"nextToken"* ]]
then
nextToken=`echo $NAME | cut -d ":" -f2-`
continue
fi
NAME=`echo $NAME | cut -d ":" -f2-`
A_STEAM_NAME+=($NAME);
done
}
# Getting all Steam-Names's
get_steam_name
while [ -n "$nextToken" ]
do
get_steam_name
done
for STREAMNAME in "${A_STEAM_NAME[@]}"
do
FILENAME=`echo "$STREAMNAME.log" | sed 's/\//_/g'`
aws logs get-log-events --region $AWS_REGION --log-group-name $LOGGROUPNAME --log-stream-name $STREAMNAME > $LOGDIR/$FILENAME;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment