Alarm

class aws_cdk.aws_cloudwatch.Alarm(scope, id, *, metric, evaluation_periods, threshold, actions_enabled=None, alarm_description=None, alarm_name=None, comparison_operator=None, datapoints_to_alarm=None, evaluate_low_sample_count_percentile=None, treat_missing_data=None)

Bases: AlarmBase

An alarm on a CloudWatch metric.

ExampleMetadata:

infused

Example:

import aws_cdk.aws_cloudwatch as cloudwatch

# alias: lambda.Alias

# or add alarms to an existing group
# blue_green_alias: lambda.Alias

alarm = cloudwatch.Alarm(self, "Errors",
    comparison_operator=cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
    threshold=1,
    evaluation_periods=1,
    metric=alias.metric_errors()
)
deployment_group = codedeploy.LambdaDeploymentGroup(self, "BlueGreenDeployment",
    alias=alias,
    deployment_config=codedeploy.LambdaDeploymentConfig.LINEAR_10PERCENT_EVERY_1MINUTE,
    alarms=[alarm
    ]
)
deployment_group.add_alarm(cloudwatch.Alarm(self, "BlueGreenErrors",
    comparison_operator=cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
    threshold=1,
    evaluation_periods=1,
    metric=blue_green_alias.metric_errors()
))
Parameters:
  • scope (Construct) –

  • id (str) –

  • metric (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.

  • evaluation_periods (Union[int, float]) – The number of periods over which data is compared to the specified threshold.

  • threshold (Union[int, float]) – The value against which the specified statistic is compared.

  • actions_enabled (Optional[bool]) – Whether the actions for this alarm are enabled. Default: true

  • alarm_description (Optional[str]) – Description for the alarm. Default: No description

  • alarm_name (Optional[str]) – Name of the alarm. Default: Automatically generated name

  • comparison_operator (Optional[ComparisonOperator]) – Comparison to use to check if metric is breaching. Default: GreaterThanOrEqualToThreshold

  • datapoints_to_alarm (Union[int, float, None]) – 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. Default: evaluationPeriods

  • evaluate_low_sample_count_percentile (Optional[str]) – 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. Default: - Not configured.

  • treat_missing_data (Optional[TreatMissingData]) – Sets how this alarm is to handle missing data points. Default: TreatMissingData.Missing

Methods

add_alarm_action(*actions)

Trigger this action if the alarm fires.

Typically SnsAcion or AutoScalingAction.

Parameters:

actions (IAlarmAction) –

Return type:

None

add_insufficient_data_action(*actions)

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

Typically SnsAction or AutoScalingAction.

Parameters:

actions (IAlarmAction) –

Return type:

None

add_ok_action(*actions)

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

Typically SnsAction or AutoScalingAction.

Parameters:

actions (IAlarmAction) –

Return type:

None

apply_removal_policy(policy)

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).

Parameters:

policy (RemovalPolicy) –

Return type:

None

render_alarm_rule()

AlarmRule indicating ALARM state for Alarm.

Return type:

str

to_annotation()

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: :rtype: HorizontalAnnotation

  • 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.

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

alarm_arn

ARN of this alarm.

Attribute:

true

alarm_name

Name of this alarm.

Attribute:

true

env

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

The metric object this alarm was based on.

node

The tree node.

stack

The stack in which this resource is defined.

Static Methods

classmethod from_alarm_arn(scope, id, alarm_arn)

Import an existing CloudWatch alarm provided an ARN.

Parameters:
  • scope (Construct) – The parent creating construct (usually this).

  • id (str) – The construct’s name.

  • alarm_arn (str) – Alarm ARN (i.e. arn:aws:cloudwatch:::alarm:Foo).

Return type:

IAlarm

classmethod from_alarm_name(scope, id, alarm_name)

Import an existing CloudWatch alarm provided an Name.

Parameters:
  • scope (Construct) – The parent creating construct (usually this).

  • id (str) – The construct’s name.

  • alarm_name (str) – Alarm Name.

Return type:

IAlarm

classmethod is_construct(x)

Checks if x is a construct.

Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

Parameters:

x (Any) – Any object.

Return type:

bool

Returns:

true if x is an object created from a class which extends Construct.

classmethod is_owned_resource(construct)

Returns true if the construct was created by CDK, and false otherwise.

Parameters:

construct (IConstruct) –

Return type:

bool

classmethod is_resource(construct)

Check whether the given construct is a Resource.

Parameters:

construct (IConstruct) –

Return type:

bool