Collecting Logs with CloudWatch Logs and the CloudWatch Agent
This lesson explains how CloudWatch Logs serves as the default home for logs from AWS services, operating systems and applications, and the starting point for many detection and troubleshooting scenarios.
Learning outcomes
After completing this lesson, you will be able to:
- Describe log group retention, encryption and access settings.
- Explain when the CloudWatch agent is needed to collect operating system logs.
- Describe cross-account observability for central monitoring.
- Encrypt a log group with a customer managed key scoped to it.
Log groups
A log group is the container for log streams that share the same retention, encryption and access settings, so security decisions are made at the log group level. The default retention is never to expire, which increases storage cost and keeps sensitive data longer than necessary. Encryption with a customer managed KMS key gives control over who can read the logs, because a principal needs permission on the key as well as on the log group, and the key policy must allow the CloudWatch Logs service principal for the Region. Logs often contain sensitive values, such as email addresses or tokens, which data protection policies can detect and mask. Finally, subscription filters send events onward in near real time for analysis or central storage. The following list describes these settings.
- Set a retention period on each: the default is never expire, increasing cost and exposure.
- Encrypt with a KMS key whose policy allows
logs.REGION.amazonaws.com, ideally scoped by an encryption context condition on the log group ARN. - Data protection policies mask sensitive data.
- Subscription filters stream events in real time to Lambda, Kinesis Data Streams, Firehose or OpenSearch, including a destination in another account.
A key policy that breaks log encryption
The CloudWatch agent
Some security-relevant data is not collected by default. EC2 publishes CPU, network and disk operation metrics, but operating system logs, such as authentication logs and the Windows Security event log, application logs, and memory and disk-space metrics are visible only from inside the instance. The CloudWatch agent runs on EC2 instances and on-premises servers to collect them and send them to CloudWatch. The agent's permissions, configuration and network path are frequent sources of failure. The following list describes what the agent needs.
- An instance profile with CloudWatchAgentServerPolicy.
- A JSON configuration file, usually in Parameter Store and deployed with Systems Manager.
- Network access to CloudWatch endpoints (NAT or interface endpoints in private subnets).
An agent that sends nothing
Central monitoring
In a multi-account organisation, security teams need to search and alarm on logs from every account without signing in to each one. CloudWatch cross-account observability links source accounts to a monitoring account, which can then query logs, metrics and traces from all linked accounts in one place, while the data stays in the source accounts. Alternatively, subscription filters with a cross-account destination copy log events to a central account, which suits long-term retention in a log archive. Many AWS services, including VPC Flow Logs, Route 53 Resolver query logs, Lambda, API Gateway and CloudTrail, can deliver logs to CloudWatch Logs, so it often serves as the common place where security events are searched and correlated.
The following diagram shows the two gates a reader must pass to read a log group encrypted with a customer managed key.

Centralising CloudWatch data
An encrypted log group with a key scoped to it
Application logs may contain customer data, so the log group must be encrypted with a customer managed key that can be used only for that log group. The key policy allows the CloudWatch Logs service principal in this Region, constrained by the encryption context CloudWatch Logs supplies:
{
"Sid": "AllowLogsForOneLogGroup",
"Effect": "Allow",
"Principal": { "Service": "logs.eu-west-1.amazonaws.com" },
"Action": ["kms:Encrypt*", "kms:Decrypt*", "kms:ReEncrypt*", "kms:GenerateDataKey*", "kms:Describe*"],
"Resource": "*",
"Condition": {
"ArnEquals": {
"kms:EncryptionContext:aws:logs:arn": "arn:aws:logs:eu-west-1:111111111111:log-group:/orders/app"
}
}
}aws logs create-log-group --log-group-name /orders/app \
--kms-key-id arn:aws:kms:eu-west-1:111111111111:key/1234abcd-12ab-34cd-56ef-1234567890ab
aws logs put-retention-policy --log-group-name /orders/app --retention-in-days 90Readers of the log group now need both logs:GetLogEvents and permission to use the key, a second gate on sensitive logs. The retention policy prevents the default of keeping logs forever.
How long does CloudWatch Logs keep log data by default?
Show answer
For ever, so retention should be set on every log group.
Exam Tips
Remember for the exam
- Logs are kept for ever by default, so set retention on every log group.
- If the agent is not sending logs, check the role policy, the configuration file and access to the endpoints.
- For a central monitoring account, use cross-account observability or subscription filters to a cross-account destination.
- For a KMS-encrypted log group, the key policy must allow the CloudWatch Logs service principal.
- To stream logs to a SIEM in real time, use a subscription filter.