AWS Log Analysis: Athena, CloudWatch Insights, and OpenSearch
Analyzing logs with AWS services (for example, Amazon Athena, CloudWatch Logs Insights)
AWS Log Analysis: Athena, CloudWatch Insights, and OpenSearch
This guide covers the essential services and techniques for auditing, monitoring, and analyzing logs within the AWS ecosystem, specifically tailored for the DevOps Engineer Professional (DOP-C02) exam.
Learning Objectives
By the end of this module, you should be able to:
- Differentiate between Amazon Athena and CloudWatch Logs Insights for specific log analysis use cases.
- Configure CloudWatch Metric Filters and Metric Streams to generate actionable data from raw logs.
- Implement Log Subscriptions to forward data to Amazon OpenSearch, Lambda, or Kinesis.
- Design cost-effective log storage lifecycles using Amazon S3 and CloudWatch retention policies.
- Analyze real-time and historical security events using CloudTrail and VPC Flow Logs.
Key Terms & Glossary
- Log Stream: A sequence of log events that share the same source (e.g., a specific EC2 instance or Lambda function execution).
- Log Group: A collection of log streams that share the same retention, monitoring, and access control settings.
- Subscription Filter: A mechanism to stream log events to other services (Lambda, Kinesis, OpenSearch) in near real-time.
- Metric Filter: A pattern matching rule that extracts numerical data from log events to create CloudWatch Metrics.
- Partitioning (Athena): The process of organizing data in S3 (e.g., by year/month/day) to improve query performance and reduce cost.
The "Big Idea"
Logs are the "truth" of your system, but raw text is unusable at scale. The goal of AWS log analysis is to move from Passive Storage (just keeping files) to Active Intelligence. This involves a pipeline: Collection (CloudWatch Agent) Aggregation (Log Groups/S3) Analysis (Insights/Athena) Visualization (Dashboards/QuickSight).
Formula / Concept Box
| Feature | CloudWatch Logs Insights | Amazon Athena | Amazon OpenSearch (ELK) |
|---|---|---|---|
| Query Language | Proprietary Pattern Syntax | Standard SQL | DSL / Lucene / SQL |
| Data Source | Logs in CloudWatch Log Groups | Logs stored in S3 | Indexed data in OpenSearch |
| Ideal Use Case | Ad-hoc troubleshooting, quick searches | Complex joins, historical long-term analysis | Real-time dashboards, full-text search |
| Pricing | Per GB of data scanned | Per TB of data scanned | Per instance hour + EBS storage |
Hierarchical Outline
- I. CloudWatch Logs Ecosystem
- CloudWatch Agent: Collecting custom OS-level metrics and file-based logs.
- Metric Filters: Creating alarms from log patterns (e.g., counting "404" errors).
- Logs Insights: Interactive querying (, , ).
- II. Long-Term Analysis with Athena
- S3 Export: Moving logs from CW to S3 (not real-time).
- Direct S3 Ingestion: VPC Flow Logs, CloudTrail, and ALB logs delivered directly to S3.
- AWS Glue: Using crawlers to automatically discover schema for Athena.
- III. Real-Time Streaming & Search
- Subscription Filters: Pushing logs to Kinesis Data Firehose OpenSearch.
- Lambda Transformation: Cleaning or enriching logs before they reach the destination.
- IV. Security & Compliance
- KMS Encryption: Encrypting log groups at rest.
- Retention Policies: Automatically deleting logs to save costs (e.g., 30 days for Dev, 365 for Prod).
Visual Anchors
Log Ingestion and Analysis Flow
CloudWatch vs. Athena Scope
Definition-Example Pairs
- Metric Filter: A rule to turn log text into numbers.
- Example: Searching for the string
"ERROR"in an application log and creating a metricErrorCount. IfErrorCount > 5in 1 minute, trigger an SNS notification.
- Example: Searching for the string
- Logs Insights Parse: A command to extract fields from a raw log string.
- Example:
parse @message "[*] *" as level, msgtakes a log like[INFO] User logged inand creates searchable fieldslevel="INFO"andmsg="User logged in".
- Example:
- Athena Partitioning: Organizing S3 folders to limit data scanned.
- Example: Storing logs in
s3://my-bucket/year=2023/month=10/day=27/. Athena only scans the specific folder for that day's query, significantly reducing cost.
- Example: Storing logs in
Worked Examples
Case 1: Querying for 403 Forbidden Errors in CloudWatch Insights
To find the most frequent IP addresses causing access denied errors in an ALB log group:
fields @timestamp, @message
| filter @message like /403/
| parse @message "* * * * * * * * * * *" as time, elb, client_ip, target_ip, request_processing_time, target_processing_time, response_processing_time, elb_status_code, target_status_code, received_bytes, sent_bytes
| stats count(*) as errorCount by client_ip
| sort errorCount desc
| limit 10Case 2: Athena Query for CloudTrail Security Audit
To find who deleted an S3 bucket in the last 24 hours:
SELECT eventTime, eventName, userIdentity.arn, requestParameters
FROM cloudtrail_logs
WHERE eventName = 'DeleteBucket'
AND eventTime > '2023-10-26T00:00:00Z'
ORDER BY eventTime DESC;Checkpoint Questions
- You need to perform a complex SQL join between VPC Flow Logs and a customer metadata table. Which service is most appropriate?
- What is the most cost-effective way to store logs that must be kept for 7 years but are rarely accessed?
- How do you trigger an AWS Lambda function every time a specific keyword appears in your CloudWatch Logs?
- Does CloudWatch Logs Insights require you to set up a server or index data beforehand?
[!TIP] Answers: 1. Amazon Athena (supports SQL joins). 2. Export to S3 and use S3 Glacier Lifecycle policies. 3. Use a CloudWatch Logs Subscription Filter. 4. No, it is a serverless, on-demand query engine.
Muddy Points & Cross-Refs
- Latency: CloudWatch Logs Insights is near-instant for data already in the log group. Athena depends on the data being delivered to S3 (which can have a 5-15 minute lag for services like VPC Flow Logs).
- Concurrency: Athena has service quotas on concurrent queries; it is not meant for high-concurrency application backends (use OpenSearch for that).
- Cross-Account: To analyze logs across accounts, use CloudWatch Cross-Account Observability or centralize logs into a single S3 bucket for Athena analysis.
Comparison Tables: Log Analysis Strategy
| Requirement | Recommended Path |
|---|---|
| Immediate Operational Debugging | CloudWatch Logs Insights |
| Security Forensics (Long Term) | S3 + Athena |
| Real-time Dashboard (Kibana) | OpenSearch Service |
| Triggering Auto-Scaling | Metric Filter CloudWatch Metric Scaling Policy |
| Reporting to Business Users | Athena Amazon QuickSight |