CloudWatch Logs and Logs Insights
Log groups and streams, retention and log classes, structured logging, Logs Insights queries, metric filters, subscriptions and masking.
CloudWatch metrics and alarmsLambda functions and the execution modelECS task definitions and services
CloudWatch Logs and Logs Insights
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Events go into log streams, streams belong to a log group, and retention and access are set on the group.
- Log groups keep data forever by default. Set a retention period on every group.
- Log one JSON object per event, with a request or trace ID, so fields can be filtered and grouped.
- Logs Insights queries are billed by data scanned. Choose the groups and time range narrowly.
- Metric filters turn matching log lines into metrics from the moment they are created, not retroactively.
- The Essentials class costs less to ingest but drops features such as metric filters, and you choose the class when creating the group.
The record of what happened
A metric tells you errors went up at 14:05. It can’t tell you which request failed, or why. That is in the logs, and on AWS the containers and functions that wrote them may no longer exist. CloudWatch Logs receives them as they’re written and keeps them. Collection pipelines in general are covered in log aggregation.
Groups, streams, retention
- A log event is a timestamp and a message.
- A log stream is the sequence from one source, such as one container or one function environment.
- A log group gathers streams that share settings: retention, access, encryption.
Lambda writes to /aws/lambda/<function-name> by default, if its role allows it. ECS tasks use the log driver you configure.
By default a log group never expires. I created one on moto, a local emulator, and its retention came back empty, which is the “forever” setting. Set it explicitly, ideally in your infrastructure code:
aws logs put-retention-policy --log-group-name /shop/api --retention-in-days 30
A log class is chosen when the group is created. Standard has everything. Essentials (renamed from Infrequent Access) has a lower ingestion price and fewer features: no metric filters, and logs are read through Logs Insights.
Write logs a machine can read
{"route":"/checkout","status":502,"ms":3001,"requestId":"r1"}
Logs Insights discovers JSON fields automatically. With free text you would be writing patterns to dig the same values out. Include an ID that follows a request across services, and leave secrets and personal data out.
Logs Insights
A query is a pipeline: each command passes its result to the next.
filter route = "/checkout"
| stats count(*) as requests,
sum(status >= 500) as errors,
pct(ms, 99) as p99
by bin(5m)
That gives requests, server errors and 99th percentile latency per five minutes. For investigating, fields @timestamp, @message | filter status >= 500 | sort @timestamp desc | limit 20 shows the latest failures. parse extracts fields from text, and pattern clusters similar lines, which is a fast way to find the new kind of error. CloudWatch also accepts PPL and SQL.
You pay for data scanned, so narrow the log groups and time window first. Field indexes on things like requestId let equality searches skip data.
Acting on logs as they arrive
A metric filter counts matches and publishes a metric you can alarm on. It starts from when you create it.
A subscription filter streams matching events to Kinesis, Firehose or Lambda, usually within minutes, for a search system or archive. A log group can have up to five, delivery is at least once, and if a destination returns permanent errors, events are skipped while the filter is disabled. Account-level subscriptions cover many groups at once.
One emulator warning. I wrote five JSON events and ran filter-log-events with the pattern { $.status >= 500 }. moto returned all five, including the 200s. Real CloudWatch applies the pattern. Test filter patterns in the console against real lines before you rely on an alarm built on one.
Access and sensitive data
Logs often hold more than intended. Restrict read access per group, and use a data protection policy to mask detected identifiers; seeing the originals then needs logs:Unmask. A central logging account, out of reach of the workloads that write, keeps evidence intact.
Watching live and centralising
During an incident or a deployment, Live Tail streams matching events as they arrive; it is billed by session time, so close it afterwards. In an organisation, centralization rules can copy log data from member accounts and Regions into one account. They apply to new data only, so set them up before you need the history.
About the video
The embed is Be A Better Dev’s “AWS Cloudwatch Logs Core Concepts”, November 2024, 14 minutes, with a sponsor. Its chapters cover service setup, application monitoring, log streams and the CloudWatch agent. Its description says it also covers log groups and Logs Insights. Query syntax, filters and classes are better from this page and the docs. I judged it from chapters and description only, since transcripts couldn’t be fetched. English was judged from the “English (US) original” audio track. A deeper re:Invent session is linked.
What was run: retention and the filter experiment ran on moto 5.1.22, which, as shown, ignores the JSON pattern. The Logs Insights queries were not run; they follow the documented syntax. Other behaviour is from the AWS docs, checked 2026-11-29.
Where this fits
Logs show what one service saw. To follow a single request through many services you need tracing.
Check yourself
You create a metric filter for errors after an outage and the graph shows nothing for the outage. Why?
Metric filters only publish data points for events that arrive after the filter exists. For the past, query the logs with Logs Insights.
Your CloudWatch bill is mostly log ingestion. Does setting retention to 7 days fix it?
No. Retention reduces storage. Ingestion is charged when data arrives, so log less, sample noisy debug output, or use a cheaper log class or destination.
A Lambda function runs but no logs appear. What do you check?
The execution role. It needs permission to create and write to the log group, for example through AWSLambdaBasicExecutionRole, and the function may be configured to write to a different group.
A developer printed customer email addresses into logs. What can limit who sees them from now on?
A data protection policy on the log group masks matching data for everyone without the logs:Unmask permission. Fix the code too, and handle the data already stored according to your obligations.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.