Lesson 1 of 6 CloudWatch Logs and Log Analytics

Storing and Securing Logs in Amazon CloudWatch Logs

Logs are evidence: they explain incidents, support audits and feed security detection. They must therefore be collected reliably, kept for the right time, protected from tampering and disclosure, and stored at reasonable cost. This lesson explains the CloudWatch Logs data model, retention and log classes, KMS encryption, data protection policies, access control and exports, and central log account designs.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe log groups, log streams and log events and how AWS services and agents deliver logs.
  • Explain retention settings and the Standard and Infrequent Access log classes.
  • Explain how log groups are encrypted with AWS KMS and how sensitive data is masked with data protection policies.
  • Describe access control, export and centralisation options for logs across accounts.

The Data Model

CloudWatch Logs stores log events, each a timestamp and a message, in log streams, which are sequences of events from one source such as an instance, a container or a Lambda execution environment. Streams belong to log groups, which hold settings for retention, encryption, data protection, metric filters and subscriptions. Logs arrive from many sources: Lambda and ECS (awslogs) automatically, the CloudWatch agent from servers, AWS services such as API Gateway, VPC Flow Logs, Route 53 Resolver and CloudTrail, and applications through the PutLogEvents API. Live Tail shows events as they arrive, which is useful during deployments and debugging.

Retention and Log Classes

New log groups keep data forever by default, which steadily increases storage cost. Retention can be set per log group from one day to ten years, after which events are deleted automatically. Retention should follow a policy, for example 30 days for debug logs and one year for audit logs, and should be enforced: an AWS Config rule such as cw-loggroup-retention-period-check detects groups with too short or no retention, and an automated remediation or an EventBridge rule on CreateLogGroup can apply the standard retention to new groups. Longer-term retention is usually cheaper in S3, as described later in this module.

Log groups use one of two log classes, chosen at creation. The Standard class supports every feature, including metric filters, subscription filters, Live Tail and anomaly detection. The Infrequent Access class has a lower ingestion price and supports Logs Insights queries, encryption and export, but not features such as metric filters, subscription filters or Live Tail, which suits logs kept mainly for occasional investigation or compliance.

Standard
Ingestion priceHigher
Logs InsightsYes
Metric filters, subscriptions, Live TailYes
Choose when Logs used for alerting, streaming and real-time analysis.
Infrequent Access
Ingestion priceLower
Logs InsightsYes
Metric filters, subscriptions, Live TailNo
Choose when Logs kept for occasional queries and compliance.
True or false

Log storage facts

Exam skills 4.1.1 · 4.1.5 · 4.1.8

Check four facts about retention, log classes and encryption.

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

Encrypting Logs with AWS KMS

CloudWatch Logs always encrypts data at rest. To use a customer managed KMS key, for control over key policy, rotation and the ability to revoke access, the key is associated with the log group, either at creation or later with AssociateKmsKey. Only events ingested after association are encrypted with the key.

The key policy must allow the CloudWatch Logs service principal for the Region, logs.<region>.amazonaws.com, to use the key, typically with a condition on the encryption context kms:EncryptionContext:aws:logs:arn matching the log group ARN, so the key can only be used for the intended log groups. If the key is disabled or the policy no longer allows CloudWatch Logs, the data cannot be read or written. Users who query the logs also need decrypt permission through the service.

{
  "Sid": "AllowCloudWatchLogs",
  "Effect": "Allow",
  "Principal": {"Service": "logs.eu-west-1.amazonaws.com"},
  "Action": ["kms:Encrypt*", "kms:Decrypt*", "kms:ReEncrypt*", "kms:GenerateDataKey*", "kms:Describe*"],
  "Resource": "*",
  "Condition": {"ArnLike": {"kms:EncryptionContext:aws:logs:arn":
                            "arn:aws:logs:eu-west-1:111122223333:log-group:/orders/*"}}
}

Masking Sensitive Data and Controlling Access

Applications sometimes log sensitive data such as email addresses, card numbers or credentials. A data protection policy on a log group, or at account level, uses managed data identifiers (for example for credentials, financial data and personal information) and custom identifiers to detect sensitive data on ingestion and mask it, so that users see masked values unless they have the logs:Unmask permission. Findings can be reported to CloudWatch Logs, S3 or Firehose for auditing.

Access to logs is controlled with IAM policies on log group ARNs, so teams can read only their own log groups, and with resource policies that let AWS services write to log groups. Log groups can carry tags for ABAC. Because logs are evidence, deletion of log groups should be restricted, for example with SCPs denying logs:DeleteLogGroup for security log groups.

Exporting and Centralising Logs

Logs can leave a log group in several ways. Export tasks (CreateExportTask) copy a time range of a log group to an S3 bucket in a batch; they are not real time, and only one export task per account can run at a time, so they suit occasional archiving. Subscription filters, described in a later lesson, stream logs in near real time. For multi-account environments, logs are centralised in a log archive or observability account by cross-account observability (viewing and querying source accounts' logs from a monitoring account), by subscription filters to a cross-account destination, or by CloudWatch Logs centralisation rules that copy log data from accounts and Regions in an organisation into a central account. Many AWS services can also deliver logs directly to S3 or Firehose in a central account.

Scenario questions

Protect and move logs

Exam skills 4.1.1 · 4.1.6

Practise protecting and centralising CloudWatch Logs data.

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

A Log Retention and Protection Standard

An auditor finds that application log groups keep data indefinitely, some contain customer email addresses, and logs are encrypted only with AWS-owned keys. The company must keep application logs for 90 days in CloudWatch and one year in total, encrypt them with keys it controls and hide personal data from most engineers.

  1. Retention

    Set 90-day retention on all application log groups, enforced by a Config rule with automatic remediation and applied to new groups by an EventBridge-triggered function.

  2. Archive

    Stream logs with a subscription filter to Firehose and S3, where a lifecycle rule keeps them for a year.

  3. Encryption

    Create a customer managed key per environment with a key policy scoped by encryption context, and associate it with the log groups.

  4. Privacy

    Apply an account-level data protection policy that masks email addresses; grant logs:Unmask only to the incident response role.

Check yourself

After associating a customer managed key with a log group, CloudWatch Logs reports access denied when writing events. What is the likely cause?

Show answer

The key policy does not allow the CloudWatch Logs service principal (logs.<region>.amazonaws.com) to use the key, or its encryption context condition does not match the log group ARN.

Exam Tips

Remember for the exam

  • Log groups retain data forever by default; set retention (1 day to 10 years) and enforce it with Config rules or automation.
  • Infrequent Access log class is cheaper but lacks metric filters, subscription filters and Live Tail.
  • KMS encryption of a log group requires the key policy to allow logs.<region>.amazonaws.com, usually with an encryption context condition on the log group ARN; only new events are encrypted.
  • Data protection policies mask sensitive data; logs:Unmask reveals it.
  • Export tasks are batch and one at a time; use subscription filters or centralisation for near-real-time central logging.