Skip to main content
Welcome — this lesson covers AWS CloudWatch Logs Insights, the built-in, interactive query engine used to analyze CloudWatch Logs. We’ll walk through the lifecycle from log collection to log intelligence: collecting logs (CloudWatch Agent), storing them in log groups and log streams, routing via subscription filters, and analyzing with Logs Insights.
A presentation slide titled "From Log Collection to Log Intelligence" showing a person at a laptop alongside a diagram that outlines collecting, storing, and routing logs to services like CloudWatch Agent, Log groups, Kinesis, Lambda, and OpenSearch.
Why CloudWatch Logs Insights?
  • Fast, interactive, SQL-like queries over log data already in CloudWatch Logs.
  • No additional infrastructure required — suitable for troubleshooting, pattern discovery, and ad-hoc performance analysis.
  • Optimized for recent, time-series log analysis (for archived S3 logs use Athena; for large-batch processing consider EMR).
A presentation slide for CloudWatch Log Insights with the service icon and name. Below it are five feature tiles: Launched in 2018, Purpose-Built Query Service, SQL-Like Query Language, Interactive Analysis, and Pay-per-Scan Pricing.
Use cases and when not to use it Query retention and limits (quick reference)
  • Query results retained: 7 days.
  • Saved queries per account: up to 1,000.
  • Price model: pay-per-scan (billed on volume scanned during queries).
  • Query timeout: 15 minutes.
  • Result rows: default 1,000, maximum 10,000.
Query language fundamentals CloudWatch Logs Insights uses a sequential, SQL-like query language. Queries execute in a pipeline where each command transforms or filters the event stream. Commands are case-sensitive by default. Common pipeline stages:
  1. Select fields — fields
  2. Restrict events — filter (do this early to reduce scanned bytes)
  3. Extract fields — parse (for unstructured logs)
  4. Aggregate & transform — stats, sort, limit
A slide titled "Query Language Fundamentals" showing a blue funnel graphic divided into three stages labeled 01, 02, and 03. The stages are captioned "Six Primary Commands," "Sequential Execution," and "Result Set Modification," each with a small icon.
Key commands and examples Automatic fields always available: @timestamp, @message, @logStream, @log. JSON logs are auto-discovered and do not require parse for already-known fields. Common query examples
  • Top error messages:
  • Latency percentile analysis by endpoint:
  • Request volume by service, hourly:
Parsing an unstructured message:
Pattern matching and regular expressions Supported match styles:
  • = — exact equality
  • like — wildcard pattern matching (case-sensitive by default; append /i for case-insensitive)
  • in — membership in a list
  • Full regular expressions — use for complex extraction and matches
Example: find all 4xx and 5xx responses
A slide titled "Pattern Matching and Regular Expressions" that lists three matching operators: "like" (case-sensitive by default unless /i flag is used), "=" (exact string matching), and "IN" (matches against a list of values).
Automatic field discovery and JSON support
  • Logs that are structured JSON (including nested JSON / JSON Lines) are automatically discovered at query time.
  • First queries against a new structure may be slightly slower while discovery occurs. Example JSON log:
A presentation slide titled "Automatic Field Discovery" showing two feature cards: "Query-Time Discovery" (field discovery occurs only during log ingestion) and "Schema-Free Operation" (no schema definition, data modeling, or ETL pipelines required), each with a colored icon. The slide is copyrighted by KodeKloud.
Query performance and cost optimization (best practices) Because Logs Insights billing is based on scanned bytes, query performance and cost are linked. Apply these best practices:
  • Filter early: put filter at the top of your pipeline before parse or stats.
  • Target specific log groups: avoid querying all logs unless required.
  • Narrow time ranges: start small (e.g., last 1 hour or 7 days) and expand only as needed.
  • Sample during development: test query logic on a small time window before wide runs.
  • Save reusable queries: reduce ad-hoc rework and accidental scans.
If an exam asks how to reduce Logs Insights costs dramatically: shorten time ranges, narrow log groups, use sampling, and optimize filters.
An infographic titled "Query Performance and Optimization" showing four colored, numbered arrows with tips: Filter Early in Pipeline, Specific Log Groups, Limit Time Range, and Use Sampled Queries. It notes that query performance directly impacts troubleshooting speed and cost.
Integration, visualization, and export
  • Embed Logs Insights queries in CloudWatch dashboards for charts and widgets.
  • Export query results to CSV for offline analysis.
  • Visual charts available: bar, line, stacked area, and other CloudWatch chart types.
  • Exported results can feed external tools: Amazon QuickSight, Grafana (including AWS Managed Grafana), or OpenSearch for advanced dashboards.
A presentation slide titled "Integration and Visualization" showing three colorful tiles labeled Bar Charts, Line Charts, and Stacked Area Charts, each with a simple icon and brief description.
Metrics, alarms, and scheduled queries
  • Create CloudWatch metric filters from log data and publish metrics to drive alarms.
  • Use metric filters + alarms for continuous monitoring. Logs Insights is intended for interactive analysis, not as a primary alerting mechanism.
  • There is no native scheduler in the Logs Insights console. For automation, call the Logs Insights APIs and orchestrate with EventBridge or Lambda (example below).
A slide titled "Limitations and When NOT to Use" with four colored panels listing constraints: 15-minute query timeout, 10,000-row result limit, no scheduled/programmatic queries, and costly historical analysis. Each panel includes a simple icon and a brief explanatory note beneath it.
CloudWatch Logs Insights can be invoked programmatically using the CloudWatch Logs APIs (for example, StartQuery, GetQueryResults, and StopQuery). For scheduled or automated queries, combine StartQuery with EventBridge (or Lambda) to run queries on a schedule and capture results.
When to choose other services
  • Amazon Athena: long-term, large-scale SQL queries over logs archived in S3 (good for scheduled/recurring historic analysis).
  • OpenSearch / Elasticsearch: full-text search, complex dashboards, external team access without the AWS Console.
  • EMR or other big-data tools: large-scale batch processing or custom analytics pipelines.
A slide titled "Better Alternatives" showing two option buttons — "Use Amazon Athena" and "Use Elasticsearch/OpenSearch" — with a large question mark. To the right is a "When?" list of use cases (building complex search interfaces/dashboards, full-text search across unstructured content, external team access without the AWS console, and needing advanced visualization).
Summary CloudWatch Logs Insights is the exam-relevant, interactive query engine for CloudWatch Logs. Use it for recent troubleshooting, pattern discovery, percentile-based performance analysis, and quick visualizations when data already resides in CloudWatch Logs. For scheduled queries, long-term historical analysis, or advanced full-text search, choose Athena, OpenSearch, or big-data processing tools as appropriate. Links and references

Watch Video