Sub topic 8
Log groups, log streams, and retention
This chapter assumes you know what a log event is, can run AWS CLI commands, and have IAM permission to use CloudWatch Logs. Here you will learn how CloudWatch Logs organizes events and how to stop them from piling up forever.
The working model
CloudWatch Logs stores events in a three-level hierarchy:
- A log event is one record: a timestamp and a message.
- A log stream is an ordered sequence of events from a single source, such as one EC2 instance or one Lambda execution environment.
- A log group is a collection of streams that share the same retention, access control, and encryption settings.
Settings apply to the group, not to individual streams. You query, filter, and attach metric filters at the group level. This is why the agent configuration in the topic uses /app/web as the group and {instance_id} as the stream: one group for the application, one stream per instance.
Creating a group and setting retention
By default, a log group keeps events forever, and you pay to store them. Set a retention period when you create the group.
aws logs create-log-group --log-group-name /app/web
aws logs put-retention-policy \
--log-group-name /app/web \
--retention-in-days 30Retention accepts only specific values, such as 1, 3, 5, 7, 14, 30, 60, 90, 180, 365, and longer fixed periods. A value like 45 is rejected. CloudWatch deletes expired events in the background, so they can remain for a short time after the period ends.
To inspect what you have, list the groups and streams:
aws logs describe-log-groups --log-group-name-prefix /app
aws logs describe-log-streams \
--log-group-name /app/web \
--order-by LastEventTime \
--descendingThe first command shows retentionInDays for each group. If that field is missing, the group never expires.
Reading events
You can read recent events from a stream without opening the console:
aws logs tail /app/web --since 15m --followtail reads across all streams in the group. Add --log-stream-names to limit it to one stream.
Common errors
- Groups created automatically have no retention. AWS services such as Lambda, and the CloudWatch agent, create groups for you with "never expire." Set retention afterward, or create the group first.
- Streams are not deleted separately. An empty stream stays until its events expire and it is removed, or until you delete it.
- Wrong region. Logs are regional. If a group seems missing, check the region of your CLI profile.
- Permission errors from the agent. The instance role needs permission to create streams and put events, such as
logs:CreateLogStreamandlogs:PutLogEvents.
Choosing a retention period
Pick the shortest period that meets your debugging and compliance needs. Operational logs often need 14 to 90 days. Audit logs may need a year or more. Shorter retention directly lowers storage cost, which the cost chapter covers later.
Exercise
A Lambda function created its log group automatically, and storage cost keeps rising. What is the likely cause and what command fixes it?
Show answer
Automatically created log groups have no retention policy, so events never expire. Run aws logs put-retention-policy --log-group-name <group> --retention-in-days <allowed value>, for example 30.
Exercise
Why does the agent configuration use {instance_id} as the log stream name?
Show answer
It gives each instance its own stream inside the shared group, so events from different instances stay in order and can be told apart, while the group keeps one retention and access policy.