Lesson 3 of 5 CloudWatch Logs and the CloudWatch Agent

Searching Logs with Logs Insights and Live Tail

This lesson explains how CloudWatch Logs Insights and Live Tail help operators search large volumes of logs quickly during an incident.

Learning outcomes

After completing this lesson, you will be able to:

  • Write Logs Insights queries.
  • Watch events as they arrive with Live Tail.
  • Find the cause of errors with a query.

Logs Insights queries

CloudWatch Logs Insights runs queries across one or many log groups (up to 50, or more with log group selection by prefix or across linked accounts) for a chosen time range. The query language pipes commands: fields selects fields, filter keeps matching events, parse extracts fields from text, stats aggregates (count, avg, percentile) with by grouping and bin() for time buckets, and sort and limit order the output. JSON logs and AWS service logs have fields discovered automatically. Charges are per GB of data scanned, so narrow time ranges and log groups reduce cost. Queries can also be written with OpenSearch PPL or SQL, and natural language query generation can draft a query from a question.

# Top 10 slowest Lambda invocations in the last hour
filter @type = "REPORT"
| fields @requestId, @duration, @maxMemoryUsed / 1000000 as memMB
| sort @duration desc
| limit 10

# 5XX responses per minute from ALB-style JSON application logs
filter status >= 500
| stats count(*) as errors by bin(1m)
Put in order

Building a Logs Insights query

Exam skills 1.1.1 · 1.2.1

Arrange the commands of a query that finds the slowest request paths.

Log in to try this activity Interactive activities are free with an account.

Live Tail and other tools

Live Tail streams log events from selected log groups to the console or the CLI (aws logs start-live-tail) as they arrive, with filter patterns and highlighting. It is the quickest way to watch a deployment or reproduce a problem. Saved Logs Insights queries can be added to dashboards. Pattern analysis groups similar log lines and highlights new or changed patterns, which is useful when you do not know what to search for. For logs already archived in S3, such as ELB access logs or CloudTrail logs, Amazon Athena runs SQL directly on the files.

Scenario questions

Choosing a log tool

Exam skills 1.1.1 · 1.2.1

Choose the log tool for each investigation.

Log in to try this activity Interactive activities are free with an account.
Worked example

Finding the cause of 500 errors

Customers report intermittent 500 errors from an API. The operator runs a Logs Insights query over the API's log group for the last three hours, grouping errors by the errorType field and by 5-minute bins. The results show that almost all errors are DatabaseConnectionTimeout and began at 14:05. A second query on the RDS PostgreSQL log group for the same period shows remaining connection slots are reserved messages. The database ran out of connections after a deployment increased the number of Lambda functions; RDS Proxy is the longer-term fix.

Check yourself

How can the cost of a Logs Insights query be reduced?

Show answer

Limit the time range and the log groups, because charges are per GB scanned.

Exam Tips

Remember for the exam

  • Logs Insights queries many log groups with fields, filter, parse, stats and sort.
  • Charges are per GB scanned; limit the time range and log groups.
  • Live Tail streams new events for real-time troubleshooting.
  • Use Athena for logs that are stored in S3.