AWS CDK CloudWatch (v1)

1.204.0 · deprecated · verified Thu Apr 16

The `aws-cdk-aws-cloudwatch` library is part of the AWS Cloud Development Kit (CDK) v1, providing Python constructs for provisioning and managing AWS CloudWatch resources, including metrics, alarms, and dashboards. This specific package, version 1.204.0, was released on June 19, 2023. AWS CDK v1 officially reached End-of-Support on June 1, 2023, meaning it no longer receives maintenance, updates, patches, or technical support. Users are strongly recommended to migrate to AWS CDK v2.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic AWS CloudWatch alarm using AWS CDK v1. It defines an SQS queue and sets up an alarm that triggers if the number of visible messages in the queue exceeds a specified threshold.

import os
from aws_cdk import (
    aws_cloudwatch as cw,
    aws_sqs as sqs,
    core as cdk
)

class MyCloudWatchStack(cdk.Stack):
    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        queue = sqs.Queue(self, "MyQueue")

        # Create a metric for the number of visible messages in the SQS queue
        # Using a default SQS queue for demonstration purposes.
        queue_metric = queue.metric_approximate_number_of_messages_visible(
            period=cdk.Duration.minutes(5)
        )

        # Create an alarm if the number of visible messages exceeds 100
        cw.Alarm(self, "QueueHighMessagesAlarm",
            metric=queue_metric,
            threshold=100,
            evaluation_periods=1,
            comparison_operator=cw.ComparisonOperator.GREATER_THAN_THRESHOLD,
            alarm_description="Alarm when queue visible messages are too high"
        )

app = cdk.App()
# Specify account and region for deployment (optional, but good practice)
# env = cdk.Environment(account=os.environ.get('CDK_DEFAULT_ACCOUNT'), region=os.environ.get('CDK_DEFAULT_REGION'))
MyCloudWatchStack(app, "CloudWatchV1Stack")
app.synth()

view raw JSON →