Lesson 5 of 6 CloudWatch Logs and Log Analytics

Managing Log Lifecycles and Processing Log Files in Amazon S3

Many AWS services write logs as files to Amazon S3, and S3 is the most economical place for long-term log retention. This lesson explains which logs are delivered to S3, how lifecycle rules, storage classes and Object Lock manage log retention, and how S3 event notifications trigger processing of new log files, for example loading them into OpenSearch or CloudWatch Logs.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify AWS services that deliver logs to Amazon S3 and the bucket permissions they need.
  • Design S3 lifecycle rules and storage classes for log retention and cost.
  • Explain how S3 event notifications and EventBridge trigger processing of new log files.
  • Describe patterns for delivering S3 log files to OpenSearch Service and CloudWatch Logs, and pitfalls such as recursive triggers.

Logs Delivered to S3

Several AWS services deliver logs as files to S3: AWS CloudTrail trails, Elastic Load Balancing access logs, Amazon CloudFront standard logs, S3 server access logs, VPC Flow Logs (to S3 or CloudWatch Logs), AWS WAF logs, AWS Config snapshots and history, and Network Firewall logs. Each service needs a bucket policy that allows its service principal or log delivery service to write, often with conditions on the source account and source ARN to prevent the confused deputy problem. Logs from many accounts are usually delivered to a bucket in the log archive account, with prefixes per account and Region.

Lifecycle Rules and Storage Classes

Log data is accessed frequently for a few days, occasionally for weeks and rarely afterwards. S3 Lifecycle rules automate the transition between storage classes and the expiration of objects, by prefix, tag or object size.

AgeStorage classReason
0 to 30 daysS3 StandardFrequent queries during investigations
30 to 90 daysS3 Standard-Infrequent AccessLower storage price, retrieval fee
90 days to 1 yearS3 Glacier Instant RetrievalRare access with millisecond retrieval
1 to 7 yearsS3 Glacier Flexible Retrieval or Deep ArchiveLowest cost; retrieval in minutes to hours
After 7 yearsExpireEnd of retention requirement

When access patterns are unknown, S3 Intelligent-Tiering moves objects between tiers automatically. Lifecycle rules should also expire noncurrent versions in versioned buckets and abort incomplete multipart uploads. Because transitions have per-object charges and minimum storage durations, many tiny log files may cost more to transition than they save, so aggregating small files, for example with Firehose buffering, helps.

For tamper-proof audit logs, S3 Object Lock in compliance mode with a default retention period prevents deletion or modification by anyone for the retention period, and CloudTrail log file integrity validation, described in the auditing module, proves logs were not altered.

Processing New Log Files with S3 Events

When a log file arrives, processing can start automatically. S3 event notifications send s3:ObjectCreated:* events, filtered by prefix and suffix, to a Lambda function, an SQS queue or an SNS topic. Alternatively, enabling Amazon EventBridge for the bucket sends all object events to EventBridge, where rules with rich filtering can route them to many targets, including Step Functions, and several rules can consume the same event. Using an SQS queue between S3 and the processor adds buffering, retries and a dead-letter queue, which protects against bursts and failures.

S3 event notifications
TargetsLambda, SQS, SNS
FilteringPrefix and suffix
Multiple consumersOne destination per non-overlapping event and prefix
Choose when Simple, direct processing of one file type.
EventBridge
TargetsOver 20 target types, including Step Functions and other buses
FilteringAny event field, including size and key patterns
Multiple consumersMany rules on the same events
Choose when Complex routing or several independent consumers.

Recursive triggers

A Lambda function triggered by object creation that writes its output to the same bucket and prefix triggers itself again, creating a recursive loop. Write outputs to a different bucket or prefix and scope the trigger with prefix filters. Lambda's recursive loop detection stops some such loops, but the design should prevent them.

Scenario questions

Process log files in S3

Exam skills 4.3.3 · 4.1.5

Practise S3 events and lifecycle rules for log files.

Log in to try this activity Interactive activities are free with an account.

Delivering Log Files to OpenSearch and CloudWatch Logs

Two delivery patterns from S3 are named in the exam guide. To OpenSearch Service, a Lambda function triggered by the S3 event (directly or through SQS) reads the new log file, parses each line into a JSON document and indexes it in bulk, using a role mapped to an OpenSearch role with write permission; at larger scale, an OpenSearch Ingestion pipeline with an S3 source reads files from S3 using SQS notifications, parses and transforms them and writes to the domain or serverless collection without custom code. To CloudWatch Logs, a Lambda function reads the file and writes events with PutLogEvents to a log group, which makes S3-only logs, such as ALB access logs, available to metric filters, Logs Insights and alarms.

Worked example

Searching Load Balancer Logs

A team wants ALB access logs from 15 load balancers to be searchable within minutes in OpenSearch Dashboards, kept in S3 for one year at low cost, and alarmed on when a single client IP exceeds a request threshold.

  1. Delivery

    Enable ALB access logging to a central bucket with the required bucket policy.

  2. Processing

    Configure S3 event notifications for the access log prefix to an SQS queue, and an OpenSearch Ingestion pipeline with an S3 source that reads from the queue, parses the ALB log format and indexes into a daily index.

  3. Lifecycle

    Lifecycle rules move logs to Standard-IA after 30 days and Glacier Instant Retrieval after 90, and expire them after 365.

  4. Alerting

    Use an OpenSearch alerting monitor on request counts per client IP, or load the logs into CloudWatch Logs and use Contributor Insights.

Check yourself

A Lambda function triggered by S3 object creation processes each log file and writes a summary file to the same bucket. Lambda invocations increase uncontrollably. Why?

Show answer

Each summary file is itself an object creation event that triggers the function again, creating a recursive loop. Write summaries to another bucket or a prefix excluded from the trigger.

Exam Tips

Remember for the exam

  • CloudTrail, ELB, CloudFront, S3 server access, VPC Flow Logs, WAF and Config deliver logs to S3; bucket policies must allow the service principals.
  • Lifecycle rules transition logs through Standard-IA and Glacier classes and expire them; Object Lock compliance mode makes them immutable.
  • S3 event notifications target Lambda, SQS or SNS with prefix and suffix filters; EventBridge supports richer filtering and many consumers.
  • Load S3 log files into OpenSearch with Lambda or an OpenSearch Ingestion pipeline, and into CloudWatch Logs with Lambda and PutLogEvents.
  • Avoid recursive triggers by writing outputs to a different bucket or prefix.