Mastering Real-Time Log Ingestion in AWS
Real-time log ingestion
Mastering Real-Time Log Ingestion in AWS
This guide covers the architecture and implementation of real-time log ingestion systems within the AWS ecosystem, specifically focusing on CloudWatch Logs, Kinesis, and Lambda processing for the DevOps Engineer Professional (DOP-C02) exam.
Learning Objectives
After studying this guide, you should be able to:
- Configure the CloudWatch Unified Agent on EC2 and on-premises instances to stream application logs.
- Implement CloudWatch Logs Subscriptions to enable real-time delivery to downstream services.
- Design ingestion pipelines using Amazon Kinesis Data Streams for high-throughput, ordered log processing.
- Apply security best practices, including IAM roles and KMS encryption, to log data in transit and at rest.
- Evaluate the appropriate destination (Lambda, Kinesis, OpenSearch) based on processing requirements.
Key Terms & Glossary
- CloudWatch Unified Agent: A cross-platform agent used to collect both internal system-level metrics and logs from EC2 instances and on-premises servers.
- Subscription Filter: A mechanism in CloudWatch Logs used to define which log events are delivered to a destination based on a specific pattern.
- Shard: The base unit of throughput in a Kinesis Data Stream; it provides a fixed capacity (1MB/sec ingress, 2MB/sec egress).
- Partition Key: A value used by producers to group data into specific shards within a stream, ensuring related log events (e.g., from the same UserID) stay together.
- Sequence Number: A unique identifier assigned by Kinesis to each data record, used to maintain ordering within a shard.
The "Big Idea"
[!IMPORTANT] The core philosophy of real-time log ingestion is decoupling collection from analysis. By streaming logs immediately into a managed buffer (like Kinesis) or a compute layer (like Lambda), organizations move from reactive "post-mortem" analysis to proactive, sub-second monitoring and automated incident response.
Formula / Concept Box
| Concept | Metric / Rule | Significance |
|---|---|---|
| Kinesis Shard Ingress | 1,000 records/sec OR 1MB/sec | Limits how fast a single shard can receive log data. |
| Kinesis Shard Egress | 2MB/sec (Standard) | Limits how fast consumers can read data from a shard. |
| Log Retention | 1 day to Indefinite | Configurable per Log Group to manage costs. |
| Max Record Size | 1 MB | The maximum size of a single data blob in Kinesis. |
Hierarchical Outline
- Log Collection Layer
- Unified Agent: Installation on EC2/On-prem.
- IAM Role: Required permission (
logs:PutLogEvents).
- Aggregation Layer (CloudWatch Logs)
- Log Groups: Logical containers for log streams.
- Metric Filters: Extracting numerical data from logs for alarms.
- Real-Time Delivery (Subscriptions)
- Subscription Filters: Pattern matching (e.g.,
?ERROR ?404). - Destinations: Kinesis Data Streams, Kinesis Data Firehose, AWS Lambda.
- Subscription Filters: Pattern matching (e.g.,
- Downstream Processing
- Kinesis Data Streams: Ordered, real-time ingestion for multiple consumers.
- Kinesis Data Firehose: Near real-time loading into S3, Redshift, or OpenSearch.
Visual Anchors
Log Ingestion Pipeline Flow
Kinesis Shard Architecture
Definition-Example Pairs
- CloudWatch Subscription Filter: A rule that selects specific log events for streaming.
- Example: Filtering a web server log group for the string
"ERROR"so that only failed requests are sent to a Lambda function for Slack notifications.
- Example: Filtering a web server log group for the string
- Partition Key: A value that determines which shard a record is assigned to.
- Example: Using
instance-idas the partition key ensures that all logs from a specific server arrive in the same shard, maintaining chronological order for that specific server.
- Example: Using
Worked Examples
Setup: Streaming Logs to OpenSearch
Goal: Take application logs from an EC2 fleet and make them searchable in Amazon OpenSearch Service in near real-time.
- Preparation: Create an Amazon OpenSearch domain and an IAM role that allows CloudWatch Logs to put data into it.
- Agent Config: On the EC2 instances, configure
amazon-cloudwatch-agentto point/var/log/app.logto a Log Group namedAppLogs. - Subscription: In the CloudWatch Console, select the
AppLogsgroup, choose Subscription Filters, and click Create Amazon OpenSearch Service subscription filter. - Transformation: Select the target OpenSearch domain. CloudWatch will automatically create a Lambda function to handle the format conversion and delivery.
- Verification: Log into the OpenSearch Dashboards (Kibana) to see incoming log entries within seconds of their generation on the EC2 instance.
Checkpoint Questions
- What is the maximum data ingress rate for a single Kinesis Shard?
- Which service would you use if you need to transform log data (e.g., anonymize PII) before it reaches its final storage destination?
- True or False: CloudWatch Logs Subscriptions can deliver data to more than one Kinesis Data Stream simultaneously.
- How does a Partition Key affect data distribution in Kinesis?
▶Click for Answers
- 1MB/sec or 1,000 records/sec.
- AWS Lambda (via a Subscription Filter or Kinesis Data Firehose transformation).
- False (one subscription per destination, but you can have multiple subscription filters on a single log group).
- It ensures that all data with the same key is mapped to the same shard, preserving the order of events for that key.
Muddy Points & Cross-Refs
- Agent vs. Unified Agent: Legacy documentation might mention the "CloudWatch Logs Agent." Always prefer the Unified Agent for modern deployments as it handles both logs and metrics in one process.
- Kinesis Streams vs. Firehose: If you need sub-second processing and complex ordering, use Data Streams. If you want a managed delivery to S3/OpenSearch with minimal code, use Firehose.
- Cross-Account Ingestion: Sending logs from Account A to Kinesis in Account B requires a Destination resource in Account B and appropriate resource-based policies.
Comparison Tables
| Feature | Kinesis Data Streams | Kinesis Data Firehose | AWS Lambda |
|---|---|---|---|
| Best For | Custom, high-speed processing | Loading data into storage | Event-driven logic/alerts |
| Scaling | Manual (shards) or On-Demand | Fully Managed | Fully Managed |
| Latency | Sub-second | 60 seconds (min) | Sub-second |
| Data Retention | 24 hours - 365 days | None (ephemeral) | None (ephemeral) |