AWS CDK CloudWatch (v1)
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
-
Error: The installed CDK Toolkit version (1.x.x) is older than the CDK library version (2.x.x) used in this app.
cause The AWS CDK Toolkit (CLI) version is incompatible with the version of the CDK Construct Library used in your project.fixUpdate your global AWS CDK Toolkit to the latest v2 version: `npm update -g aws-cdk`. If you need multiple CLI versions, install a specific version locally with `npm install aws-cdk@2.x` and use `npx aws-cdk`. -
CloudFormation Create Stack Failed: [AWS::S3::Bucket] No such bucket: cdk-xxxxxx-assets-xxxxxxxxx-us-east-1
cause Your AWS environment has not been bootstrapped, or the bootstrap stack is outdated, preventing the CDK from staging assets in an S3 bucket for deployment.fixBootstrap your AWS environment with the AWS CDK CLI: `cdk bootstrap aws://ACCOUNT-NUMBER/REGION`. Ensure you use a v2-compatible bootstrap if migrating. -
TypeError: __init__ got an unexpected keyword argument 'removal_policy'
cause A common breaking change between CDK v1 and v2 is the change of keyword arguments for certain construct properties, such as `removal_policy` on resources like S3 Buckets or DynamoDB Tables. The argument might have moved or been renamed.fixConsult the AWS CDK v2 migration guide and the specific construct's documentation. For `removal_policy`, ensure you are importing `RemovalPolicy` from `aws_cdk` (v2) or `aws_cdk.core` (v1) and that the argument name is correct for your CDK version. The v2 arguments are usually more consistent.
Warnings
- breaking AWS CDK v1 reached End-of-Support on June 1, 2023. It is no longer maintained, receives no updates, security patches, or technical support. Continuing to use v1 can expose your applications to security vulnerabilities and unaddressed bugs.
- breaking The AWS CDK v1 architecture uses separate PyPI packages for each AWS service (e.g., `aws-cdk.aws-cloudwatch`, `aws-cdk.aws-sqs`). AWS CDK v2 consolidates all stable constructs into a single package, `aws-cdk-lib`, simplifying dependency management but requiring import path changes.
- gotcha AWS CDK v2 requires a new 'modern' bootstrapping process for your AWS accounts. If you attempt to deploy v2 applications with an old v1 bootstrap, deployments will likely fail.
- gotcha Hardcoding resource names, ARNs, and environment-specific details makes CDK applications non-portable and difficult to maintain across different environments (dev, staging, prod).
Install
-
pip install aws-cdk.aws-cloudwatch aws-cdk.core -
pip install aws-cdk-lib
Imports
- aws_cloudwatch
from aws_cdk_lib import aws_cloudwatch
from aws_cdk import aws_cloudwatch
- Alarm
from aws_cdk.aws_cloudwatch import Alarm
- Metric
from aws_cdk.aws_cloudwatch import Metric
Quickstart
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()