CloudWatch
CloudWatch is AWS’s monitoring service: it collects metrics and logs, and alarms when something goes wrong.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 29, 2026
Related tutorials
Learning objectives#
- Read service metrics
- Search logs with Logs Insights
- Create alarms with SNS notifications
- Publish custom metrics
Explanation#
Most services publish metrics automatically: EC2 CPU, ALB request count and 5xx errors, Lambda duration and errors, RDS connections.
Logs are organised into log groups (one per app or function) and streams. Set a retention period — the default is to keep logs forever.
Logs Insights queries logs with a purpose-built query language.
Alarms watch a metric and notify an SNS topic (email, SMS, chat) or trigger scaling actions.
Code example: Alarm on Lambda errors + a Logs Insights query#
aws sns create-topic --name oncall
aws sns subscribe --topic-arn arn:aws:sns:ap-south-1:123456789012:oncall --protocol email --notification-endpoint oncall@example.com
aws cloudwatch put-metric-alarm --alarm-name orders-errors \
--namespace AWS/Lambda --metric-name Errors --dimensions Name=FunctionName,Value=orders \
--statistic Sum --period 300 --evaluation-periods 1 --threshold 5 \
--comparison-operator GreaterThanOrEqualToThreshold \
--alarm-actions arn:aws:sns:ap-south-1:123456789012:oncall
aws logs put-retention-policy --log-group-name /aws/lambda/orders --retention-in-days 30
# Logs Insights:
# fields @timestamp, @message
# | filter @message like /ERROR/
# | sort @timestamp desc
# | limit 20
When the orders function has 5 or more errors in 5 minutes, on-call receives an email. Logs are kept for 30 days.
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- SNS delivers the notification by email.
- The alarm sums Lambda errors over 5-minute periods.
- Retention prevents logs from piling up cost forever.
- The Logs Insights query lists the latest errors.
Real-world use case#
During a sale, an alarm on ALB 5xx errors pages the team, and Logs Insights shows the failing request within seconds.
Common mistakes#
Logs default to never expire, so storage cost grows silently.
Log JSON so you can filter by fields like orderId or userId.
Best practices#
- Set log retention on every log group
- Log structured JSON
- Alarm on user-facing symptoms (errors, latency)
- Build a dashboard per service
Remember this#
Metrics = numbers. Logs = events. Alarms = act on metrics.
Set log retention!
Alarm → SNS → your phone or inbox.
Practice exercise#
Your task: Write a Logs Insights query that counts errors per 5 minutes.
Show solution
filter @message like /ERROR/
| stats count(*) as errors by bin(5m)
Mini quiz#
What is the default retention for a new CloudWatch log group?
Want more? Practise all AWS MCQs · AWS interview questions
My notes#
Saved in this browser.