Custom CloudWatch Metrics: Setup Guide 2024

published on 08 July 2024

Custom CloudWatch metrics let you track specific aspects of your AWS resources and apps that standard metrics don't cover. Here's what you need to know:

  • Purpose: Monitor app-specific data, create graphs, and set alerts
  • Benefits: Deeper insights, better decision-making, proactive monitoring
  • Setup methods: CloudWatch agent, AWS SDKs, AWS CLI, API calls
  • Key features: High-resolution metrics, dimensions, metric math
  • Cost management: Free tier available, pricing based on usage
Feature Description
Creation Use AWS tools to send custom data points
Alerting Set up alarms for specific thresholds
Integration Works with EC2 Auto Scaling, Lambda, Systems Manager
Debugging Check logs, use CLI/SDKs for troubleshooting

This guide covers setup, advanced techniques, cost management, and problem-solving for custom CloudWatch metrics.

Before You Start

Here's what you need to do before using custom CloudWatch metrics:

Setting Up Your AWS Account

AWS

  1. Create an AWS account if you don't have one
  2. Go to the AWS website to sign up

CloudWatch Basics

Learn the main parts of CloudWatch:

Feature Description
Metrics Numbers that track your AWS resources
Logs Records of what's happening in your system
Alarms Alerts when something needs attention

Required Permissions and IAM Roles

Set up the right permissions:

  1. Create IAM roles and users
  2. Give them permission to:

This lets you make and manage custom metrics safely.

What Are Custom Metrics?

Custom metrics in Amazon CloudWatch are data points you create and send to CloudWatch. They let you track specific parts of your apps that regular metrics don't cover. With custom metrics, you can measure things like how many new users join your app each day.

Standard vs. Custom Metrics

Metric Type Description Use Case
Standard Pre-made by AWS General monitoring
Custom Made by you Specific app needs

Standard metrics work for basic checks, but custom metrics let you dig deeper into your app's performance.

When to Use Custom Metrics

Use custom metrics when:

  • You need to track something specific to your business
  • You want to collect data more often
  • You need more detailed information than standard metrics provide

Limits and Key Points

Keep these things in mind when using custom metrics:

Aspect Limit/Note
Max definitions per destination 2000
Dimension name + value Counts as one extended metric
Cost Charged as CloudWatch custom metrics

Be careful about how many custom metrics you use, as they can affect your bill.

Creating Custom Metrics

This guide shows you how to set up custom metrics in CloudWatch to track specific parts of your app.

Ways to Set Up Custom Metrics

You can set up custom metrics using:

  • CloudWatch agent
  • AWS SDKs
  • AWS CLI
  • API calls

Choose the method that fits your needs best.

CloudWatch Agent Setup

To use the CloudWatch agent:

  1. Install it on your EC2 instance or server
  2. Set it up to collect your chosen metrics
  3. Send the metrics to CloudWatch

Using AWS SDKs

AWS SDKs let you send custom metrics from your app code. Here's an example using Python:

import boto3

cloudwatch = boto3.client('cloudwatch')
cloudwatch.put_metric_data(
    Namespace='MyApp/Metrics',
    MetricData=[
        {
            'MetricName': 'MyCustomMetric',
            'Dimensions': [
                {
                    'Name': 'InstanceId',
                    'Value': 'i-1234567890abcdef'
                }
            ],
            'Value': 100,
            'Unit': 'Count'
        }
    ]
)

AWS CLI Method

You can also use the AWS CLI. Here's an example:

aws cloudwatch put-metric-data --metric-name MyCustomMetric --namespace MyApp/Metrics --value 100 --dimensions InstanceId=i-1234567890abcdef

API Setup

To use the CloudWatch API, send a POST request like this:

POST / HTTP/1.1
Host: monitoring.amazonaws.com
Content-Type: application/x-amz-json-1.1

{
    "Namespace": "MyApp/Metrics",
    "MetricData": [
        {
            "MetricName": "MyCustomMetric",
            "Dimensions": [
                {
                    "Name": "InstanceId",
                    "Value": "i-1234567890abcdef"
                }
            ],
            "Value": 100,
            "Unit": "Count"
        }
    ]
}

Advanced Custom Metric Methods

Learn about more complex ways to use custom metrics.

High-Resolution Metrics Explained

High-resolution metrics in CloudWatch let you:

  • Publish metrics every second
  • Monitor in real-time
  • Trigger alarms faster

This is useful for apps that need quick feedback, like those using Auto Scaling or Lambda functions.

To set up high-resolution metrics:

  1. Use the StorageResolution parameter when creating a metric
  2. Be aware of higher costs compared to standard metrics

Working with Dimensions

Dimensions are key-value pairs that add context to your custom metrics. They help you:

  • Filter metrics
  • Group metrics
  • Aggregate metrics
Dimension Limits Details
Max per metric 30
Character limit 256

Tips for using dimensions:

  • Use clear names
  • Avoid duplicates
  • Keep them the same across your app

Using Metric Math

Metric Math lets you make new metrics from existing ones using math functions. You can:

  • Do calculations (sum, average, percentage)
  • Create new metrics (like error rates or response times)

Example of Metric Math:

AVG(m1, m2, m3)

This calculates the average of metrics m1, m2, and m3.

Metric Math Uses Examples
Error rates (Errors / Total Requests) * 100
Response times AVG(Instance1Time, Instance2Time)
Resource usage SUM(CPU1, CPU2, CPU3)

Using Metric Math helps you understand your app's performance better without creating extra custom metrics.

sbb-itb-6210c22

Setting Up Alerts

Alerts help you quickly respond to changes in your app's performance. Here's how to set them up for your custom CloudWatch metrics.

Making Alarms for Custom Metrics

To create an alarm:

  1. Go to the CloudWatch console and click Alarms.
  2. Choose Create Alarm then Select metric.
  3. Find your custom metric in the search box.
  4. Set up the alarm:
    • Choose when it should go off (e.g., when a value is too high)
    • Pick how often to check (e.g., every 5 minutes)
    • Decide what should happen when the alarm goes off

Setting Up Notifications

To get alerts:

  1. Make a new Amazon SNS topic.
  2. In CloudWatch, go to your alarm.
  3. Add an action to send a message to your SNS topic.

Connecting with Other AWS Services

You can use your custom metrics with other AWS tools. For example, you can make your app grow or shrink based on these metrics:

  1. Go to Amazon EC2 Auto Scaling.
  2. Make or change an Auto Scaling group.
  3. Add a new scaling rule.
  4. Pick your custom metric to control the scaling.
  5. Set up when to add or remove servers based on the metric.
Service Use with Custom Metrics
EC2 Auto Scaling Grow or shrink your app
Lambda Trigger functions
Systems Manager Run tasks on your servers

Managing Costs

Custom CloudWatch metrics can help you track your app's performance, but they can cost money. Here's how to use them wisely.

Custom Metric Pricing

CloudWatch charges for custom metrics based on:

  • How many you create
  • How often you update them
  • How many times you ask for data
Metric Count Price per Month
First 10,000 Free
Up to 100,000 $0.30 per metric
Over 100,000 Price goes down

Ways to Save Money

To keep costs down:

  • Use the free 10,000 metrics each month
  • Keep track of how many metrics you're using
  • Update metrics less often if you can
  • Combine similar metrics

Checking and Fixing Metric Usage

To make sure you're not spending too much:

  • Use CloudWatch to watch your metric use
  • Set up alerts for when you use too many metrics
  • Look at your logs to see where you can cut back
Method How It Helps
Use CloudWatch See how many metrics you're using
Set up alerts Get warned if you use too many
Check your logs Find ways to use fewer metrics

Fixing Common Problems

When using custom metrics in Amazon CloudWatch, you might run into some issues. Here's how to spot and fix them, along with where to get help.

Typical Custom Metric Issues

Issue Cause Solution
Metric not showing up Takes up to 15 minutes to appear Wait and check again
Metric missing from search No new data in 2 weeks Add new data points
Delayed metrics Recent creation or update Give it time, test with sample data

How to Debug

To find and fix problems:

  1. Check CloudWatch logs for errors
  2. Use AWS CLI or SDKs to get metric data
  3. Test your Lambda function with a sample event

Where to Get AWS Help

If you're stuck, try these AWS resources:

Resource Description
CloudWatch docs Full guide to features and fixes
AWS Support Create a ticket for expert help
AWS Forums Ask questions, get community answers
SDK and CLI docs Guides for using AWS tools

Wrap-Up

This guide has covered the key aspects of custom CloudWatch metrics, including:

  • Setting up your AWS account
  • Creating and managing custom metrics
  • Setting up alarms and notifications
  • Using advanced methods like high-resolution metrics and metric math
  • Reducing costs
  • Fixing common problems

Now you have the knowledge to use custom CloudWatch metrics in your AWS setup. Here are some tips to help you use them well:

Tip Description
Use metrics wisely Only create metrics you really need
Watch your costs Keep track of how many metrics you use
Set up alerts Get notified when issues come up
Review regularly Make sure your metrics still fit your needs

Remember to:

  • Only use custom metrics when standard ones don't work
  • Look for ways to save money on metrics
  • Set up alarms to catch problems quickly
  • Check and update your metrics as your needs change

FAQs

Can I add my own metrics to CloudWatch?

Yes, you can add your own metrics to CloudWatch. While AWS services automatically send data to CloudWatch, you might need to track metrics that AWS doesn't cover. You can send custom metrics to CloudWatch using:

  • The CloudWatch agent
  • AWS API

How do I create a custom metric in CloudWatch?

To create a custom metric using the AWS Command Line Interface:

  1. Open your command line tool
  2. Use the cloudwatch command
  3. Add the put-metric-data option

What are custom metrics in CloudWatch?

Custom metrics in CloudWatch let you track specific parts of your app that regular metrics don't cover. They help you:

Regular CloudWatch Metrics Custom Metrics
Monitor EC2 basics (CPU, disk, network) Track app-specific data
Automatically sent by AWS You choose what to send
Limited to AWS resource data Can include any data you want

Custom metrics fill the gaps left by standard AWS monitoring, giving you a complete view of your app's performance.

Related posts

Read more