Certopolis

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 100 makes a query scan less data: a regular limit scans the whole time range first. Narrow the log groups and time range, or use limit any when order does not matter.
  • Believing dedup keeps the oldest event by default: without a sort it uses descending @timestamp and keeps the newest. Sort ascending first to keep the earliest.
  • Believing you can sort after dedup: limit is the only command allowed after it, so the sort goes before.
  • Believing diff previousDay compares with the hour just before: plain diff uses the immediately preceding range. previousDay uses 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.

Written from these sources

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. A developer is investigating an outage in Amazon CloudWatch Logs Insights. Each application log event carries a server field and a level field. For each server, the developer needs only the earliest ERROR event that the server logged during the selected time range, returned as one row per server. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Troubleshooting and Optimization building.

    Not that one: D is right. In the game your Troubleshooting and Optimization building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Sorting by @timestamp in ascending order before dedup server therefore keeps each server's earliest ERROR. Without an explicit sort, dedup falls back to descending @timestamp, which keeps the newest event instead, and nothing except limit may follow dedup.

    Written from docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax-Dedup.html

  2. A developer must report how long the slowest invocations of an AWS Lambda function take. For each hour of the past day, the report must give the 95th percentile of the function's run time, so that a few extreme outliers do not decide the value. Which solutions will meet these requirements? (Select TWO.)

    Right. In the game that’s Renown for your Troubleshooting and Optimization building.

    Not that one: B,E is right. In the game your Troubleshooting and Optimization building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Lambda also publishes a Duration metric, and that metric accepts percentile statistics, so graphing it with p95 at a 1-hour period produces the same kind of series without writing a query.

    Written from docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax-Stats.html

  3. A developer runs Amazon CloudWatch Logs Insights queries in the console many times a day to investigate errors in one application, whose logs are in Standard-class log groups. The team's Logs Insights charges are higher than expected. The developer must lower the charges without missing any of the application's matching log events. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Troubleshooting and Optimization building.

    Not that one: D is right. In the game your Troubleshooting and Optimization building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    AWS's guidance for avoiding excessive charges is to select only the log groups a query needs and to specify the narrowest time range possible. Doing that before each run lowers the data scanned while still returning every matching event inside the scope that matters.

    Written from docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html

Ask for something