Study Guide920 words

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) \rightarrow Aggregation (Log Groups/S3) \rightarrow Analysis (Insights/Athena) \rightarrow Visualization (Dashboards/QuickSight).

Formula / Concept Box

FeatureCloudWatch Logs InsightsAmazon AthenaAmazon OpenSearch (ELK)
Query LanguageProprietary Pattern SyntaxStandard SQLDSL / Lucene / SQL
Data SourceLogs in CloudWatch Log GroupsLogs stored in S3Indexed data in OpenSearch
Ideal Use CaseAd-hoc troubleshooting, quick searchesComplex joins, historical long-term analysisReal-time dashboards, full-text search
PricingPer GB of data scannedPer TB of data scannedPer 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 (parseparse, filterfilter, statsstats).
  • 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 \rightarrow 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

Loading Diagram...
Figure 1 — Mermaid diagram

CloudWatch vs. Athena Scope

Compiling TikZ diagram…
Running TeX engine…
This may take a few seconds
Figure 2 — TikZ diagram

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 metric ErrorCount. If ErrorCount > 5 in 1 minute, trigger an SNS notification.
  • Logs Insights Parse: A command to extract fields from a raw log string.
    • Example: parse @message "[*] *" as level, msg takes a log like [INFO] User logged in and creates searchable fields level="INFO" and msg="User logged in".
  • 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.

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:

sql
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 10

Case 2: Athena Query for CloudTrail Security Audit

To find who deleted an S3 bucket in the last 24 hours:

sql
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

  1. You need to perform a complex SQL join between VPC Flow Logs and a customer metadata table. Which service is most appropriate?
  2. What is the most cost-effective way to store logs that must be kept for 7 years but are rarely accessed?
  3. How do you trigger an AWS Lambda function every time a specific keyword appears in your CloudWatch Logs?
  4. 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

RequirementRecommended Path
Immediate Operational DebuggingCloudWatch Logs Insights
Security Forensics (Long Term)S3 + Athena
Real-time Dashboard (Kibana)OpenSearch Service
Triggering Auto-ScalingMetric Filter \rightarrow CloudWatch Metric \rightarrow Scaling Policy
Reporting to Business UsersAthena \rightarrow Amazon QuickSight

Ready to study AWS Certified DevOps Engineer - Professional (DOP-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up needed.

Start Studying — Free