Skip to main content
This guide expands on CloudWatch Logs and CloudWatch Logs Insights with practical architecture, ingestion patterns, cost and retention considerations, security, metric and subscription filters, query optimization, and integration strategies. It’s designed for engineers operating observability at scale and for those preparing for AWS exams. What this article covers
  • CloudWatch Logs architecture and data flow
  • Ingestion methods and trade-offs
  • Retention, storage, and cost considerations
  • Security and encryption controls
  • Metric filters and subscription filters (including common limitations)
  • CloudWatch Logs Insights querying best practices
  • Integration patterns, cost-optimization strategies, and exam-relevant takeaways
Cost numbers change frequently. Any dollar amounts shown here are directional examples — treat them as illustrative, not authoritative.

Architecture: log groups → log streams → log events

CloudWatch Logs uses a simple three-level hierarchy that determines organization, retention, and deployment patterns:
  • Log group: a top-level container for related logs; retention policies and resource policies are applied here.
  • Log stream: ordered sequence of log events, often associated with an instance, container, or Lambda execution environment.
  • Log event: a single timestamped record (a single line or structured JSON entry).
Example: a payment-processor Lambda might have one log group for the function. Each execution environment may create a separate log stream; log events are the individual lines (or JSON objects) emitted during invocation.
A slide titled "CloudWatch Logs Architecture" showing a three-layer pyramid that illustrates the hierarchy: Log Groups (top), Log Streams (middle), and Log Events (bottom). The graphic summarizes how CloudWatch structures log data.

Log ingestion methods — choose by environment and latency needs

You can mix ingestion approaches depending on the deployment model, volume, and real-time requirements: Links:
A slide titled "Log Ingestion Methods" displaying three colored panels: CloudWatch Unified Agent, SDK/API Direct Ingestion, and Service-Native Integration, each with brief bullet points describing how they collect and handle logs. The slide includes small icons for each method and a © Copyright KodeKloud note.
Practical guidance:
  • Use agents for full-host telemetry and system logs.
  • Use PutLogEvents or SDK for short-lived containers or services where installing an agent is impractical.
  • Rely on service-native integration for AWS-managed services to avoid custom ingestion plumbing.

Retention and cost considerations

Retention is configured at the log group level; all streams in a log group share the same retention period. Choose log-group boundaries carefully so you can apply different retention rules (e.g., audit logs vs. debug logs). Common retention options include 1, 3, 5, 7, 14 days, monthly/yearly options, and “Never Expire” (default). Billing model (directional examples):
  • Ingestion: charged per GB ingested.
  • Storage: charged per GB-month for retained logs.
  • Insights: charged per GB scanned by queries.
  • Custom metrics: metric filters can produce metrics which may incur CloudWatch custom metric charges.
Plan retention around compliance and cost: keep audit/security logs longer; set debug/noisy logs to short retention or export to S3.

Encryption, network security, and IAM

  • At-rest: CloudWatch Logs supports KMS encryption using AWS-managed keys or customer-managed CMKs in KMS. Use CMKs when compliance or auditability requires it.
  • In-transit: log delivery uses TLS (TLS 1.2+).
  • IAM: grant least privilege to agents, services, or applications that push logs.
  • Cross-account: use resource policies and IAM roles for cross-account delivery.
  • Network: use VPC interface endpoints for CloudWatch Logs to avoid public internet egress.
A presentation slide titled "Encryption and Security" showing four numbered boxes: Encryption at Rest, Encryption in Transit, IAM Permissions, and Cross-Account Access. Each box highlights brief points like KMS encryption, TLS 1.2+, least-privilege IAM, and cross-account access.
Best practice checklist:
A presentation slide titled "Encryption and Security" with a "Best Practices" header.It lists five recommendations: use KMS encryption for sensitive logs, implement least-privilege IAM, use resource-based policies for cross-account scenarios, enable CloudTrail auditing, and apply IAM conditions to restrict access by source IP or VPC.

Metric filters — derive metrics from log events

Metric filters extract numeric values or count occurrences from incoming log events and create CloudWatch metrics in near-real time. Key points:
  • Real-time: metric filters apply to new incoming log events (not retroactive).
  • Capabilities: pattern matching, namespaces, dimensions, and simple stats (count/sum/avg/min/max).
  • Limits: 100 metric filters per log group.
  • Cost: metric filters themselves do not add a separate log-processing charge beyond ingestion; however, created custom metrics may incur CloudWatch metric charges.
A slide titled "Metric Filters – Creating Metrics From Logs" showing a funnel labeled Metric Filters that transforms unstructured log data into actionable CloudWatch metrics. The diagram visually maps logs on the left to CloudWatch metric reports on the right.
A presentation slide titled "Metric Filters – Creating Metrics From Logs" with a four‑segment circular infographic showing key capabilities: Pattern Matching, Namespaces, Dimensions, and Stats. Each segment gives a short description (pull numbers from logs, organize metrics, add env/region filters, compute sum/avg/min/max/count).
Slide titled "Metric Filters – Creating Metrics From Logs" showing two info cards: the left card says "$0 Processing Cost" with a note that metric filters are free after initiating log ingestion, and the right card says "100 Filters per Group" with a note about the maximum filters allowed per log group.
Use metric filters when you need low-latency numeric indicators (e.g., error counts, specific response codes) that feed alarms or dashboards without reprocessing logs in Insights.

Subscription filters — streaming and the “single filter” constraint

