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

  1. Monitor non-AWS-specific metrics.
  2. Trigger alerts based on application-defined thresholds.
  3. 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

  1. Navigate to the CloudWatch Console:
    • Go to MetricsAll Metrics.
  2. Find Your Custom Metric:
    • Select the MyApplication namespace.
    • Locate the ErrorCount metric.
  3. Visualize:
    • Add the metric to a graph to observe data points over time.

Step 3: Create a CloudWatch Dashboard

  1. Navigate to Dashboards:
    • Click Create Dashboard.
  2. Add a Widget:
    • Choose a Line Graph.
    • Select the MyApplication/ErrorCount metric.
  3. Save the Dashboard:
    • Name the dashboard (e.g., ApplicationMetrics).

Step 4: Set Up a CloudWatch Alarm

  1. Navigate to Alarms:
    • Click Create Alarm.
  2. Select Metric:
    • Choose the MyApplication/ErrorCount metric.
  3. Define Threshold:
    • Set a condition (e.g., alert when ErrorCount > 10).
  4. Notification Settings:
    • Choose an existing SNS topic or create a new one for email notifications.
  5. Save Alarm:
    • Name the alarm (e.g., HighErrorRate).

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

  1. Use Namespaces Wisely:
    • Group related metrics using meaningful namespaces.
  2. Leverage Dimensions:
    • Add dimensions (e.g., Environment) to filter and categorize metrics.
  3. Monitor Costs:
    • Publishing high-frequency metrics can increase costs. Optimize the interval.
  4. 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.