SkillSprout
Chapter 10 · Lesson 1
Intermediate
7 min read

CloudWatch

Overview

Amazon CloudWatch is the monitoring and observability service of AWS. It collects metrics (numbers over time, such as CPU utilisation), logs (text output from applications and services), and events, then lets you visualise them on dashboards, set alarms, and trigger automated actions.

Imagine the dashboard of a car: speed, fuel and engine temperature are metrics, the warning light is an alarm, and the black-box recorder is the log. CloudWatch plays all three roles for your cloud resources.

Almost every AWS service publishes metrics to CloudWatch automatically and for free at a basic level. For the SAA-C03 exam you must know what is and is not collected by default (for example, EC2 memory and disk usage are not), how alarms work, and how logs are stored and analysed.

Key Concepts

  • A metric is a time-ordered set of data points identified by namespace, name and dimensions; metrics are regional.
  • EC2 basic monitoring = 5-minute data (free); detailed monitoring = 1-minute data (extra cost); high-resolution custom metrics go down to 1 second.
  • EC2 does not report memory or disk space by default; install the CloudWatch unified agent to collect them.
  • Alarms have states OK, ALARM and INSUFFICIENT_DATA, and can notify SNS, trigger Auto Scaling, or perform EC2 actions (stop, terminate, reboot, recover).
  • CloudWatch Logs organises data into log groups and log streams; retention is configurable (1 day to 10 years, or never expire).
  • Metric filters turn log patterns into metrics; Logs Insights queries logs; subscription filters stream logs to Lambda, Kinesis or Firehose.
  • Composite alarms combine alarms with AND/OR to reduce noise; anomaly detection uses machine learning bands.
  • Dashboards are global and can show metrics from multiple Regions and accounts.

Detailed Notes

Metrics

Metrics are stored by namespace (e.g. AWS/EC2), metric name and dimensions (e.g. InstanceId). Data points are kept at decreasing resolution:

ResolutionRetention
< 60 seconds (high-resolution)3 hours
1 minute15 days
5 minutes63 days
1 hour455 days (15 months)

You can publish custom metrics with PutMetricData or through the agent. Standard-resolution custom metrics are 1-minute granularity; high resolution is 1 second.

What EC2 sends by default

CPU, network, disk I/O (instance store) and status checks, all visible to the hypervisor. Memory, swap, disk space and processes are visible only inside the OS, so you need the unified CloudWatch agent, which also ships log files.

Alarms

An alarm watches a single metric (or a math expression) over N evaluation periods and compares it with a threshold.

bash
aws cloudwatch put-metric-alarm --alarm-name high-cpu \
  --namespace AWS/EC2 --metric-name CPUUtilization \
  --dimensions Name=InstanceId,Value=i-0abc123 \
  --statistic Average --period 300 --evaluation-periods 2 \
  --threshold 80 --comparison-operator GreaterThanThreshold \
  --alarm-actions arn:aws:sns:us-east-1:111122223333:ops-alerts

Alarm actions: SNS notification, Auto Scaling policy, EC2 action (stop / terminate / reboot / recover an instance after a system status check failure), Systems Manager OpsItem or incident. Composite alarms reduce noise by evaluating the state of other alarms. Alarms are evaluated in the Region where they are created, but cross-account alarms are supported.

CloudWatch Logs

  • Hierarchy: log group (retention, KMS encryption, access) -> log stream (a sequence from one source).
  • Sources: unified agent, Lambda (automatic), ECS/EKS, VPC Flow Logs, Route 53 query logs, CloudTrail, API Gateway, and more.
  • Retention defaults to never expire, so set a policy to control cost.
  • Metric filters convert patterns (e.g. ERROR) into metrics that can drive alarms.
  • Logs Insights is an interactive query language for ad-hoc analysis.
  • Subscription filters deliver a real-time stream to Lambda, Kinesis Data Streams or Firehose (for cross-account aggregation or OpenSearch).
  • Export to S3 (CreateExportTask, up to 12 hours delay) is for batch archiving; for near real-time use a subscription filter with Firehose.
text
fields @timestamp, @message
| filter @message like /ERROR/
| stats count(*) as errors by bin(5m)
| sort errors desc

Other CloudWatch capabilities

