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:
# log_group: logs.LogGroup mf = logs.MetricFilter(self, "MetricFilter", log_group=log_group, metric_namespace="MyApp", metric_name="Latency", filter_pattern=logs.FilterPattern.exists("$.latency"), metric_value="$.latency", dimensions={ "ErrorCode": "$.errorCode" }, unit=cloudwatch.Unit.MILLISECONDS ) # expose a metric from the metric filter metric = mf.metric() # you can use the metric to create a new alarm cloudwatch.Alarm(self, "alarm from metric filter", metric=metric, threshold=100, evaluation_periods=2 )
- 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: truealarm_description (
Optional
[str
]) – Description for the alarm. Default: No descriptionalarm_name (
Optional
[str
]) – Name of the alarm. Default: Automatically generated namecomparison_operator (
Optional
[ComparisonOperator
]) – Comparison to use to check if metric is breaching. Default: GreaterThanOrEqualToThresholddatapoints_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 SnsAction 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.
- classmethod from_alarm_name(scope, id, alarm_name)
Import an existing CloudWatch alarm provided an Name.
- classmethod is_construct(x)
Checks if
x
is a construct.Use this method instead of
instanceof
to properly detectConstruct
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 classConstruct
in each copy of theconstructs
library is seen as a different class, and an instance of one class will not test asinstanceof
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 theconstructs
library can be accidentally installed, andinstanceof
will behave unpredictably. It is safest to avoid usinginstanceof
, 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 extendsConstruct
.
- 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