Sub topic 11
Querying logs with Logs Insights
This chapter assumes you have log groups receiving data, for example from the CloudWatch agent, and permission to run logs:StartQuery and logs:GetQueryResults.
What Logs Insights does
Logs Insights is a query service built into CloudWatch Logs. You choose one or more log groups and a time range, then run a query. CloudWatch scans the matching log events and returns rows. Nothing is indexed ahead of time, so every query reads the data in the range you select. You are billed per GB scanned, so a narrow time range and fewer log groups cost less.
Query structure
A query is a series of commands joined by the pipe character (|). Each command takes the output of the previous one.
fieldschooses which fields to show.filterkeeps only events that match a condition.statsaggregates, using functions such ascount(),avg(), andmax().sortandlimitorder the rows and cap how many are returned.parseextracts a value from message text into a new field.
Every event has built-in fields: @timestamp, @message, @logStream, and @log. If your logs are JSON, Logs Insights discovers the JSON keys as fields automatically.
A worked example
This query counts error messages in 5-minute bins and lists the 20 busiest bins.
fields @timestamp, @message
| filter @message like /ERROR/
| stats count() as errors by bin(5m)
| sort errors desc
| limit 20The filter runs before stats, so only error events are counted. bin(5m) groups events by 5-minute window. Use bin(1h) for a longer trend.
Running a query from the CLI
The CLI runs queries asynchronously. You start the query, then fetch the results with the returned query ID. Times are Unix epoch seconds.
QUERY_ID=$(aws logs start-query \
--log-group-name /app/web \
--start-time $(date -d '1 hour ago' +%s) \
--end-time $(date +%s) \
--query-string 'fields @timestamp, @message | filter @message like /ERROR/ | stats count() as errors by bin(5m) | sort errors desc | limit 20' \
--query 'queryId' --output text)
aws logs get-query-results --query-id "$QUERY_ID"The result has a status field. Wait until it is Complete before reading the rows. While it is Running, the rows may be partial.
Insights or metric filters?
| Need | Use |
|---|---|
| Investigate an incident after the fact | Logs Insights |
| Ad hoc counts and breakdowns | Logs Insights |
| Alarm on a log pattern continuously | Metric filter, then an alarm |
| Graph a log-derived number over time | Either; a dashboard widget can run a saved query |
A query only runs when you start it, so it cannot trigger an alarm by itself. For alerting, turn the pattern into a metric with a metric filter.
Common errors
- Empty results. The time range does not cover the events, or the log group name is wrong. Check the range first.
- Slow or costly queries. Add
filterearly and shrink the time range. - A field is missing. Logs Insights discovers fields only from the sampled events. Use
parseto extract values from plain text. - Truncated rows. When you do not set
limit, a query returns at most 1,000 rows by default. Add an explicitlimitto change that.
Exercise
Write a Logs Insights query that finds the 10 most recent events containing the word "timeout" and shows only the timestamp and message.
Show answer
fields @timestamp, @message | filter @message like /timeout/ | sort @timestamp desc | limit 10