Skip to content
v2.275.0

Experimental

@aws-cdk/aws-iot-actions-alpha is an experimental (alpha) package: its API may change in any release, without a major version bump.

CloudWatchSetAlarmStateAction Class ​

The action to change the state of an Amazon CloudWatch alarm.

Example ​

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

const metric = new cloudwatch.Metric({
  namespace: 'MyNamespace',
  metricName: 'MyMetric',
  dimensionsMap: { MyDimension: 'MyDimensionValue' },
});
const alarm = new cloudwatch.Alarm(this, 'MyAlarm', {
  metric: metric,
  threshold: 100,
  evaluationPeriods: 3,
  datapointsToAlarm: 2,
});

const topicRule = new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
  actions: [
    new actions.CloudWatchSetAlarmStateAction(alarm, {
      reason: 'AWS Iot Rule action is triggered',
      alarmStateToSet: cloudwatch.AlarmState.ALARM,
    }),
  ],
});
python
import aws_cdk.aws_cloudwatch as cloudwatch


metric = cloudwatch.Metric(
    namespace="MyNamespace",
    metric_name="MyMetric",
    dimensions_map={"MyDimension": "MyDimensionValue"}
)
alarm = cloudwatch.Alarm(self, "MyAlarm",
    metric=metric,
    threshold=100,
    evaluation_periods=3,
    datapoints_to_alarm=2
)

topic_rule = iot.TopicRule(self, "TopicRule",
    sql=iot.IotSql.from_string_as_ver20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
    actions=[
        actions.CloudWatchSetAlarmStateAction(alarm,
            reason="AWS Iot Rule action is triggered",
            alarm_state_to_set=cloudwatch.AlarmState.ALARM
        )
    ]
)
java
import software.amazon.awscdk.services.cloudwatch.*;


Metric metric = Metric.Builder.create()
        .namespace("MyNamespace")
        .metricName("MyMetric")
        .dimensionsMap(Map.of("MyDimension", "MyDimensionValue"))
        .build();
Alarm alarm = Alarm.Builder.create(this, "MyAlarm")
        .metric(metric)
        .threshold(100)
        .evaluationPeriods(3)
        .datapointsToAlarm(2)
        .build();

TopicRule topicRule = TopicRule.Builder.create(this, "TopicRule")
        .sql(IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"))
        .actions(List.of(
            CloudWatchSetAlarmStateAction.Builder.create(alarm)
                    .reason("AWS Iot Rule action is triggered")
                    .alarmStateToSet(AlarmState.ALARM)
                    .build()))
        .build();
csharp
// Install: dotnet add package Amazon.CDK.AWS.IoT.Actions.Alpha
using Amazon.CDK.AWS.CloudWatch;


var metric = new Metric(new MetricProps {
    Namespace = "MyNamespace",
    MetricName = "MyMetric",
    DimensionsMap = new Dictionary<string, string> { { "MyDimension", "MyDimensionValue" } }
});
var alarm = new Alarm(this, "MyAlarm", new AlarmProps {
    Metric = metric,
    Threshold = 100,
    EvaluationPeriods = 3,
    DatapointsToAlarm = 2
});

var topicRule = new TopicRule(this, "TopicRule", new TopicRuleProps {
    Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
    Actions = new [] {
        new CloudWatchSetAlarmStateAction(alarm, new CloudWatchSetAlarmStateActionProps {
            Reason = "AWS Iot Rule action is triggered",
            AlarmStateToSet = AlarmState.ALARM
        }) }
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdkiotactionsalpha/v2
import "github.com/aws/aws-cdk-go/awscdk"


metric := cloudwatch.NewMetric(&MetricProps{
	Namespace: jsii.String("MyNamespace"),
	MetricName: jsii.String("MyMetric"),
	DimensionsMap: map[string]*string{
		"MyDimension": jsii.String("MyDimensionValue"),
	},
})
alarm := cloudwatch.NewAlarm(this, jsii.String("MyAlarm"), &AlarmProps{
	Metric: metric,
	Threshold: jsii.Number(100),
	EvaluationPeriods: jsii.Number(3),
	DatapointsToAlarm: jsii.Number(2),
})

topicRule := iot.NewTopicRule(this, jsii.String("TopicRule"), &TopicRuleProps{
	Sql: iot.IotSql_FromStringAsVer20160323(jsii.String("SELECT topic(2) as device_id FROM 'device/+/data'")),
	Actions: []IAction{
		actions.NewCloudWatchSetAlarmStateAction(alarm, &CloudWatchSetAlarmStateActionProps{
			Reason: jsii.String("AWS Iot Rule action is triggered"),
			AlarmStateToSet: cloudwatch.AlarmState_ALARM,
		}),
	},
})

Initializer ​

ts
new CloudWatchSetAlarmStateAction(alarm: IAlarm, props: CloudWatchSetAlarmStateActionProps)
python
CloudWatchSetAlarmStateAction(alarm: IAlarm, props: CloudWatchSetAlarmStateActionProps)
java
new CloudWatchSetAlarmStateAction(IAlarm alarm, CloudWatchSetAlarmStateActionProps props)
csharp
new CloudWatchSetAlarmStateAction(IAlarm alarm, CloudWatchSetAlarmStateActionProps props)
go
NewCloudWatchSetAlarmStateAction(Alarm IAlarm, Props CloudWatchSetAlarmStateActionProps)