AWS CloudWatch: Advanced Log Searching and Analysis
Searching log data by using filter and pattern syntax or Amazon CloudWatch Logs Insights
AWS CloudWatch: Advanced Log Searching and Analysis
This guide focuses on the critical skill of analyzing log data within AWS, specifically using standard filter/pattern syntax and the powerful Amazon CloudWatch Logs Insights query engine. Mastering these tools is essential for Task Statement 4.2 of the DOP-C02 exam: auditing, monitoring, and analyzing logs to detect issues.
Learning Objectives
By the end of this module, you should be able to:
- Differentiate between Standard Filter Patterns and CloudWatch Logs Insights syntax.
- Construct filter patterns for Metric Filters and Subscription Filters.
- Write complex Logs Insights queries using
filter,stats,sort, andparsecommands. - Identify the best tool for real-time monitoring versus retrospective root-cause analysis.
Key Terms & Glossary
- Log Group: A group of log streams that share the same retention, monitoring, and access control settings.
- Filter Pattern: A specific symbolic syntax used to match terms in log events (e.g.,
[w1, w2="*Error*", w3]). - CloudWatch Logs Insights: A fully managed, pay-per-query search and analysis service for log data.
- Metric Filter: A feature that searches for and matches terms or patterns in log events and turns them into numerical CloudWatch Metrics.
- Ephemeral Field: Metadata fields automatically generated by Insights, such as
@timestamp,@message, and@logStream.
The "Big Idea"
In modern DevOps, logs are no longer just files to be read; they are a data stream. While standard pattern matching allows for simple, reactive alerting (e.g., "Tell me when an 'Error' occurs"), CloudWatch Logs Insights transforms logs into an interactive database, allowing you to perform aggregations, identify trends over time, and correlate events across multiple log groups simultaneously.
Formula / Concept Box
| Feature | Standard Filter Syntax | Logs Insights Query Language |
|---|---|---|
| Usage | Metric Filters, Subscriptions | Interactive Console searching, Dashboards |
| Complexity | Simple string/numeric matching | Rich syntax (stats, math, regex) |
| Latency | Real-time (as logs arrive) | On-demand (historical analysis) |
| Multi-group? | No (Single Log Group) | Yes (Up to 50 Log Groups) |
Hierarchical Outline
- I. Standard Filter & Pattern Syntax
- Literal Term Matching: Searching for specific strings (e.g., "Error" or "404").
- Space-Delimited Log Events: Using brackets
[ ]to define fields in unstructured logs. - JSON Log Events: Using dot notation (e.g.,
$.errorCode = "AccessDenied") to filter structured logs.
- II. CloudWatch Logs Insights
- The
fieldsCommand: Selecting specific metadata or parsed values. - The
filterCommand: Supporting boolean operators (and,or,not) and regex. - The
statsCommand: Performing aggregations (e.g.,count(),sum(),avg()). - The
parseCommand: Extracting data from a glob or regex into custom fields.
- The
- III. Visualizing Results
- Adding Insights queries to CloudWatch Dashboards.
- Exporting results to CSV or Markdown.
Visual Anchors
Log Analysis Workflow
Complexity vs. Speed
Definition-Example Pairs
- Filter Pattern (Literal): Matches specific text.
- Example:
"Exception"matches any log entry containing that word.
- Example:
- Filter Pattern (JSON): Filters based on JSON keys.
- Example:
{ $.status = 500 }matches only events where the JSON key 'status' is 500.
- Example:
- Insights (Stats): Aggregates data over time.
- Example:
stats count(*) by bin(5m)counts log events in 5-minute intervals.
- Example:
Worked Examples
Example 1: Finding 5xx Errors in ALB Logs (Insights)
Goal: Identify which backend instances are throwing the most 5xx errors in the last hour.
fields @timestamp, elb_status_code, target_port
| filter elb_status_code >= 500
| stats count(*) as errorCount by target_port
| sort errorCount descExample 2: Metric Filter for Specific User Access
Goal: Create a metric that increments whenever the user "Admin" fails a login.
Pattern Syntax:
[date, time, status="FAILURE", user="Admin"]
Explanation: This assumes a space-delimited log where the 3rd field is status and the 4th is user.
Checkpoint Questions
- Which tool would you use to trigger an AWS Lambda function every time a specific keyword appears in a log?
- In Logs Insights, how do you extract a custom field from a raw log message that is not in JSON format?
- Can a standard Metric Filter search across multiple Log Groups simultaneously?
- What is the difference between
filter @message like /Error/andfilter @message = "Error"in Insights?
▶Click to see answers
- CloudWatch Logs Subscription Filter.
- Use the
parsecommand with a glob or regular expression. - No, Metric Filters are applied to a single Log Group. Logs Insights is required for multi-group searching.
likeuses a regular expression or partial match;=requires an exact string match.
Muddy Points & Cross-Refs
- Case Sensitivity: Standard Filter Patterns are case-sensitive. If you search for "error", you will miss "Error".
- Cost Considerations: Standard searching in the "Search Logs" UI is free, but CloudWatch Logs Insights charges per GB of data scanned. Always use a narrow time range to save costs.
- Retention: If logs are expired by a retention policy, Insights cannot search them. Cross-reference with S3 Glacier for long-term log archival/compliance searching using Amazon Athena.
Comparison Tables
| Capability | Filter Patterns | Logs Insights |
|---|---|---|
| Regex Support | No (Partial globs only) | Yes |
| Mathematical Functions | No | Yes (sum, avg, etc.) |
| Visualization | Metric Graphs only | Bar, Pie, Line, Area charts |
| Output Format | Metric values / Stream | Table of results |
| Max Log Groups | 1 | 50 |