@Stability(value=Experimental)
See: Description
| Interface | Description |
|---|---|
| CloudWatchLogsActionProps |
(experimental) Configuration properties of an action for CloudWatch Logs.
|
| CloudWatchPutMetricActionProps |
(experimental) Configuration properties of an action for CloudWatch metric.
|
| CloudWatchSetAlarmStateActionProps |
(experimental) Configuration properties of an action for CloudWatch alarm.
|
| CommonActionProps |
(experimental) Common properties shared by Actions it access to AWS service.
|
| DynamoDBv2PutItemActionProps |
(experimental) Configuration properties of an action for the dynamodb table.
|
| FirehosePutRecordActionProps |
(experimental) Configuration properties of an action for the Kinesis Data Firehose stream.
|
| IotEventsPutMessageActionProps |
(experimental) Configuration properties of an action for the IoT Events.
|
| IotRepublishMqttActionProps |
(experimental) Configuration properties of an action to republish MQTT messages.
|
| KinesisPutRecordActionProps |
(experimental) Configuration properties of an action for the Kinesis Data stream.
|
| S3PutObjectActionProps |
(experimental) Configuration properties of an action for s3.
|
| SnsTopicActionProps |
(experimental) Configuration options for the SNS topic action.
|
| SqsQueueActionProps |
(experimental) Configuration properties of an action for SQS.
|
| Enum | Description |
|---|---|
| FirehoseRecordSeparator |
(experimental) Record Separator to be used to separate records.
|
| MqttQualityOfService |
(experimental) MQTT Quality of Service (QoS) indicates the level of assurance for delivery of an MQTT Message.
|
| SnsActionMessageFormat |
(experimental) SNS topic action message format options.
|
---
The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.
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:
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();
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();
The code snippet below creates an AWS IoT Rule that puts 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();
The code snippet below creates an AWS IoT Rule that puts 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();
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();
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();
The code snippet below creates an AWS IoT Rule that puts 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();
The code snippet below creates an AWS IoT Rule that puts records to Put records to Kinesis Data Firehose stream when it is triggered.
import software.amazon.awscdk.services.kinesisfirehose.alpha.*;
import software.amazon.awscdk.services.kinesisfirehose.destinations.alpha.*;
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();
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();
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();
The code snippet below creates an AWS IoT rule that writes all or part of an MQTT message to DynamoDB using the DynamoDBv2 action.
import software.amazon.awscdk.services.dynamodb.*;
Table table;
TopicRule topicRule = TopicRule.Builder.create(this, "TopicRule")
.sql(IotSql.fromStringAsVer20160323("SELECT * FROM 'device/+/data'"))
.actions(List.of(
new DynamoDBv2PutItemAction(table)))
.build();
The code snippet below creates an AWS IoT Rule that puts messages to an IoT Events input when it is triggered:
import software.amazon.awscdk.services.iotevents.alpha.*;
import software.amazon.awscdk.services.iam.*;
IRole role;
Input input = Input.Builder.create(this, "MyInput")
.attributeJsonPaths(List.of("payload.temperature", "payload.transactionId"))
.build();
TopicRule topicRule = TopicRule.Builder.create(this, "TopicRule")
.sql(IotSql.fromStringAsVer20160323("SELECT * FROM 'device/+/data'"))
.actions(List.of(
IotEventsPutMessageAction.Builder.create(input)
.batchMode(true) // optional property, default is 'false'
.messageId("${payload.transactionId}") // optional property, default is a new UUID
.role(role)
.build()))
.build();
Copyright © 2023. All rights reserved.