Home > Software design >  AWS CLI put cloudwatch logs with a JSON in message
AWS CLI put cloudwatch logs with a JSON in message

Time:06-10

I am trying to put logs to AWS CloudWatch logs via AWS CLI using a bash script:

#!/bin/bash
EVENT_TIME=$(date  %s%3N)
LOG_LEVEL=6
EVENT_SOURCE=myapp
MESSAGE=1

OUTPUT=$(jq -n \
  --arg EventTime "$EVENT_TIME" \
  --arg LogLevel "$LOG_LEVEL" \
  --arg EventSource "$EVENT_SOURCE" \
  --arg Message "$MESSAGE" \
  '{EventTime:$EventTime,LogLevel:$LogLevel,EventSource:$EventSource,Message:$Message}')

MESSAGE="$OUTPUT"

aws logs put-log-events --log-group-name test --log-stream-name local --log-events timestamp=$(date  %s%3N),message=$MESSAGE

but I am getting error:

Error parsing parameter '--log-events': Expected: '<double quoted>', received: '<none>' 
for input:
 timestamp=1654692489664,message="{

The command works fine, if I remove the JSON message to a simple string. It should be an issue with quoting but not sure where the problem is. Any idea?

CodePudding user response:

The message parameter needs to be a string containing the json, not the direct json created with jq.

Something like this should work:

#!/bin/bash
EVENT_TIME=$(date  %s000)
LOG_LEVEL=6
EVENT_SOURCE=myapp
MESSAGE=1

OUTPUT=$(jq -n \
  --arg EventTime "$EVENT_TIME" \
  --arg LogLevel "$LOG_LEVEL" \
  --arg EventSource "$EVENT_SOURCE" \
  --arg Message "$MESSAGE" \
      '{EventTime:$EventTime,LogLevel:$LogLevel,EventSource:$EventSource,Message:$Message}')


LOG_MESSAGE=$(echo $OUTPUT | sed 's/"/\\"/g') 
   
aws logs put-log-events --log-group-name test --log-stream-name local --log-events timestamp=$(date  %s000),message=\""$LOG_MESSAGE"\"

Also, if you plan to use the put-log-events like this, you will need to provide the --sequence-token for consecutive puts. See here: https://docs.aws.amazon.com/cli/latest/reference/logs/put-log-events.html

Might be best to setup CloudWatch agent to publish the logs.

  • Related