Subscription filters stream matching log events to destinations such as Lambda, Kinesis Data Streams, or Kinesis Data Firehose. Important limit: each log group supports only one active subscription filter.
Each log group supports only one subscription filter. If multiple consumers are required, implement a fan‑out pattern (Kinesis Data Streams or a central Lambda that forwards to other consumers) or use separate log groups.
Patterns and recommendations:
  • Fan-out: use Kinesis Data Streams to allow multiple consumers to read the same stream concurrently.
  • Firehose: use for buffering, transformation, and delivery to S3/OpenSearch/Redshift/Splunk.
  • Cross-account: configure IAM roles and resource policies for cross-account subscription delivery.
  • Centralization: common architectures forward logs from many accounts to a central logging/analytics account.
A presentation slide titled "Subscription Filters – The ONE Filter Limitation" showing a four-step timeline with numbered boxes: Destinations, One Filter Rule, Fan-Out Pattern, and Cross-Account Delivery. Each box briefly explains log streaming options (Kinesis Streams, Lambda, Firehose) and notes that each log group supports only one subscription filter.

CloudWatch Logs Insights — query efficiently at scale

CloudWatch Logs Insights offers a log query language to search, parse, aggregate, and visualize log data. It is designed for interactive troubleshooting and analytics. Billing model:
  • Insights charges per GB scanned. Be selective in which log groups and time ranges you query.
Query example (CloudWatch Logs Insights):
Best practices for Insights:
  • Narrow log groups, use explicit filter expressions, and limit time ranges to reduce scanned GB.
  • Prefer structured logs (JSON) so you can parse fields and reduce text scanning.
  • Use fields to limit returned data and avoid unnecessary scanning/transfer.
A presentation slide titled "CloudWatch Logs Insights – Querying at Scale" that says it includes a purpose-built query language. Four colorful circular icons illustrate capabilities: searching logs, filtering events, extracting fields, and aggregating/visualizing results.

Cost optimization strategies

Manage observability costs while retaining actionable visibility:
  • Selective retention: configure different retention for audit vs. debug logs.
  • Export to S3: export older logs to S3 and use lifecycle policies (Glacier/Deep Archive) for long-term storage.
  • Log sampling: sample high-volume, low-value events (e.g., 1–10%) but do not sample critical security/audit logs.
  • Structured logging: emitting JSON reduces parsing cost and improves Insights performance.
A presentation slide titled "Cost Optimization Strategies" showing four colorful rounded cards—Selective Retention, S3 Export for Archives, Log Sampling, and Structured Logging—each with a short description and an icon.
Practical tips:
  • Use lifecycle policies on S3 to move older logs to Glacier for cost savings.
  • Consider Firehose for batched ingestion to S3 and downstream analytics.
  • Aggregate high-cardinality fields only when necessary to avoid exploding metric counts.

Integration patterns

Common deployment patterns to centralize and analyze logs:
  • Cross-account aggregation: forward logs from multiple accounts into a central logging account for security analysis.
  • Multi-region consolidation: centralize logs to a single region/account for simplified analytics and dashboards.
  • SIEM / third-party: deliver logs to Splunk, Datadog, Sumo Logic, etc., for advanced correlation and threat detection.
  • OpenSearch: use Firehose to index logs into OpenSearch for full-text search and dashboards.
An infographic titled "Integration Patterns" showing a cloud icon with four colored semicircular segments. Each segment describes a log integration option: Cross‑Account Aggregation, Multi‑Region Consolidation, SIEM Integration, and OpenSearch Integration.

Operational best practices and exam tips

  • Use structured logging (JSON) and consistent field names to make parsing and queries reliable.
  • Apply appropriate log levels; avoid storing debug-level logs long-term.
  • For multiple subscription consumers, design fan-out via Kinesis Data Streams or a central Lambda.
  • Offload long-term archives to S3/Glacier; CloudWatch Logs is not optimized for the cheapest long-term storage.
  • Keep KMS key policies and grants under control—missing key grants can block access.
  • Monitor ingestion rates, storage, and set billing alarms to detect runaway costs.
A slide titled "Best Practices and Exam Tips" with three colorful panels highlighting logging recommendations: Structured Logging (use JSON), Appropriate Log Levels (use DEBUG/INFO/WARN/ERROR), and Core-Aware Architecture (retention policies, monitor ingestion, set billing alarms).

Workarounds & exam-focus items

  • One-subscription-filter-per-log-group: exam favorite. Solutions include multiple log groups, a Kinesis Data Stream for fan-out, or a Lambda that fans out to downstream consumers.
  • Metric filters are not retroactive: re-ingest historical logs if you need metrics from past data.

Key takeaways

  • Hierarchy: log groups → log streams → log events.
  • A log group allows only one subscription filter—plan fan-out accordingly.
  • Retention is at the log-group level; archive to S3/Glacier to save costs.
  • Filter and sample noisy events early to reduce ingestion, storage, and query costs.
  • Metric filters create low-latency metrics (limit: 100 per log group) but apply only to new events.
  • Tailor ingestion and integration patterns for real-time consumers vs. batched analytics.
A colorful presentation slide titled "Key Takeaways" showing numbered points about logging best practices, including a three-tier hierarchy, one subscription filter, flexible retention, cost optimization, and Logs Insights pricing.

Suggested hands-on practice and references

  • Try creating log groups with different retention settings and experiment with metric filters and custom metrics.
  • Build a sample fan-out: push logs from a test log group to a Kinesis Data Stream and attach multiple consumers.
  • Run a few Logs Insights queries on structured JSON logs and measure GB scanned.
Key documentation: That wraps up this lesson on CloudWatch Logs and Logs Insights beyond the basics. Practice the patterns described here with demo exercises and scenario-based questions to validate your understanding. Good luck!

Watch Video