Skip to Content

CloudWatch Logs

CloudWatch Logs groups and streams, retention, encryption and log classes, log sources, Logs Insights, exporting to S3, subscriptions, cross-account aggregation, and the CloudWatch agent.

Log groups and streams

CloudWatch Logs stores and analyses logs from applications, systems and AWS services.

  • Log groups: an arbitrary name, usually representing an application (/myapp/prod).
  • Log streams: instances within the application, log files or containers.
  • Expiration policies: never expire (the default) or 1 day to 10 years.
  • Logs are encrypted by default; KMS with customer-owned keys is also supported.
  • CloudWatch Logs can send logs to S3 (exports), Kinesis Data Streams, Data Firehose, Lambda and OpenSearch.
  • Live Tail shows new log events as they arrive, for troubleshooting.
  • Two log classes: Standard, and the cheaper Essentials class (formerly Infrequent Access) with a subset of features, for logs that are rarely looked at.

Sources:

  • the SDK, the CloudWatch agent;
  • Elastic Beanstalk: logs from the application;
  • ECS: logs from containers;
  • Lambda: function logs;
  • VPC Flow Logs: VPC traffic logs;
  • API Gateway access and execution logs;
  • CloudTrail, based on a filter;
  • Route 53: DNS query logs.

CloudWatch Logs Insights

Logs Insights searches and analyses log data stored in CloudWatch Logs.

  • Example: find a specific IP address in a log, count occurrences of "ERROR" in the logs.
  • A purpose-built query language (OpenSearch PPL and SQL also work):
    • it automatically discovers fields from AWS services and JSON log events;
    • fetch fields, filter on conditions, calculate aggregate statistics, sort, limit…
    • save queries and add them to dashboards.
  • Query multiple log groups, even in different AWS accounts.
  • It's a query engine, not a real-time engine.
fields @timestamp, @message
| filter @message like /ERROR/
| stats count(*) as errors by bin(5m)

Exporting to S3

  • Export log data to S3 in batches with the CreateExportTask API.
  • Log data can take up to 12 hours to become available for export.
  • It's not near real time or real time: for that, use subscriptions.

Subscriptions

CloudWatch Logs subscriptions: a subscription filter selects log events and sends them in real time to a Lambda function (for example to load OpenSearch), to Firehose (near real time, to S3 or OpenSearch), or to Kinesis Data Streams for Flink or custom apps CloudWatch Logs subscriptions: a subscription filter selects log events and sends them in real time to a Lambda function (for example to load OpenSearch), to Firehose (near real time, to S3 or OpenSearch), or to Kinesis Data Streams for Flink or custom apps

A real-time feed of log events.
  • Get a real-time feed of log events from CloudWatch Logs for processing and analysis.
  • Send it to Kinesis Data Streams, Data Firehose or Lambda (see the OpenSearch patterns).
  • A subscription filter chooses which log events are delivered to the destination.
  • Cross-account subscription: send log events to a Kinesis stream or Firehose in a different AWS account. The recipient account creates a destination with a destination access policy allowing the sender, and an IAM role that can write to the stream.

Multi-account, multi-Region aggregation

Aggregating logs from several accounts and Regions: CloudWatch Logs subscription filters in account A Region 1 and account B Regions 2 and 3 send log events to one Kinesis data stream in a central account, and Firehose delivers them to a single S3 bucket in near real time Aggregating logs from several accounts and Regions: CloudWatch Logs subscription filters in account A Region 1 and account B Regions 2 and 3 send log events to one Kinesis data stream in a central account, and Firehose delivers them to a single S3 bucket in near real time

Every account's logs in one central bucket.

Subscription filters in each account and Region send logs to one Kinesis data stream, and Firehose delivers them to one S3 bucket in near real time. (CloudWatch Logs can now also centralize logs across accounts and Regions by itself.)

CloudWatch agent for EC2 and on premises

  • By default, no logs from an EC2 instance go to CloudWatch.
  • A CloudWatch agent must run on EC2 to push the wanted log files.
  • Make sure IAM permissions are correct (an instance role).
  • The agent also runs on premises.

Two agents:

  • CloudWatch Logs agent: the old version; can only send to CloudWatch Logs.
  • CloudWatch unified agent: collects logs and extra system-level metrics such as RAM and processes. Centralized configuration in SSM Parameter Store.

Unified agent metrics, collected directly on a Linux server or EC2 instance:

  • CPU (active, guest, idle, system, user, steal);
  • disk metrics (free, used, total) and disk I/O (writes, reads, bytes, IOPS);
  • RAM (free, inactive, used, total, cached);
  • netstat (TCP and UDP connections, packets, bytes);
  • processes (total, dead, blocked, idle, running, sleeping);
  • swap space (free, used, used %).

Reminder: out-of-the-box EC2 metrics are only high level (CPU, disk I/O, network).

Real-world examples

  • An operations team runs a Logs Insights query across 40 Lambda log groups to find which function threw the most timeouts last night.
  • A bank keeps 7 years of logs: a subscription with Firehose writes them to S3 Glacier storage classes, and CloudWatch keeps only 30 days.

Exam tips

  • "Analyse or search logs with queries" → Logs Insights.
  • "Logs to S3 in batch" → export (CreateExportTask, up to 12 hours). "Real time / near real time" → subscription filter to Kinesis, Firehose or Lambda.
  • "Centralize logs from many accounts and Regions" → subscriptions to one Kinesis stream (cross-account destination), then Firehose to S3.
  • "Collect RAM, disk, processes from EC2 or on premises" → CloudWatch unified agent.
  • "No logs from EC2 in CloudWatch" → install the agent and check the instance role permissions.

Common traps

  • "EC2 sends its application logs to CloudWatch automatically." Only with the agent.
  • "Use export for real-time processing." Exports can lag 12 hours; use subscriptions.
  • "Logs Insights alerts in real time." It's a query engine; for alerts use metric filters and alarms.

Check yourself

The agent lab needs an EC2 instance and an IAM role (covered later in Systems Manager), so instead:

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt