Volume 6 · Chapter 3 — Observability and Operations
CloudWatch Logs and Log Analysis
3.1 Introduction
When a metric tells you something is wrong, logs tell you what happened. CloudWatch Logs is where AWS services and your applications send their log data, and it’s the primary tool for investigating incidents in detail. This chapter covers how logs are organized, why structured logging matters, how to search logs effectively, and how correlation ties logs to the rest of your telemetry - the practical skills that turn a pile of log lines into fast root-cause analysis.
3.2 How CloudWatch Logs Works
CloudWatch Logs collects and stores log data from applications and AWS services. Logs are organized into log groups (typically one per application or service) containing log streams (individual sources, like a specific instance or container). Applications send logs via an agent or SDK; many AWS services send logs directly - Lambda functions, API Gateway, VPC flow logs, and more. Once centralized, logs can be searched, filtered, and retained according to policy, and you can set retention so old logs expire (managing cost) or are archived. Centralizing logs is itself valuable: in a distributed, auto-scaling system where instances come and go, you can’t log into servers to read files - centralized logging is the only practical way to see what happened across an ephemeral fleet.
3.3 Structured Logging
The single biggest improvement to log usefulness is structured logging - emitting logs as structured data (typically JSON) with consistent fields, rather than free-form text. A structured log entry has named fields: timestamp, level, service, a correlation ID, the user or transaction, the event, and relevant context. Structured logs are machine-parseable, so you can filter and query on specific fields - “show all error logs for this correlation ID” or “count failures by endpoint” - which is impossible to do reliably with inconsistent free text. For a bank investigating an incident, structured logs are the difference between quickly querying exactly the relevant events and manually scanning thousands of unstructured lines. Emitting structured logs with consistent fields is a foundational instrumentation practice.
{
"timestamp": "2026-07-24T14:03:11Z",
"level": "ERROR",
"service": "payments",
"correlationId": "req-8f3a2b",
"event": "transfer_failed",
"reason": "downstream_timeout",
"accountId": "acct-123",
"amount": 100.00
}
3.4 Searching and Analyzing Logs
Centralized structured logs become powerful when you can query them. CloudWatch Logs Insights lets you run queries over log data - filtering by fields, aggregating, counting, and finding patterns - so you can answer investigative questions quickly: how many errors of a given type occurred, which requests failed and why, what the logs for a specific correlation ID show across services. This turns logs from a passive archive into an active diagnostic tool. During an incident, the workflow is often: an alert fires on a metric, you open the dashboard to see scope, then you query the logs (filtered by the failing requests’ correlation IDs or error type) to see exactly what happened - moving from symptom to cause. The ability to query logs efficiently is what makes centralized logging pay off.
3.5 Correlation and Retention
Two practices maximize log value. Correlation: including a correlation ID (and trace ID) in every log entry lets you follow a single request across all the services it touched by filtering on that ID - essential in distributed systems where one request’s story is spread across many services’ logs. Retention: logs cost money to store, so you set retention policies to keep logs as long as needed for operations and compliance (a bank may need to retain certain logs for years) and expire or archive the rest, and you can send important logs to cheaper long-term storage like S3. The balance is keeping enough log history to investigate incidents and meet compliance, without paying to store high-volume logs indefinitely - achieved with tiered retention and archival.
3.6 Banking Production Scenario
A bank centralizes all logs in CloudWatch Logs, one log group per service, with every service emitting structured JSON logs that include a correlation ID, service name, level, event, and context. Audit-relevant logs are retained for the compliance-required period and archived to S3 for cheap long-term storage, while high-volume operational logs have shorter retention to control cost. When a payment incident occurs, the on-call engineer uses Logs Insights to query for the failing transactions’ correlation IDs, instantly seeing the exact error, the reason, and the sequence of events across services - diagnosing root cause in minutes. Because logs are structured and centralized, investigation is a precise query rather than a manual hunt through ephemeral instances that may no longer exist.
3.7 Troubleshooting: Logging Problems
Log-related difficulties usually come from missing centralization, structure, or correlation. If you can’t find logs for a failed request because the instance is gone, logs weren’t centralized - send them to CloudWatch Logs so they persist beyond the instance. If you can’t query logs to answer specific questions, they’re unstructured free text - move to structured logging with consistent fields. If you can’t follow a request across services, logs lack a correlation ID - add one that propagates through the request. If logging costs are high, retention is too long for high-volume logs - set tiered retention and archive to S3. If logs are missing entirely for a service, the agent or logging configuration isn’t set up. The discipline is centralized, structured, correlated logs with sensible retention - which makes incident investigation fast and keeps costs controlled.
Why is structured, centralized logging with correlation IDs so important in a distributed system?
In a distributed, auto-scaling system, structured centralized logging with correlation IDs is what makes incident investigation possible at all, and the three properties each solve a specific problem. Centralization matters because in a modern system instances and containers are ephemeral - they come and go with auto-scaling and deployments - so you can’t log into a server to read its log files; the server may not exist anymore by the time you investigate. Sending all logs to a central store like CloudWatch Logs means the logs persist independently of the instances that produced them, and you have one place to look across the entire fleet rather than hunting across disappearing machines. Without centralization, the logs for a failed request might simply be gone. Structure matters because it determines whether you can actually query the logs. Free-form text logs can only be scanned or crudely grep’d, but structured logs - emitted as JSON with consistent named fields like timestamp, level, service, correlation ID, event, and context - are machine-parseable, so you can run precise queries: filter to error logs for a specific transaction, count failures by endpoint, aggregate by reason. During an incident, that’s the difference between quickly querying exactly the relevant events and manually reading thousands of inconsistent lines under pressure. Correlation IDs matter because in a distributed system a single request’s story is spread across many services’ logs - the request touches the gateway, the payments service, a downstream call, the database - and each logs separately. If every log entry for that request carries the same correlation ID, you can filter on that ID and see the complete journey of the request across every service it touched, reconstructing exactly what happened and where it failed. Without it, you have disconnected fragments in different services with no way to tie them into one request’s story. Put together, these enable the core incident workflow: a metric alert fires, you check the dashboard for scope, then you query the centralized structured logs filtered by the failing requests’ correlation IDs and immediately see, across all services, what happened and why - root cause in minutes instead of hours. This is also why correlation IDs should propagate through the whole request path and tie logs to traces. So structured, centralized, correlated logging isn’t a nicety - it’s the practical foundation that lets you investigate a distributed system where problems span many ephemeral services, turning logs from an unsearchable, scattered, disappearing mess into a precise, queryable, end-to-end diagnostic tool.
Chapter Revision Notes
- CloudWatch Logs centralizes logs from services and applications into log groups/streams - essential when instances are ephemeral.
- Structured logging (JSON with consistent fields) makes logs queryable; free text can’t be reliably filtered or aggregated.
- Logs Insights queries logs to answer investigative questions - counts, filters, patterns - turning logs into an active diagnostic tool.
- Correlation IDs in every log entry let you follow one request across all services; propagate them through the request path.
- Set tiered retention and archive to S3 to balance investigation/compliance needs against storage cost.