class Alarm (construct)
Language | Type name |
---|---|
.NET | Amazon.CDK.AWS.CloudWatch.Alarm |
Go | github.com/aws/aws-cdk-go/awscdk/v2/awscloudwatch#Alarm |
Java | software.amazon.awscdk.services.cloudwatch.Alarm |
Python | aws_cdk.aws_cloudwatch.Alarm |
TypeScript (source) | aws-cdk-lib » aws_cloudwatch » Alarm |
Implements
IConstruct
, IDependable
, IResource
, IAlarm
, IAlarm
An alarm on a CloudWatch metric.
Example
declare const logGroup: logs.LogGroup;
const mf = new logs.MetricFilter(this, 'MetricFilter', {
logGroup,
metricNamespace: 'MyApp',
metricName: 'Latency',
filterPattern: logs.FilterPattern.exists('$.latency'),
metricValue: '$.latency',
dimensions: {
ErrorCode: '$.errorCode',
},
unit: cloudwatch.Unit.MILLISECONDS,
});
//expose a metric from the metric filter
const metric = mf.metric();
//you can use the metric to create a new alarm
new cloudwatch.Alarm(this, 'alarm from metric filter', {
metric,
threshold: 100,
evaluationPeriods: 2,
});
Initializer
new Alarm(scope: Construct, id: string, props: AlarmProps)
Parameters
- scope
Construct
- id
string
- props
Alarm
Props
Construct Props
Name | Type | Description |
---|---|---|
evaluation | number | The number of periods over which data is compared to the specified threshold. |
metric | IMetric | The metric to add the alarm on. |
threshold | number | The value against which the specified statistic is compared. |
actions | boolean | Whether the actions for this alarm are enabled. |
alarm | string | Description for the alarm. |
alarm | string | Name of the alarm. |
comparison | Comparison | Comparison to use to check if metric is breaching. |
datapoints | number | The number of datapoints that must be breaching to trigger the alarm. |
evaluate | string | Specifies whether to evaluate the data and potentially change the alarm state if there are too few data points to be statistically significant. |
treat | Treat | Sets 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:
Comparison
(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.
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:
Treat
(optional, default: TreatMissingData.Missing)
Sets how this alarm is to handle missing data points.
Properties
Name | Type | Description |
---|---|---|
alarm | string | ARN of this alarm. |
alarm | string | Name of this alarm. |
env | Resource | The environment this resource belongs to. |
metric | IMetric | The metric object this alarm was based on. |
node | Node | The tree node. |
stack | Stack | The stack in which this resource is defined. |
alarmArn
Type:
string
ARN of this alarm.
alarmName
Type:
string
Name of this alarm.
env
Type:
Resource
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
Name | Description |
---|---|
add | Trigger this action if the alarm fires. |
add | Trigger this action if there is insufficient data to evaluate the alarm. |
add | Trigger this action if the alarm returns from breaching state into ok state. |
apply | Apply the given removal policy to this resource. |
render | AlarmRule indicating ALARM state for Alarm. |
to | Turn this alarm into a horizontal annotation. |
to | Returns a string representation of this construct. |
static from | Import an existing CloudWatch alarm provided an ARN. |
static from | Import an existing CloudWatch alarm provided an Name. |
AlarmAction(...actions)
addpublic addAlarmAction(...actions: IAlarmAction[]): void
Parameters
- actions
IAlarm
Action
Trigger this action if the alarm fires.
Typically SnsAction or AutoScalingAction.
InsufficientDataAction(...actions)
addpublic addInsufficientDataAction(...actions: IAlarmAction[]): void
Parameters
- actions
IAlarm
Action
Trigger this action if there is insufficient data to evaluate the alarm.
Typically SnsAction or AutoScalingAction.
OkAction(...actions)
addpublic addOkAction(...actions: IAlarmAction[]): void
Parameters
- actions
IAlarm
Action
Trigger this action if the alarm returns from breaching state into ok state.
Typically SnsAction or AutoScalingAction.
RemovalPolicy(policy)
applypublic applyRemovalPolicy(policy: RemovalPolicy): void
Parameters
- policy
Removal
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
).
AlarmRule()
renderpublic renderAlarmRule(): string
Returns
string
AlarmRule indicating ALARM state for Alarm.
Annotation()
topublic toAnnotation(): HorizontalAnnotation
Returns
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.
String()
topublic toString(): string
Returns
string
Returns a string representation of this construct.
AlarmArn(scope, id, alarmArn)
static frompublic static fromAlarmArn(scope: Construct, id: string, alarmArn: string): IAlarm
Parameters
- scope
Construct
— The parent creating construct (usuallythis
). - id
string
— The construct's name. - alarmArn
string
— Alarm ARN (i.e. arn:aws:cloudwatch::<account-id>:alarm:Foo).
Returns
Import an existing CloudWatch alarm provided an ARN.
AlarmName(scope, id, alarmName)
static frompublic static fromAlarmName(scope: Construct, id: string, alarmName: string): IAlarm
Parameters
- scope
Construct
— The parent creating construct (usuallythis
). - id
string
— The construct's name. - alarmName
string
— Alarm Name.
Returns
Import an existing CloudWatch alarm provided an Name.