Skip to content

Instantly share code, notes, and snippets.

@totomz
Last active September 11, 2023 08:37
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save totomz/e1750511a83b29a65e74220e0dc29ff4 to your computer and use it in GitHub Desktop.
Save totomz/e1750511a83b29a65e74220e0dc29ff4 to your computer and use it in GitHub Desktop.
A simple script to push a JSON log to AWS CLoudWatch
#!/bin/bash
AWS_REGION="--region XXX_REGION"
AWS_PROFILE="--profile XXX_PROFILE"
LOG_GROUP="XXX_YOUR_LOG_GROUP"
LOG_STREAM="$(whoami)-$( date "+%Y%m%d-%H-%M-%S")"
aws logs create-log-stream --log-group-name $LOG_GROUP --log-stream-name $LOG_STREAM $AWS_REGION $AWS_PROFILE
# Create a new Logstream and ush all the logs
SEQUENCE=""
while IFS='' read -r line || [[ -n "$line" ]]; do
TIMESTAMP=$(echo $line| jq .timestamp)
JSONOBJ=$(echo $line| jq tostring)
echo "SENDING $TIMESTAMP $line"
SEQ=$(aws logs put-log-events \
--log-group-name $LOG_GROUP \
--log-stream-name $LOG_STREAM \
--log-events "{\"message\":$JSONOBJ,\"timestamp\":$TIMESTAMP}" \
$SEQUENCE \
$AWS_REGION \
$AWS_PROFILE | jq .nextSequenceToken --raw-output)
echo $SEQ
SEQUENCE=" --sequence-token $SEQ"
done < "$1"
@totomz
Copy link
Author

totomz commented Dec 19, 2017

Send logs from a file to AWS CloudWatch.

Usage

./send-to-cloudwatch.sh your-file

your-file is expected to have a single json object in each row, with a field timestamp that containing the epoch in ms (eg: new Date().getTime())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment