Logs & Logs Insights

Search millions of log lines in seconds with a simple query language.

1 min read

How logs are organized

  • Log group: one per app or function, e.g. /aws/lambda/notesnap-summarize
  • Log stream: one per source within the group, e.g. each Lambda execution environment
  • Retention: how long to keep logs. The default is never expire, so set it!

Logs Insights

CloudWatch Logs Insights queries log groups with a pipe-based language. Some recipes:

Most recent errors:

Text
fields @timestamp, @message
| filter @message like /ERROR/
| sort @timestamp desc
| limit 20

Lambda duration and memory, every 5 minutes:

Text
filter @type = "REPORT"
| stats avg(@duration), max(@duration), max(@maxMemoryUsed / 1000 / 1000) as maxMemMB by bin(5m)

Top error messages:

Text
filter level = "ERROR"
| stats count(*) as errors by message
| sort errors desc

Structured logging pays off

That last query works because the logs are JSON. Fields like level and message are discovered automatically. Log JSON instead of free text:

Python
import json
print(json.dumps({"level": "ERROR", "message": "Bedrock timeout", "noteId": key, "ms": 30012}))
Tip

Lambda can emit JSON logs natively with log format: JSON and a log level setting, so you don't need to build the JSON yourself.

Key takeaways

  • Logs live in log groups and streams. Set retention, because the default is forever.
  • Logs Insights uses fields | filter | stats | sort | limit to query.
  • Structured JSON logs make querying far easier.

Check your understanding

Pick an answer to see the explanation.

  1. 1. What's the default retention for a new CloudWatch log group?