AWS CloudWatch Custom Metrics Example: A Step-by-Step Guide
Amazon CloudWatch provides monitoring and observability for AWS resources and applications. While CloudWatch automatically collects standard metrics for AWS services, custom metrics allow you to monitor application-specific data, such as business KPIs or performance indicators not captured by default. This article demonstrates how to create, publish, and visualize custom CloudWatch metrics.
What Are CloudWatch Custom Metrics?
Custom metrics are user-defined data points that can be published to Amazon CloudWatch to monitor application-specific parameters. Examples include:
- Application error rates.
- Custom latency measurements.
- Business metrics like the number of user sign-ups.
Advantages of Custom Metrics
- Monitor non-AWS-specific metrics.
- Trigger alerts based on application-defined thresholds.
- Enable deeper observability for your custom applications.
AWS CloudWatch Custom Metrics Example
Objective
We’ll publish a custom metric (MyApplication/ErrorCount
) to CloudWatch using AWS CLI and Python. Then, we’ll visualize it in a CloudWatch dashboard and set up an alarm.
Step 1: Publish a Custom Metric
Using AWS CLI
Publish a custom metric using the AWS CLI:
aws cloudwatch put-metric-data \
--metric-name ErrorCount \
--namespace MyApplication \
--value 5 \
--unit Count
Explanation:
- Namespace: Groups related metrics (e.g.,
MyApplication
). - Metric Name: Describes the metric (e.g.,
ErrorCount
). - Value: The value to report (e.g.,
5
). - Unit: The data unit (e.g.,
Count
,Milliseconds
).
Using Python (Boto3)
Install the AWS SDK for Python (Boto3):
pip install boto3
Publish a metric using Python:
import boto3
# Initialize CloudWatch client
cloudwatch = boto3.client('cloudwatch', region_name='us-east-1')
# Publish custom metric
response = cloudwatch.put_metric_data(
Namespace='MyApplication',
MetricData=[
{
'MetricName': 'ErrorCount',
'Value': 5,
'Unit': 'Count',
'Dimensions': [
{
'Name': 'Environment',
'Value': 'Production'
}
]
}
]
)
print("Custom metric published:", response)
Step 2: View Custom Metrics in CloudWatch
- Navigate to the CloudWatch Console:
- Go to Metrics → All Metrics.
- Find Your Custom Metric:
- Select the
MyApplication
namespace. - Locate the
ErrorCount
metric.
- Select the
- Visualize:
- Add the metric to a graph to observe data points over time.
Step 3: Create a CloudWatch Dashboard
- Navigate to Dashboards:
- Click Create Dashboard.
- Add a Widget:
- Choose a Line Graph.
- Select the
MyApplication/ErrorCount
metric.
- Save the Dashboard:
- Name the dashboard (e.g.,
ApplicationMetrics
).
- Name the dashboard (e.g.,
Step 4: Set Up a CloudWatch Alarm
- Navigate to Alarms:
- Click Create Alarm.
- Select Metric:
- Choose the
MyApplication/ErrorCount
metric.
- Choose the
- Define Threshold:
- Set a condition (e.g., alert when
ErrorCount
> 10).
- Set a condition (e.g., alert when
- Notification Settings:
- Choose an existing SNS topic or create a new one for email notifications.
- Save Alarm:
- Name the alarm (e.g.,
HighErrorRate
).
- Name the alarm (e.g.,
Step 5: Automate Custom Metric Publishing
Automate the metric publishing by integrating it into your application or scheduling it using AWS Lambda or cron jobs.
Example AWS Lambda Script
import boto3
import random
# Initialize CloudWatch client
cloudwatch = boto3.client('cloudwatch')
def lambda_handler(event, context):
# Simulate error count
error_count = random.randint(0, 20)
# Publish metric
response = cloudwatch.put_metric_data(
Namespace='MyApplication',
MetricData=[
{
'MetricName': 'ErrorCount',
'Value': error_count,
'Unit': 'Count',
'Dimensions': [
{
'Name': 'Environment',
'Value': 'Production'
}
]
}
]
)
print("Published ErrorCount:", error_count)
Best Practices for Custom Metrics
- Use Namespaces Wisely:
- Group related metrics using meaningful namespaces.
- Leverage Dimensions:
- Add dimensions (e.g.,
Environment
) to filter and categorize metrics.
- Add dimensions (e.g.,
- Monitor Costs:
- Publishing high-frequency metrics can increase costs. Optimize the interval.
- Integrate with CloudWatch Alarms:
- Trigger alarms based on thresholds to proactively manage issues.
Conclusion
AWS CloudWatch custom metrics provide powerful capabilities to monitor and analyze application-specific data. By following this example, you can define, publish, and visualize custom metrics, as well as set up automated alarms for proactive issue management. Leveraging custom metrics ensures your applications are always running smoothly and efficiently.