Mastering Amazon CloudWatch: Custom Metrics, Filters, and Automated Response
Creating CloudWatch custom metrics and metric filters, alarms, and notifications (for example, Amazon SNS, Lambda)
Mastering Amazon CloudWatch: Custom Metrics, Filters, and Automated Response
This guide covers the core monitoring and automation capabilities within Amazon CloudWatch, specifically focusing on custom metrics, log-to-metric transformations, and the event-driven notification ecosystem essential for the AWS Certified DevOps Engineer Professional exam.
Learning Objectives
After studying this guide, you should be able to:
- Create and Publish custom metrics using the AWS CLI, SDKs, and the CloudWatch Agent.
- Configure Metric Filters to extract actionable data from CloudWatch Logs.
- Design Alarms with appropriate thresholds, evaluation periods, and datapoints-to-alarm settings.
- Automate Remediations by integrating CloudWatch Alarms with SNS, Lambda, and Systems Manager.
Key Terms & Glossary
- Namespace: A container for CloudWatch metrics. AWS services use
AWS/service(e.g.,AWS/EC2), while custom metrics require a user-defined namespace. - Dimension: A name/value pair (e.g.,
InstanceId=i-12345) that uniquely identifies a metric. Adding a dimension creates a new variation (time-series) of the metric. - Resolution: The granularity of data. Standard Resolution is 1-minute; High Resolution allows for 1-second data points.
- Metric Filter: A set of rules applied to Log Groups to search for patterns and turn log data into numeric metrics.
- Statistic: Aggregations of metric data over a period (e.g., Sum, Average, Minimum, Maximum, SampleCount, or Percentiles).
The "Big Idea"
Amazon CloudWatch is not just a dashboard; it is the central nervous system of AWS observability. It acts as a metrics repository that connects three distinct phases: Collection (metrics/logs), Observation (alarms/dashboards), and Action (SNS/Lambda/Auto Scaling). In a DevOps context, the goal is to minimize "Mean Time to Resolution" (MTTR) by automating the transition from a detected threshold breach to a programmed remediation.
Formula / Concept Box
| Concept | Rule / Formula | Notes |
|---|---|---|
| Alarm Evaluation | Datapoints to Alarm / Evaluation Periods | e.g., 3 out of 5 periods must exceed threshold. |
| Metric Math | SUM(m1, m2) or m1/m2*100 | Used to create "derived" metrics for dashboards/alarms. |
| Standard Resolution | 1 Minute | Default for most services. |
| High Resolution | 1 Second | Requires StorageResolution: 1 in PutMetricData (extra cost). |
| Max Dimensions | 10 per metric | Uniquely identifies the data stream. |
Hierarchical Outline
- Metric Collection
- Standard Metrics: Automatically provided (CPU, Disk I/O, Network).
- Custom Metrics: Published via
PutMetricDataAPI. - CloudWatch Agent: Essential for OS-level metrics (Memory, Disk Space usage).
- CloudWatch Logs & Metric Filters
- Pattern Matching: Case-sensitive string matching or JSON property filtering.
- Transformation: Assigning a numeric value (e.g., "1") to every match found in logs.
- Alarm Configuration
- Static Thresholds: Manual numeric limits.
- Anomaly Detection: Machine learning based on historical trends.
- Alarm States:
OK,ALARM,INSUFFICIENT_DATA.
- Notifications & Actions
- Amazon SNS: Email, SMS, or triggering HTTPS endpoints.
- AWS Lambda: Custom code for automated remediation (e.g., restarting a service).
- Systems Manager: Triggering Automation documents or OpsItems.
Visual Anchors
The Monitoring Workflow
Metric Threshold Visualization
Definition-Example Pairs
- Term: Metric Filter
- Definition: A filter pattern that searches for terms in log events and turns them into metrics.
- Example: Searching for the string
"ERROR"in an NGINX log and incrementing a4xxErrorCountmetric by 1 every time it appears.
- Term: Anomaly Detection
- Definition: CloudWatch applies machine learning to your metric data to determine a normal baseline.
- Example: Monitoring a web application's traffic where the "normal" band changes based on the time of day and day of the week.
- Term: CloudWatch Agent
- Definition: An installable binary for EC2 and On-Premises servers that collects system-level metrics and logs.
- Example: Collecting
mem_used_percenton a fleet of Ubuntu instances where EC2 standard metrics cannot "see" inside the OS RAM.
Worked Examples
Example 1: Creating a Custom Metric for Application Latency
You have a legacy application that doesn't natively support CloudWatch. You want to track the processing time of a specific function.
CLI Command:
aws cloudwatch put-metric-data \
--namespace "MyApp/Backend" \
--metric-name "ProcessingTime" \
--dimensions InstanceId=i-0123456789abcdef0,Environment=Prod \
--value 450 \
--unit Milliseconds[!NOTE] If you run this frequently with high resolution, ensure you add the
--storage-resolution 1flag to enable sub-minute monitoring.
Example 2: Log Metric Filter for Security Auditing
You need to alert whenever a user attempts to log in as root in your application logs.
- Log Group:
/apps/web-server - Filter Pattern:
[month, day, time, user="root", status="FAILURE"] - Metric Name:
RootLoginFailures - Metric Value:
1 - Alarm: Trigger if
Sum(RootLoginFailures) >= 1in any 5-minute period.
Checkpoint Questions
- Does CloudWatch aggregate metrics across different regions automatically? (Answer: No, metrics are region-specific.)
- What is the maximum number of dimensions you can assign to a single metric? (Answer: 10.)
- True or False: To monitor EC2 Memory Utilization, you only need to enable "Detailed Monitoring." (Answer: False; you must install the CloudWatch Agent.)
- Which alarm state is triggered if there is not enough data points to determine if a threshold is crossed? (Answer: INSUFFICIENT_DATA.)
Muddy Points & Cross-Refs
- CloudWatch Events vs. EventBridge: While they share the same API, EventBridge is the evolved version. For DevOps exams, prefer EventBridge for cross-account/cross-region event routing.
- Metric Streams: Used for exporting CloudWatch metrics to S3 or Kinesis Firehose in near real-time. This is different from a Metric Filter, which inputs data from logs into CloudWatch.
- Resolution vs. Frequency: Detailed monitoring (EC2) provides 1-minute data, but the PutMetricData API can support 1-second resolution for custom metrics.
Comparison Tables
Metric Filter vs. CloudWatch Logs Insights
| Feature | Metric Filter | Logs Insights |
|---|---|---|
| Purpose | Real-time monitoring/alerting | Ad-hoc interactive querying |
| Output | A CloudWatch Metric | A table of results / Visualization |
| Persistence | Ongoing (creates new data) | Point-in-time analysis |
| Alerting | Yes (via Alarms) | No (manual execution) |
Standard vs. High Resolution Metrics
| Feature | Standard Resolution | High Resolution |
|---|---|---|
| Smallest Period | 60 Seconds | 1 Second |
| Use Case | General health monitoring | High-frequency trading/Real-time apps |
| Alarm Speed | Can alarm every 1 minute | Can alarm every 10 seconds |
| Cost | Baseline | Higher (per high-res metric) |