Package software.amazon.awscdk.services.iot.actions


@Stability(Experimental) @Deprecated package software.amazon.awscdk.services.iot.actions
Deprecated.

Actions for AWS IoT Rule

---

End-of-Support

AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2.

For more information on how to migrate, see the Migrating to AWS CDK v2 guide.


This library contains integration classes to send data to any number of supported AWS Services. Instances of these classes should be passed to TopicRule defined in @aws-cdk/aws-iot.

Currently supported are:

  • Republish a message to another MQTT topic
  • Invoke a Lambda function
  • Put objects to a S3 bucket
  • Put logs to CloudWatch Logs
  • Capture CloudWatch metrics
  • Change state for a CloudWatch alarm
  • Put records to Kinesis Data stream
  • Put records to Kinesis Data Firehose stream
  • Send messages to SQS queues
  • Publish messages on SNS topics

Republish a message to another MQTT topic

The code snippet below creates an AWS IoT Rule that republish a message to another MQTT topic when it is triggered.

 TopicRule.Builder.create(this, "TopicRule")
         .sql(IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'"))
         .actions(List.of(
             IotRepublishMqttAction.Builder.create("${topic()}/republish")
                     .qualityOfService(MqttQualityOfService.AT_LEAST_ONCE)
                     .build()))
         .build();
 

Invoke a Lambda function

The code snippet below creates an AWS IoT Rule that invoke a Lambda function when it is triggered.

 Function func = Function.Builder.create(this, "MyFunction")
         .runtime(Runtime.NODEJS_14_X)
         .handler("index.handler")
         .code(Code.fromInline("\n    exports.handler = (event) => {\n      console.log(\"It is test for lambda action of AWS IoT Rule.\", event);\n    };"))
         .build();
 
 TopicRule.Builder.create(this, "TopicRule")
         .sql(IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'"))
         .actions(List.of(new LambdaFunctionAction(func)))
         .build();
 

Put objects to a S3 bucket

The code snippet below creates an AWS IoT Rule that put objects to a S3 bucket when it is triggered.

 Bucket bucket = new Bucket(this, "MyBucket");
 
 TopicRule.Builder.create(this, "TopicRule")
         .sql(IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"))
         .actions(List.of(new S3PutObjectAction(bucket)))
         .build();
 

The property key of S3PutObjectAction is given the value ${topic()}/${timestamp()} by default. This ${topic()} and ${timestamp()} is called Substitution templates. For more information see this documentation. In above sample, ${topic()} is replaced by a given MQTT topic as device/001/data. And ${timestamp()} is replaced by the number of the current timestamp in milliseconds as 1636289461203. So if the MQTT broker receives an MQTT topic device/001/data on 2021-11-07T00:00:00.000Z, the S3 bucket object will be put to device/001/data/1636243200000.

You can also set specific key as following:

 Bucket bucket = new Bucket(this, "MyBucket");
 
 TopicRule.Builder.create(this, "TopicRule")
         .sql(IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'"))
         .actions(List.of(
             S3PutObjectAction.Builder.create(bucket)
                     .key("${year}/${month}/${day}/${topic(2)}")
                     .build()))
         .build();
 

If you wanna set access control to the S3 bucket object, you can specify accessControl as following:

 Bucket bucket = new Bucket(this, "MyBucket");
 
 TopicRule.Builder.create(this, "TopicRule")
         .sql(IotSql.fromStringAsVer20160323("SELECT * FROM 'device/+/data'"))
         .actions(List.of(
             S3PutObjectAction.Builder.create(bucket)
                     .accessControl(BucketAccessControl.PUBLIC_READ)
                     .build()))
         .build();
 

Put logs to CloudWatch Logs

The code snippet below creates an AWS IoT Rule that put logs to CloudWatch Logs when it is triggered.

 import software.amazon.awscdk.services.logs.*;
 
 
 LogGroup logGroup = new LogGroup(this, "MyLogGroup");
 
 TopicRule.Builder.create(this, "TopicRule")
         .sql(IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"))
         .actions(List.of(new CloudWatchLogsAction(logGroup)))
         .build();
 

Capture CloudWatch metrics

The code snippet below creates an AWS IoT Rule that capture CloudWatch metrics when it is triggered.

 TopicRule topicRule = TopicRule.Builder.create(this, "TopicRule")
         .sql(IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, namespace, unit, value, timestamp FROM 'device/+/data'"))
         .actions(List.of(
             CloudWatchPutMetricAction.Builder.create()
                     .metricName("${topic(2)}")
                     .metricNamespace("${namespace}")
                     .metricUnit("${unit}")
                     .metricValue("${value}")
                     .metricTimestamp("${timestamp}")
                     .build()))
         .build();
 

Change the state of an Amazon CloudWatch alarm

The code snippet below creates an AWS IoT Rule that changes the state of an Amazon CloudWatch alarm when it is triggered:

 import software.amazon.awscdk.services.cloudwatch.*;
 
 
 Metric metric = Metric.Builder.create()
         .namespace("MyNamespace")
         .metricName("MyMetric")
         .dimensions(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();
 

Put records to Kinesis Data stream

The code snippet below creates an AWS IoT Rule that put records to Kinesis Data stream when it is triggered.

 import software.amazon.awscdk.services.kinesis.*;
 
 
 Stream stream = new Stream(this, "MyStream");
 
 TopicRule topicRule = TopicRule.Builder.create(this, "TopicRule")
         .sql(IotSql.fromStringAsVer20160323("SELECT * FROM 'device/+/data'"))
         .actions(List.of(
             KinesisPutRecordAction.Builder.create(stream)
                     .partitionKey("${newuuid()}")
                     .build()))
         .build();
 

Put records to Kinesis Data Firehose stream

The code snippet below creates an AWS IoT Rule that put records to Put records to Kinesis Data Firehose stream when it is triggered.

 import software.amazon.awscdk.services.kinesisfirehose.*;
 import software.amazon.awscdk.services.kinesisfirehose.destinations.*;
 
 
 Bucket bucket = new Bucket(this, "MyBucket");
 DeliveryStream stream = DeliveryStream.Builder.create(this, "MyStream")
         .destinations(List.of(new S3Bucket(bucket)))
         .build();
 
 TopicRule topicRule = TopicRule.Builder.create(this, "TopicRule")
         .sql(IotSql.fromStringAsVer20160323("SELECT * FROM 'device/+/data'"))
         .actions(List.of(
             FirehosePutRecordAction.Builder.create(stream)
                     .batchMode(true)
                     .recordSeparator(FirehoseRecordSeparator.NEWLINE)
                     .build()))
         .build();
 

Send messages to an SQS queue

The code snippet below creates an AWS IoT Rule that send messages to an SQS queue when it is triggered:

 import software.amazon.awscdk.services.sqs.*;
 
 
 Queue queue = new Queue(this, "MyQueue");
 
 TopicRule topicRule = TopicRule.Builder.create(this, "TopicRule")
         .sql(IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'"))
         .actions(List.of(
             SqsQueueAction.Builder.create(queue)
                     .useBase64(true)
                     .build()))
         .build();
 

Publish messages on an SNS topic

The code snippet below creates and AWS IoT Rule that publishes messages to an SNS topic when it is triggered:

 import software.amazon.awscdk.services.sns.*;
 
 
 Topic topic = new Topic(this, "MyTopic");
 
 TopicRule topicRule = TopicRule.Builder.create(this, "TopicRule")
         .sql(IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'"))
         .actions(List.of(
             SnsTopicAction.Builder.create(topic)
                     .messageFormat(SnsActionMessageFormat.JSON)
                     .build()))
         .build();
 
Deprecated: AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2. For more information on how to migrate, see https://docs.aws.amazon.com/cdk/v2/guide/migrating-v2.html