FeaturePurpose
Synthetics (canaries)Scripted probes that test endpoints and APIs on a schedule
Container Insights / Lambda InsightsMetrics and logs for ECS, EKS, Lambda
Application Signals / X-RayApplication performance and distributed tracing
Contributor InsightsTop-N contributors to a log metric (e.g. noisy IPs)
Internet Monitor / RUMInternet and real-user experience
Cross-account observabilityMonitoring account sees source accounts' data

CloudWatch vs. CloudTrail vs. Config

  • CloudWatch = performance and operational data (what is happening now).
  • CloudTrail = who called which API (audit).
  • Config = how resources are configured and how that changed (compliance).

Cost and design tips

Use metric math to avoid extra custom metrics, set log retention, and prefer EventBridge for reacting to state changes and CloudWatch alarms for sustained thresholds.

Real World Example

Scenario — a SaaS company stabilises its checkout service.

The checkout API runs on an Auto Scaling group behind an ALB. Customers reported slowness, yet CPU looked healthy. The team installs the CloudWatch unified agent through a Systems Manager association to collect memory and disk usage and the application log files. A memory leak is revealed on dashboards.

They add three controls:

  • A metric filter on the application log counting PaymentTimeout and an alarm that pages the on-call engineer through SNS when more than 10 occur in 5 minutes.
  • A target tracking scaling policy on ALBRequestCountPerTarget, plus an alarm on HTTPCode_Target_5XX_Count.
  • A Synthetics canary that runs a full checkout flow every minute from several Regions.

Logs are retained for 30 days in CloudWatch, then exported to S3 and Glacier for seven-year compliance. A single dashboard shared with business teams shows latency, errors and orders per minute.

Architecture Diagram

CloudWatch collecting, alarming and reacting
Architecture
AWS Cloud
EC2 + agentmemory, disk, logs
Lambdalogs + metrics
RDSmetrics
CloudWatch
Metrics and Logslog groups, filters
AlarmOK / ALARM
SNSnotify on-call
Auto Scalingscale out
Remediation Lambda

Metrics and logs flow in; alarms notify people or trigger automated actions.

  1. 1Resources publish metrics automatically; the unified agent adds memory, disk and log files.
  2. 2CloudWatch stores metrics and log groups, and metric filters turn log patterns into metrics.
  3. 3An alarm evaluates a metric against a threshold over several periods and changes state to ALARM.
  4. 4The alarm notifies SNS, triggers an Auto Scaling policy or invokes automated remediation.

Best Practices

  • ✓Install the unified CloudWatch agent on EC2/on-premises hosts to collect memory, disk and application logs; deploy it with Systems Manager.
  • ✓Alarm on symptoms users feel (latency, 5xx errors, queue age) in addition to resource metrics like CPU.
  • ✓Set log retention on every log group; the default is never expire.
  • ✓Use composite alarms and anomaly detection to cut alert noise.
  • ✓Send alarm actions to SNS and integrate with paging tools; use the EC2 recover action for status-check failures.
  • ✓Centralise logs and metrics from many accounts with cross-account observability and subscription filters.
  • ✓Encrypt log groups with KMS and restrict access via IAM.
  • ✓Treat dashboards and alarms as code (CloudFormation/CDK) for consistency.

Common Interview Questions

Exam Tips

SAA-C03 focus

  • Memory / disk space / swap of EC2 -> CloudWatch agent (custom metrics). Default EC2 metrics don't include them.
  • Basic EC2 monitoring 5 min, detailed 1 min, high-resolution custom metric 1 s.
  • CloudWatch = performance and logs; CloudTrail = API audit; Config = configuration history. Distinguish them.
  • Alarm states: OK, ALARM, INSUFFICIENT_DATA. Alarm actions: SNS, Auto Scaling, EC2 actions.
  • Auto-recover an instance on system status check failure -> CloudWatch alarm with the recover action.
  • Log groups never expire by default; use retention policies. Real-time log processing -> subscription filters.
  • Ad-hoc log analysis -> Logs Insights. Count log patterns -> metric filter.
  • Scheduled uptime checks of an endpoint or user journey -> CloudWatch Synthetics canaries.

Finished this lesson?

0 of 64 lessons done · 0% of the course