Part 2: CloudWatch Query Syntax Fundamentals
Learning Query Commands
Query Command Structure
<command> <parameters>
| <command> <parameters>
| <command> <parameters>Core Commands
1. fields - Selecting Data
# Select specific fields
fields @timestamp, @message
# Select all fields
fields @timestamp, @message, @logStream, @log
# Select and rename
fields @timestamp as time, @message as log_message
# Select using wildcards (for JSON logs)
fields @timestamp, request.*Real Example from Lambda Logs
2. filter - Filtering Data
Filter Operators
Operator
Description
Example
Real Example from API Gateway Logs
3. stats - Aggregating Data
Aggregation Functions
Function
Description
Example
Real Example from Application Logs
4. sort - Ordering Results
Real Example
5. limit - Restricting Results
6. parse - Extracting Fields
Parse Patterns
Real Example from Application Logs
7. display - Showing Fields
Working with Time
Time Functions
Time-Based Queries
Events in Last Hour
Events Per 5-Minute Interval
Events Per Hour
Real Example: Request Rate Over Time
Data Types
Supported Types
Type Conversion
String Operations
String Functions
String Matching
Real Example: Extract User Agent Info
Number Operations
Arithmetic
Math Functions
Real Example: Convert Bytes to GB
Working with JSON Logs
Accessing JSON Fields
Nested JSON Fields
Real Example: Lambda Logs (JSON)
Boolean Logic
AND Logic
OR Logic
Combined Logic
Conditional Expressions
if Function
Real Example: Categorize Response Times
Checking for Field Existence
ispresent Function
Real Example: Handle Optional Fields
Practical Query Patterns
Pattern 1: Error Rate Over Time
Pattern 2: Top Error Messages
Pattern 3: Performance by Endpoint
Pattern 4: User Activity Analysis
Pattern 5: Time-Based Comparison
Query Optimization Tips
1. Filter Early
2. Limit Field Selection
3. Use Aggregations Wisely
4. Specific Time Ranges
Common Syntax Errors I Made
Error 1: Missing Pipes
Error 2: Incorrect String Matching
Error 3: Type Mismatches
Error 4: Invalid Field Names
Key Takeaways
PreviousPart 1: Introduction to CloudWatch Logs InsightsNextPart 3: Advanced Query Operations and Functions
Last updated