Last active
September 11, 2023 08:37
-
-
Save totomz/e1750511a83b29a65e74220e0dc29ff4 to your computer and use it in GitHub Desktop.
A simple script to push a JSON log to AWS CLoudWatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 fieldtimestamp
that containing the epoch in ms (eg:new Date().getTime()
)