aws-cdk-lib.aws_cloudwatch.Alarm

class Alarm (construct)

LanguageType name
.NETAmazon.CDK.AWS.CloudWatch.Alarm
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awscloudwatch#Alarm
Javasoftware.amazon.awscdk.services.cloudwatch.Alarm
Pythonaws_cdk.aws_cloudwatch.Alarm
TypeScript (source)aws-cdk-lib » aws_cloudwatch » Alarm

Implements IConstruct, IDependable, IResource, IAlarm, IAlarmRule

An alarm on a CloudWatch metric.

Example

import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';

declare const alias: lambda.Alias;
const alarm = new cloudwatch.Alarm(this, 'Errors', {
  comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
  threshold: 1,
  evaluationPeriods: 1,
  metric: alias.metricErrors(),
});
const deploymentGroup = new codedeploy.LambdaDeploymentGroup(this, 'BlueGreenDeployment', {
  alias,
  deploymentConfig: codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE,
  alarms: [
    // pass some alarms when constructing the deployment group
    alarm,
  ],
});

// or add alarms to an existing group
declare const blueGreenAlias: lambda.Alias;
deploymentGroup.addAlarm(new cloudwatch.Alarm(this, 'BlueGreenErrors', {
  comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
  threshold: 1,
  evaluationPeriods: 1,
  metric: blueGreenAlias.metricErrors(),
}));

Initializer

new Alarm(scope: Construct, id: string, props: AlarmProps)

Parameters

  • scope Construct
  • id string
  • props AlarmProps

Construct Props

NameTypeDescription
evaluationPeriodsnumberThe number of periods over which data is compared to the specified threshold.
metricIMetricThe metric to add the alarm on.
thresholdnumberThe value against which the specified statistic is compared.
actionsEnabled?booleanWhether the actions for this alarm are enabled.
alarmDescription?stringDescription for the alarm.
alarmName?stringName of the alarm.
comparisonOperator?ComparisonOperatorComparison to use to check if metric is breaching.
datapointsToAlarm?numberThe number of datapoints that must be breaching to trigger the alarm.
evaluateLowSampleCountPercentile?stringSpecifies whether to evaluate the data and potentially change the alarm state if there are too few data points to be statistically significant.
treatMissingData?TreatMissingDataSets how this alarm is to handle missing data points.

evaluationPeriods

Type: number

The number of periods over which data is compared to the specified threshold.


metric

Type: IMetric

The metric to add the alarm on.

Metric objects can be obtained from most resources, or you can construct custom Metric objects by instantiating one.


threshold

Type: number

The value against which the specified statistic is compared.


actionsEnabled?

Type: boolean (optional, default: true)

Whether the actions for this alarm are enabled.


alarmDescription?

Type: string (optional, default: No description)

Description for the alarm.


alarmName?

Type: string (optional, default: Automatically generated name)

Name of the alarm.


comparisonOperator?

Type: ComparisonOperator (optional, default: GreaterThanOrEqualToThreshold)

Comparison to use to check if metric is breaching.


datapointsToAlarm?

Type: number (optional, default: evaluationPeriods)

The number of datapoints that must be breaching to trigger the alarm.

This is used only if you are setting an "M out of N" alarm. In that case, this value is the M. For more information, see Evaluating an Alarm in the Amazon CloudWatch User Guide.

See also: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html#alarm-evaluation


evaluateLowSampleCountPercentile?

Type: string (optional, default: Not configured.)

Specifies whether to evaluate the data and potentially change the alarm state if there are too few data points to be statistically significant.

Used only for alarms that are based on percentiles.


treatMissingData?

Type: TreatMissingData (optional, default: TreatMissingData.Missing)

Sets how this alarm is to handle missing data points.

Properties

NameTypeDescription
alarmArnstringARN of this alarm.
alarmNamestringName of this alarm.
envResourceEnvironmentThe environment this resource belongs to.
metricIMetricThe metric object this alarm was based on.
nodeNodeThe tree node.
stackStackThe stack in which this resource is defined.

alarmArn

Type: string

ARN of this alarm.


alarmName

Type: string

Name of this alarm.


env

Type: ResourceEnvironment

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.


metric

Type: IMetric

The metric object this alarm was based on.


node

Type: Node

The tree node.


stack

Type: Stack

The stack in which this resource is defined.

Methods

NameDescription
addAlarmAction(...actions)Trigger this action if the alarm fires.
addInsufficientDataAction(...actions)Trigger this action if there is insufficient data to evaluate the alarm.
addOkAction(...actions)Trigger this action if the alarm returns from breaching state into ok state.
applyRemovalPolicy(policy)Apply the given removal policy to this resource.
renderAlarmRule()AlarmRule indicating ALARM state for Alarm.
toAnnotation()Turn this alarm into a horizontal annotation.
toString()Returns a string representation of this construct.
static fromAlarmArn(scope, id, alarmArn)Import an existing CloudWatch alarm provided an ARN.
static fromAlarmName(scope, id, alarmName)Import an existing CloudWatch alarm provided an Name.

addAlarmAction(...actions)

public addAlarmAction(...actions: IAlarmAction[]): void

Parameters

  • actions IAlarmAction

Trigger this action if the alarm fires.

Typically SnsAcion or AutoScalingAction.


addInsufficientDataAction(...actions)

public addInsufficientDataAction(...actions: IAlarmAction[]): void

Parameters

  • actions IAlarmAction

Trigger this action if there is insufficient data to evaluate the alarm.

Typically SnsAcion or AutoScalingAction.


addOkAction(...actions)

public addOkAction(...actions: IAlarmAction[]): void

Parameters

  • actions IAlarmAction

Trigger this action if the alarm returns from breaching state into ok state.

Typically SnsAcion or AutoScalingAction.


applyRemovalPolicy(policy)

public applyRemovalPolicy(policy: RemovalPolicy): void

Parameters

  • policy RemovalPolicy

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).


renderAlarmRule()

public renderAlarmRule(): string

Returns

  • string

AlarmRule indicating ALARM state for Alarm.


toAnnotation()

public toAnnotation(): HorizontalAnnotation

Returns

  • HorizontalAnnotation

Turn this alarm into a horizontal annotation.

This is useful if you want to represent an Alarm in a non-AlarmWidget. An AlarmWidget can directly show an alarm, but it can only show a single alarm and no other metrics. Instead, you can convert the alarm to a HorizontalAnnotation and add it as an annotation to another graph.

This might be useful if:

  • You want to show multiple alarms inside a single graph, for example if you have both a "small margin/long period" alarm as well as a "large margin/short period" alarm.

  • You want to show an Alarm line in a graph with multiple metrics in it.


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.


static fromAlarmArn(scope, id, alarmArn)

public static fromAlarmArn(scope: Construct, id: string, alarmArn: string): IAlarm

Parameters

  • scope Construct — The parent creating construct (usually this).
  • id string — The construct's name.
  • alarmArn string — Alarm ARN (i.e. arn:aws:cloudwatch::<account-id>:alarm:Foo).

Returns

  • IAlarm

Import an existing CloudWatch alarm provided an ARN.


static fromAlarmName(scope, id, alarmName)

public static fromAlarmName(scope: Construct, id: string, alarmName: string): IAlarm

Parameters

  • scope Construct — The parent creating construct (usually this).
  • id string — The construct's name.
  • alarmName string — Alarm Name.

Returns

  • IAlarm

Import an existing CloudWatch alarm provided an Name.