Amazon CloudWatch Monitoring
When to Use
- Setting up metrics, alarms, and dashboards for AWS services
- Building custom metrics from application logs using metric filters
- Implementing distributed tracing with AWS X-Ray
- Creating synthetic monitors with CloudWatch Synthetics
- Designing event-driven operations with CloudWatch Alarms + SNS/Lambda
- Preparing for AWS DEA-C01, DVA-C02, or DOP-C02 exams
Core Jobs
1. CloudWatch Metrics
| Metric Type |
Cost |
Resolution |
Examples |
| Standard metrics |
Free |
1-minute minimum |
EC2 CPU, RDS connections, Lambda invocations |
| Detailed monitoring |
Paid |
1-minute (EC2) |
EC2 per-instance metrics at 1-minute granularity |
| Custom metrics |
Paid |
1-second (high-resolution) to 1-minute |
Application-specific (order count, queue depth) |
| Embedded metrics format |
Paid |
1-second possible |
Log structured metrics extracted automatically |
Key concepts:
- Namespace: logical container for metrics (e.g.,
AWS/EC2, MyApp/Orders)
- Dimensions: key-value pairs that identify the specific metric source (InstanceId, FunctionName)
- Statistics: Average, Sum, Minimum, Maximum, SampleCount, Percentile (p99, p95)
- Period: aggregation window (60s, 300s, 3600s)
- Metrics retained: 1-second → 3 hours; 1-minute → 15 days; 5-minute → 63 days; 1-hour → 15 months
2. CloudWatch Logs
Structure:
- Log group → Log streams → log events
- Log group: named container (e.g.,
/aws/lambda/my-function)
- Log stream: sequence of events from a single source (e.g., one Lambda instance)
- Retention: 1 day to 10 years (default: never expire — must set retention policy)
Metric filters:
- Extract metrics from log content using filter patterns
- Pattern syntax:
[ERROR], {$.statusCode = 500} (JSON), "Exception"
- Create CloudWatch metric from filter → alarm → SNS/Lambda pipeline
CloudWatch Logs Insights:
- Interactive SQL-like query language for log analysis
- Cross-log-group queries
- Key commands:
fields, filter, stats, sort, limit, parse
- Pre-saved queries available for AWS service logs (Lambda, VPC Flow Logs, CloudTrail)
3. CloudWatch Alarms
| Alarm Type |
Threshold Definition |
Best For |
| Static threshold |
Fixed value (> 80%) |
Predictable thresholds (CPU, queue depth) |
| Anomaly detection |
ML-based band (± N std deviations) |
Variable metrics without fixed expected value |
| Composite alarms |
AND/OR combination of multiple alarms |
Reduce alert fatigue; only alert when multiple signals |
| Metric math alarms |
Alarm on derived metric expressions |
Custom ratios, rates, combined metrics |
Alarm states: OK → ALARM → INSUFFICIENT_DATA (not enough data points)
Actions:
- Auto Scaling: scale-out/in policies
- SNS: notify email/SMS/HTTP endpoint
- EC2: stop, terminate, reboot, recover
- Systems Manager OpsItem: create incident ticket
- Lambda: via SNS subscription
Composite alarms example (reduce noise):
ALARM("CPUHigh") AND ALARM("MemoryHigh") → PagerDuty alert
ALARM("CPUHigh") alone → Slack notification only
4. CloudWatch Logs Insights Queries
Common patterns for exam and real-world use:
# Find error count per Lambda function
fields @timestamp, @message
| filter @message like /ERROR/
| stats count() as errorCount by bin(1h)
| sort @timestamp desc
# Top 10 slowest requests
fields @timestamp, @duration
| sort @duration desc
| limit 10
# Parse custom log format
parse @message "* * * *" as requestId, statusCode, latency, path
| filter statusCode = "500"
5. AWS X-Ray Distributed Tracing
- Traces requests across microservices, Lambda functions, and AWS services
- Trace: end-to-end request lifecycle (collection of segments)
- Segment: one service's contribution to the trace (with subsegments)
- Subsegment: granular operations within a segment (DB call, external HTTP call)
- Service map: visual topology of services and their dependencies with latency/error rates
Sampling rules:
- Default: 5% of requests + 1 request/second minimum (avoid high-volume trace costs)
- Custom sampling rules: define by service name, URL path, host, HTTP method
- Reservoir = guaranteed fixed-rate; rate = percentage of remaining traffic
X-Ray SDK integration:
- Instrument AWS SDK calls automatically (DynamoDB, S3, SQS, etc.)
- Annotate with custom key-value pairs (for filtering traces)
- Add metadata for debugging (not indexed, not searchable)
X-Ray Daemon: runs as sidecar collecting segments and sending to X-Ray service (batch UDP).
6. CloudWatch Synthetics (Canaries)
- Node.js or Python scripts that simulate user interactions
- Run on schedule (e.g., every 5 minutes) or one-time
- Check: API availability, UI flows, broken links, visual regression
- Canary blueprints: API canary, heartbeat monitor, broken link checker, visual monitoring
- Results stored in S3; CloudWatch metrics generated per canary run
7. Container and Lambda Insights
| Feature |
For |
What It Adds |
| Container Insights |
EKS, ECS |
CPU, memory, network per pod/task; Kubernetes events |
| Lambda Insights |
Lambda |
Cold start time, memory used, init duration, extension overhead |
| Application Signals |
Applications |
SLI/SLO tracking; request success rate, latency, volume |
Key Concepts
- CloudWatch Agent — installed on EC2/on-prem to collect OS-level metrics (memory, disk) and logs (not available by default)
- Embedded Metrics Format (EMF) — structured JSON logs with
_aws metadata; CloudWatch extracts metrics automatically; no separate PutMetricData call
- Metric math — perform arithmetic on metrics (e.g., ErrorRate = Errors / Invocations × 100)
- CloudWatch Contributor Insights — analyze log data to identify top contributors (e.g., top 10 IPs causing 4xx errors)
- CloudWatch Evidently — A/B testing and feature flagging (launch features to % of users)
- CloudWatch RUM — Real User Monitoring for web applications (client-side performance)
Checklist
Output Format
- 🔴 Critical — no alarms on critical service metrics; log groups with "never expire" retention accumulating indefinitely; X-Ray not enabled for production microservices
- 🟡 Warning — memory/disk metrics missing (CloudWatch Agent not installed); composite alarms not used (too many individual alerts); default X-Ray sampling rate too high (100% on high-volume service)
- 🟢 Suggestion — Anomaly detection alarms for variable metrics; CloudWatch Logs Insights for ad-hoc log analysis; Lambda Insights for cold start investigation
Exam Tips
- Custom metrics = 1-second resolution possible (high-resolution); standard metrics = 1-minute minimum
- Metric filters on log groups → CloudWatch metric → alarm → SNS → Lambda — classic event-driven ops pipeline; memorize this chain
- CloudWatch Logs Insights = SQL-like queries on logs; fast cross-log-group analysis without Athena
- X-Ray sampling = reduces trace volume; default 5% + 1 req/sec minimum; can configure per path/service
- Composite alarms = combine multiple alarms with AND/OR logic; reduces alert fatigue (only page when CPU AND memory high)
- Container Insights = ECS/EKS metrics (not enabled by default — must enable); Lambda Insights = Lambda performance metrics (cold starts, memory usage)
- CloudWatch Agent required for EC2 memory and disk metrics — these are NOT available without the agent
- Anomaly detection = ML baseline based on historical data; alarms fire when metric deviates beyond expected band
1---2name: cloudwatch-monitoring3description: Use when setting up AWS observability with CloudWatch metrics, logs, alarms, dashboards, X-Ray tracing, or CloudWatch Synthetics canaries. Covers monitoring domains across DEA-C01, DVA-C02, and DOP-C02 exams.4---56# Amazon CloudWatch Monitoring78## When to Use9- Setting up metrics, alarms, and dashboards for AWS services10- Building custom metrics from application logs using metric filters11- Implementing distributed tracing with AWS X-Ray12- Creating synthetic monitors with CloudWatch Synthetics13- Designing event-driven operations with CloudWatch Alarms + SNS/Lambda14- Preparing for AWS DEA-C01, DVA-C02, or DOP-C02 exams1516## Core Jobs1718### 1. CloudWatch Metrics1920| Metric Type | Cost | Resolution | Examples |21|-------------|------|-----------|---------|22| **Standard metrics** | Free | 1-minute minimum | EC2 CPU, RDS connections, Lambda invocations |23| **Detailed monitoring** | Paid | 1-minute (EC2) | EC2 per-instance metrics at 1-minute granularity |24| **Custom metrics** | Paid | 1-second (high-resolution) to 1-minute | Application-specific (order count, queue depth) |25| **Embedded metrics format** | Paid | 1-second possible | Log structured metrics extracted automatically |2627**Key concepts**:28- **Namespace**: logical container for metrics (e.g., `AWS/EC2`, `MyApp/Orders`)29- **Dimensions**: key-value pairs that identify the specific metric source (InstanceId, FunctionName)30- **Statistics**: Average, Sum, Minimum, Maximum, SampleCount, Percentile (p99, p95)31- **Period**: aggregation window (60s, 300s, 3600s)32- Metrics retained: 1-second → 3 hours; 1-minute → 15 days; 5-minute → 63 days; 1-hour → 15 months3334### 2. CloudWatch Logs3536**Structure**:37- **Log group** → **Log streams** → log events38- Log group: named container (e.g., `/aws/lambda/my-function`)39- Log stream: sequence of events from a single source (e.g., one Lambda instance)40- Retention: 1 day to 10 years (default: never expire — must set retention policy)4142**Metric filters**:43- Extract metrics from log content using filter patterns44- Pattern syntax: `[ERROR]`, `{$.statusCode = 500}` (JSON), `"Exception"`45- Create CloudWatch metric from filter → alarm → SNS/Lambda pipeline4647**CloudWatch Logs Insights**:48- Interactive SQL-like query language for log analysis49- Cross-log-group queries50- Key commands: `fields`, `filter`, `stats`, `sort`, `limit`, `parse`51- Pre-saved queries available for AWS service logs (Lambda, VPC Flow Logs, CloudTrail)5253### 3. CloudWatch Alarms5455| Alarm Type | Threshold Definition | Best For |56|------------|---------------------|---------|57| **Static threshold** | Fixed value (> 80%) | Predictable thresholds (CPU, queue depth) |58| **Anomaly detection** | ML-based band (± N std deviations) | Variable metrics without fixed expected value |59| **Composite alarms** | AND/OR combination of multiple alarms | Reduce alert fatigue; only alert when multiple signals |60| **Metric math alarms** | Alarm on derived metric expressions | Custom ratios, rates, combined metrics |6162**Alarm states**: OK → ALARM → INSUFFICIENT_DATA (not enough data points)6364**Actions**:65- Auto Scaling: scale-out/in policies66- SNS: notify email/SMS/HTTP endpoint67- EC2: stop, terminate, reboot, recover68- Systems Manager OpsItem: create incident ticket69- Lambda: via SNS subscription7071**Composite alarms example** (reduce noise):72```73ALARM("CPUHigh") AND ALARM("MemoryHigh") → PagerDuty alert74ALARM("CPUHigh") alone → Slack notification only75```7677### 4. CloudWatch Logs Insights Queries7879Common patterns for exam and real-world use:8081```82# Find error count per Lambda function83fields @timestamp, @message84| filter @message like /ERROR/85| stats count() as errorCount by bin(1h)86| sort @timestamp desc8788# Top 10 slowest requests89fields @timestamp, @duration90| sort @duration desc91| limit 109293# Parse custom log format94parse @message "* * * *" as requestId, statusCode, latency, path95| filter statusCode = "500"96```9798### 5. AWS X-Ray Distributed Tracing99100- Traces requests across microservices, Lambda functions, and AWS services101- **Trace**: end-to-end request lifecycle (collection of segments)102- **Segment**: one service's contribution to the trace (with subsegments)103- **Subsegment**: granular operations within a segment (DB call, external HTTP call)104- **Service map**: visual topology of services and their dependencies with latency/error rates105106**Sampling rules**:107- Default: 5% of requests + 1 request/second minimum (avoid high-volume trace costs)108- Custom sampling rules: define by service name, URL path, host, HTTP method109- Reservoir = guaranteed fixed-rate; rate = percentage of remaining traffic110111**X-Ray SDK integration**:112- Instrument AWS SDK calls automatically (DynamoDB, S3, SQS, etc.)113- Annotate with custom key-value pairs (for filtering traces)114- Add metadata for debugging (not indexed, not searchable)115116**X-Ray Daemon**: runs as sidecar collecting segments and sending to X-Ray service (batch UDP).117118### 6. CloudWatch Synthetics (Canaries)119120- Node.js or Python scripts that simulate user interactions121- Run on schedule (e.g., every 5 minutes) or one-time122- Check: API availability, UI flows, broken links, visual regression123- Canary blueprints: API canary, heartbeat monitor, broken link checker, visual monitoring124- Results stored in S3; CloudWatch metrics generated per canary run125126### 7. Container and Lambda Insights127128| Feature | For | What It Adds |129|---------|-----|-------------|130| **Container Insights** | EKS, ECS | CPU, memory, network per pod/task; Kubernetes events |131| **Lambda Insights** | Lambda | Cold start time, memory used, init duration, extension overhead |132| **Application Signals** | Applications | SLI/SLO tracking; request success rate, latency, volume |133134## Key Concepts135136- **CloudWatch Agent** — installed on EC2/on-prem to collect OS-level metrics (memory, disk) and logs (not available by default)137- **Embedded Metrics Format (EMF)** — structured JSON logs with `_aws` metadata; CloudWatch extracts metrics automatically; no separate PutMetricData call138- **Metric math** — perform arithmetic on metrics (e.g., ErrorRate = Errors / Invocations × 100)139- **CloudWatch Contributor Insights** — analyze log data to identify top contributors (e.g., top 10 IPs causing 4xx errors)140- **CloudWatch Evidently** — A/B testing and feature flagging (launch features to % of users)141- **CloudWatch RUM** — Real User Monitoring for web applications (client-side performance)142143## Checklist144145- [ ] CloudWatch Agent installed for OS-level metrics (memory, disk — not included by default)?146- [ ] Log group retention policy set (not "never expire")?147- [ ] Metric filters created for critical error patterns in application logs?148- [ ] Composite alarms used to reduce alert fatigue (alert only when multiple signals fire)?149- [ ] X-Ray tracing enabled for Lambda functions and API Gateway?150- [ ] Custom sampling rules defined for X-Ray (avoid tracing 100% of high-volume requests)?151- [ ] Container Insights enabled for EKS/ECS clusters?152- [ ] Synthetics canaries monitoring critical API endpoints and user flows?153154## Output Format155156- 🔴 **Critical** — no alarms on critical service metrics; log groups with "never expire" retention accumulating indefinitely; X-Ray not enabled for production microservices157- 🟡 **Warning** — memory/disk metrics missing (CloudWatch Agent not installed); composite alarms not used (too many individual alerts); default X-Ray sampling rate too high (100% on high-volume service)158- 🟢 **Suggestion** — Anomaly detection alarms for variable metrics; CloudWatch Logs Insights for ad-hoc log analysis; Lambda Insights for cold start investigation159160## Exam Tips161162- **Custom metrics = 1-second resolution possible** (high-resolution); standard metrics = 1-minute minimum163- **Metric filters on log groups → CloudWatch metric → alarm → SNS → Lambda** — classic event-driven ops pipeline; memorize this chain164- **CloudWatch Logs Insights** = SQL-like queries on logs; fast cross-log-group analysis without Athena165- **X-Ray sampling** = reduces trace volume; default 5% + 1 req/sec minimum; can configure per path/service166- **Composite alarms** = combine multiple alarms with AND/OR logic; reduces alert fatigue (only page when CPU AND memory high)167- **Container Insights** = ECS/EKS metrics (not enabled by default — must enable); **Lambda Insights** = Lambda performance metrics (cold starts, memory usage)168- **CloudWatch Agent required** for EC2 memory and disk metrics — these are NOT available without the agent169- **Anomaly detection** = ML baseline based on historical data; alarms fire when metric deviates beyond expected band