Created
October 4, 2022 19:29
-
-
Save iBug/1e2ea4b87d82f9546aaa3d08c9196d13 to your computer and use it in GitHub Desktop.
Gather JSON logs from Lambda functions from 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/sh | |
if [ -z "$1" ]; then | |
exit 1 | |
fi | |
now=$(date +%s) | |
function_name="$1" | |
query_id=$(aws logs start-query \ | |
--log-group-name "/aws/lambda/$function_name" \ | |
--start-time "$((now - 3600))" \ | |
--end-time "$now" \ | |
--query-string 'parse @log /(?<@name>[^\/]*)$/ | |
| fields @timestamp, @message | |
| filter @message like /^(\S+\s*)?\{/ | |
| sort @timestamp desc | |
| limit 20 | |
' | jq -r .queryId) | |
f=/tmp/aws-logs.json | |
while true; do | |
aws logs get-query-results --query-id "$query_id" > "$f" || exit 1 | |
status="$(jq -r .status "$f")" | |
test "$status" = "Complete" && break | |
done | |
jq '.results | map(map(.key=.field) | from_entries | del(.["@ptr"]) | .["@message"] = (.["@message"] | fromjson))' "$f" |
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
[ | |
{ | |
"@name": "GitHubWebhook", | |
"@timestamp": "2022-10-04 18:34:37.294", | |
"@message": { | |
"ip": "140.82.115.155", | |
"user-agent": "GitHub-Hookshot/abbd694", | |
"path": "/myHook", | |
"query": null | |
} | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment