AWS CDK construct library for Amazon CloudWatch metrics, alarms, dashboards, and monitoring infrastructure as code
npx @tessl/cli install tessl/npm-aws-cdk--aws-cloudwatch@1.204.0The AWS CDK CloudWatch construct library provides comprehensive TypeScript APIs for creating CloudWatch metrics, alarms, dashboards, and monitoring infrastructure as code. This library offers complete CloudWatch functionality with type safety, extensive configuration options, and seamless integration with other AWS CDK constructs.
npm install @aws-cdk/aws-cloudwatchimport * as cloudwatch from '@aws-cdk/aws-cloudwatch';For specific imports:
import {
Metric,
Alarm,
Dashboard,
GraphWidget,
AlarmWidget
} from '@aws-cdk/aws-cloudwatch';import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as cdk from '@aws-cdk/core';
// Create a custom metric
const metric = new cloudwatch.Metric({
namespace: 'MyApp',
metricName: 'RequestCount',
dimensionsMap: {
Environment: 'Production'
}
});
// Create an alarm
const alarm = new cloudwatch.Alarm(this, 'HighRequests', {
metric: metric,
threshold: 100,
evaluationPeriods: 2,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD
});
// Create a dashboard with widgets
const dashboard = new cloudwatch.Dashboard(this, 'MyDashboard', {
dashboardName: 'ApplicationMetrics'
});
dashboard.addWidgets(
new cloudwatch.GraphWidget({
title: 'Request Rate',
left: [metric]
})
);The AWS CDK CloudWatch library is organized around several key components:
Metric and MathExpression classes for defining and manipulating CloudWatch metricsAlarm, CompositeAlarm, and rule-based alarm configurations with action supportDashboard class with comprehensive widget system for visualizationCreate, configure, and manipulate CloudWatch metrics with comprehensive options for dimensions, statistics, periods, and cross-account access.
class Metric implements IMetric {
constructor(props: MetricProps);
static grantPutMetricData(grantee: iam.IGrantable): iam.Grant;
}
interface MetricProps {
namespace: string;
metricName: string;
dimensionsMap?: DimensionsMap;
period?: Duration;
statistic?: string;
unit?: Unit;
label?: string;
color?: string;
account?: string;
region?: string;
}Comprehensive alarm functionality including simple alarms, composite alarms with boolean logic, and alarm actions for automated responses.
class Alarm extends AlarmBase {
constructor(scope: Construct, id: string, props: AlarmProps);
static fromAlarmArn(scope: Construct, id: string, alarmArn: string): IAlarm;
}
interface AlarmProps {
metric: IMetric;
threshold: number;
evaluationPeriods: number;
comparisonOperator?: ComparisonOperator;
treatMissingData?: TreatMissingData;
actionsEnabled?: boolean;
alarmDescription?: string;
alarmName?: string;
datapointsToAlarm?: number;
}Complete dashboard system with multiple widget types for creating rich CloudWatch dashboards with flexible layouts and visualization options.
class Dashboard extends Resource {
constructor(scope: Construct, id: string, props?: DashboardProps);
addWidgets(...widgets: IWidget[]): void;
}
interface DashboardProps {
dashboardName?: string;
widgets?: IWidget[][];
start?: string;
end?: string;
periodOverride?: PeriodOverride;
}Flexible layout components for organizing dashboard widgets into rows, columns, and spacers with precise positioning control.
class Row implements IWidget {
constructor(...widgets: IWidget[]);
readonly width: number;
readonly height: number;
}
class Column implements IWidget {
constructor(...widgets: IWidget[]);
readonly width: number;
readonly height: number;
}interface IMetric {
warnings?: string[];
toMetricConfig(): MetricConfig;
}
interface IWidget {
readonly width: number;
readonly height: number;
readonly warnings?: string[];
position(x: number, y: number): void;
toJson(): any[];
}
interface IAlarm extends IAlarmRule, IResource {
readonly alarmArn: string;
readonly alarmName: string;
}
enum Unit {
SECONDS = 'Seconds',
MICROSECONDS = 'Microseconds',
MILLISECONDS = 'Milliseconds',
BYTES = 'Bytes',
KILOBYTES = 'Kilobytes',
MEGABYTES = 'Megabytes',
GIGABYTES = 'Gigabytes',
TERABYTES = 'Terabytes',
BITS = 'Bits',
KILOBITS = 'Kilobits',
MEGABITS = 'Megabits',
GIGABITS = 'Gigabits',
TERABITS = 'Terabits',
PERCENT = 'Percent',
COUNT = 'Count',
BYTES_PER_SECOND = 'Bytes/Second',
KILOBYTES_PER_SECOND = 'Kilobytes/Second',
MEGABYTES_PER_SECOND = 'Megabytes/Second',
GIGABYTES_PER_SECOND = 'Gigabytes/Second',
TERABYTES_PER_SECOND = 'Terabytes/Second',
BITS_PER_SECOND = 'Bits/Second',
KILOBITS_PER_SECOND = 'Kilobits/Second',
MEGABITS_PER_SECOND = 'Megabits/Second',
GIGABITS_PER_SECOND = 'Gigabits/Second',
TERABITS_PER_SECOND = 'Terabits/Second',
COUNT_PER_SECOND = 'Count/Second',
NONE = 'None'
}
enum ComparisonOperator {
GREATER_THAN_OR_EQUAL_TO_THRESHOLD = 'GreaterThanOrEqualToThreshold',
GREATER_THAN_THRESHOLD = 'GreaterThanThreshold',
LESS_THAN_THRESHOLD = 'LessThanThreshold',
LESS_THAN_OR_EQUAL_TO_THRESHOLD = 'LessThanOrEqualToThreshold'
}
type DimensionsMap = { [key: string]: string };