Skip navigation links

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

Actions for AWS IoT Rule

See: Description

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

Actions for AWS IoT Rule

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:

Invoke a Lambda function

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

 // Example automatically generated from non-compiling source. May contain errors.
 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.core.*;
 
 
 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.

 // Example automatically generated from non-compiling source. May contain errors.
 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.core.*;
 
 
 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:

 // Example automatically generated from non-compiling source. May contain errors.
 TopicRule.Builder.create(this, "TopicRule")
         .sql(iot.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:

 // Example automatically generated from non-compiling source. May contain errors.
 TopicRule.Builder.create(this, "TopicRule")
         .sql(iot.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.core.*;
 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.core.*;
 
 
 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.

 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.core.*;
 
 
 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.core.*;
 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.core.*;
 
 
 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 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.

 // Example automatically generated from non-compiling source. May contain errors.
 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.core.*;
 
 
 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(
             FirehoseStreamAction.Builder.create(stream)
                     .batchMode(true)
                     .recordSeparator(FirehoseStreamRecordSeparator.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:

 // Example automatically generated from non-compiling source. May contain errors.
 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.core.*;
 import software.amazon.awscdk.core.*;
 
 
 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();
 
Skip navigation links

Copyright © 2022. All rights reserved.