AWS Certified Developer – Associate
CloudWatch Logs Insights query syntax for DVA-C03
The CloudWatch Logs User Guide describes Logs Insights QL as commands joined by the pipe character: fields, filter, stats, sort, limit, dedup, parse and diff. CloudWatch generates system fields such as @timestamp, @message and @log for Standard class log groups. Queries are priced per GB of data scanned, so narrow the log groups and the time range.
Which fields can a query use before it parses anything?
For every log event in a Standard class log group, Logs Insights generates system fields that start with @. @message is the raw log event, @timestamp is the event's timestamp, @ingestionTime is when CloudWatch Logs received it, @logStream is the log stream name, and @log identifies the log group as account-id:log-group-name. When one query covers several log groups, @log shows which group each row came from.
Some log types get more fields. Lambda logs add @requestId, @duration, @billedDuration, @maxMemoryUsed and @memorySize. JSON logs are discovered too, and nested fields are written with dot notation, for example userIdentity.type. Field discovery works only in the Standard log class.
Discovery has limits. In a Lambda log event only the first embedded JSON fragment is discovered. At most 200 fields are extracted from a JSON log. A field whose value is a JSON-encoded string is treated as one value, so dot notation does not reach inside it. For anything not discovered, use parse.
What do the main commands do?
fields chooses the fields to show and can create new ones. filter keeps the events that match a condition. sort orders rows asc or desc. limit sets how many events come back: the default is up to 10,000 and the highest value is 100,000. fields @timestamp, @message | sort @timestamp desc | limit 25 returns the 25 most recent events.
parse creates fields from text and reads @message unless you name another field. Glob mode uses * wildcards with as aliases. Regex mode uses named capture groups such as (?<user>...). An event that does not match still appears in the results, without the extracted fields.
stats calculates aggregates, and bin() groups them by time. stats pct(@duration, 95) by bin(1h) gives, for each hour, the @duration value that 95 percent of values are lower than.
How do dedup and diff behave?
dedup returns one event for each unique value of the field you name. It keeps the first result in the sort order. If you do not sort first, it uses descending @timestamp, so the most recent event is kept. To keep the earliest event per server, run sort @timestamp asc | dedup server. The only command allowed after dedup is limit.
diff compares the events in the selected time range with a previous period of equal length, so new kinds of events stand out. Plain diff uses the range immediately before. diff previousDay, diff previousWeek and diff previousMonth use the same time one day, week or month earlier. Log groups in the Essentials log class support every command except pattern, diff and unmask.
What decides the cost of a query?
The CloudWatch pricing page lists Logs Insights queries per GB of data scanned. AWS's guidance is to select only the log groups a query needs and the narrowest possible time range. In the console, cancel queries before closing the page, because they otherwise run to completion. A Logs Insights widget on a dashboard starts a new query at every refresh.
A regular limit does not reduce scanning: the query scans all data in the time range and then returns the top rows. limit any N stops as soon as N matching events are found, but the rows come back in no particular order. estimate returns the bytes a query would scan without running it, and field indexes let a query skip events that are known not to contain the indexed field.
Where the exam catches people
- Believing
limit 100makes a query scan less data: a regularlimitscans the whole time range first. Narrow the log groups and time range, or uselimit anywhen order does not matter. - Believing
dedupkeeps the oldest event by default: without a sort it uses descending@timestampand keeps the newest. Sort ascending first to keep the earliest. - Believing you can sort after
dedup:limitis the only command allowed after it, so the sort goes before. - Believing
diff previousDaycompares with the hour just before: plaindiffuses the immediately preceding range.previousDayuses the same time on the day before. - Believing every JSON field in a Lambda log event is discovered: only the first embedded JSON fragment is. Extract the rest with
parse.