Lesson 1 of 5 CloudWatch Logs and the CloudWatch Agent

Configuring Log Groups, Retention and Encryption

This lesson explains how configuring CloudWatch Logs log groups correctly controls cost, compliance and who can read sensitive log data.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe log groups, log streams and events.
  • Set retention and choose log classes.
  • Encrypt log groups with a customer managed key.

Log groups, log streams and events

A log event is a timestamped message. Events from one source, such as one instance's /var/log/messages or one Lambda execution environment, form a log stream, and streams that share settings belong to a log group. Retention, encryption, metric filters, subscription filters and access policies are all set on the log group. Lambda writes to /aws/lambda/<function> by default; VPC Flow Logs, Route 53 query logs, CloudTrail and API Gateway write to log groups you choose.

Retention and log classes

New log groups keep data forever by default, which is the main cause of unexpected CloudWatch Logs storage costs. Set a retention period from one day to ten years on each group; expired events are deleted automatically. A common pattern is to enforce retention with an AWS Config rule (cw-loggroup-retention-period-check) and remediate with an Automation runbook. For long-term archival at low cost, export to S3 or stream logs to S3 through Firehose and apply S3 lifecycle rules.

Log groups use one of two log classes. The Standard class supports every feature. The Infrequent Access class costs less per GB ingested and suits logs that are rarely queried, but it does not support metric filters, subscription filters, Live Tail or alarms on the logs. The class is chosen when the group is created and cannot be changed.

True or false

Retention and log classes

Exam skills 1.1.1

Check facts about log retention and log classes.

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

Encryption and access

Log data is always encrypted at rest. To use a customer managed KMS key, associate the key with the log group (associate-kms-key) and allow the Regional CloudWatch Logs service principal (logs.<region>.amazonaws.com) in the key policy, usually with a condition on kms:EncryptionContext:aws:logs:arn. If the key is disabled or the policy is wrong, writes and reads fail. Access to log data is controlled with IAM actions such as logs:GetLogEvents and logs:StartQuery scoped to log group ARNs, and data protection policies can detect and mask sensitive data such as credit card numbers or email addresses in log events, so that only principals with logs:Unmask see the original values.

Put in order

Encrypting a log group

Exam skills 1.1.1 · 4.2.2

Put the steps for encrypting a log group with a customer managed key in order.

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

Controlling log cost

A cost review shows 40 TB of CloudWatch Logs storage, most of it in Lambda and VPC Flow Log groups with no retention set. Compliance requires 90 days of searchable logs and one year of archived flow logs. The team sets 90-day retention on all groups with a script, creates a subscription filter that streams flow logs through Firehose to S3 with a lifecycle rule that transitions to S3 Glacier Flexible Retrieval after 90 days and expires after one year, and deploys a Config rule with automatic remediation so that new log groups receive 90-day retention.

for g in $(aws logs describe-log-groups --query 'logGroups[?retentionInDays==null].logGroupName' --output text); do
  aws logs put-retention-policy --log-group-name "$g" --retention-in-days 90
done
Check yourself

Which service principal must a customer managed key allow so that a log group can use it?

Show answer

logs.region.amazonaws.com, the CloudWatch Logs principal for that Region.

Exam Tips

Remember for the exam

  • Log groups never expire by default; set retention on every group.
  • Infrequent Access log class is cheaper but has no metric or subscription filters.
  • A customer managed key must allow the logs.<region>.amazonaws.com principal.
  • Data protection policies mask sensitive data in log events.