Score:0

AWS CloudWatch parse JSON case insensitive

pk flag

On the WAF section of the AWS console there is a tab for "CloudWatch Log Insights" that provides a few sample queries. One of these is "Top 100 hosts".

fields @timestamp, @message
| parse @message '{"name":"Host","value":"*"}' as host
| stats count(*) as requestCount by host
| sort requestCount desc
| limit 100

This is a somewhat misleading report title for most people I expect. I find about half my traffic uses host as opposed to Host. I spent 30 minutes trying to make this query case insensitive when parsing that header, before giving up, and deciding to ask here.

What is the easiest way to accomplish this?

Score:2
ua flag

This is how I solved it:

fields @timestamp, @message
| parse @message /\{"name":"(H|h)ost","value":"(?<host>.*?)"\}/
| stats count(*) as requestCount by host
| sort requestCount desc
| limit 100

Changing from glob patterns to regex capture groups allows to also use regexes for other matching.

kr flag
You are a diamond. Fantastic first answer. Thank you!!!!!!
pk flag
Thanks. This worked. Ended up using `(?i)`. With full filter like: `| parse @message /(?i)\{"name":"HOST","value":"(?<host>.*?|^www\..*)"\}/`
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.