Package software.amazon.awscdk.services.iot.actions.alpha
Actions for AWS IoT Rule
---
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-lib/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
- Write messages into columns of DynamoDB
- Put messages IoT Events input
- Send messages to HTTPS endpoints
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_LATEST) .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 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();
Put logs to CloudWatch Logs
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();
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();
Start Step Functions State Machine
The code snippet below creates an AWS IoT Rule that starts a Step Functions State Machine when it is triggered.
StateMachine stateMachine = StateMachine.Builder.create(this, "SM") .definitionBody(DefinitionBody.fromChainable(Wait.Builder.create(this, "Hello").time(WaitTime.duration(Duration.seconds(10))).build())) .build(); TopicRule.Builder.create(this, "TopicRule") .sql(IotSql.fromStringAsVer20160323("SELECT * FROM 'device/+/data'")) .actions(List.of( new StepFunctionsStateMachineAction(stateMachine))) .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") .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();
Put records to Kinesis Data stream
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();
Put records to Kinesis Data Firehose stream
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") .destination(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();
Write attributes of a message to DynamoDB
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();
Put messages IoT Events input
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();
Send Messages to HTTPS Endpoints
The code snippet below creates an AWS IoT Rule that sends messages to an HTTPS endpoint when it is triggered:
TopicRule topicRule = TopicRule.Builder.create(this, "TopicRule") .sql(IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'")) .build(); topicRule.addAction( HttpsAction.Builder.create("https://example.com/endpoint") .confirmationUrl("https://example.com") .headers(List.of(HttpActionHeader.builder().key("key0").value("value0").build(), HttpActionHeader.builder().key("key1").value("value1").build())) .auth(HttpActionSigV4Auth.builder().serviceName("serviceName").signingRegion("us-east-1").build()) .build());
Write Data to Open Search Service
The code snippet below creates an AWS IoT Rule that writes data to an Open Search Service when it is triggered:
import software.amazon.awscdk.services.opensearchservice.*; Domain domain; TopicRule topicRule = TopicRule.Builder.create(this, "TopicRule") .sql(IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'")) .build(); topicRule.addAction(OpenSearchAction.Builder.create(domain) .id("my-id") .index("my-index") .type("my-type") .build());
-
ClassDescription(experimental) The action to send data to Amazon CloudWatch Logs.(experimental) A fluent builder for
CloudWatchLogsAction
.(experimental) Configuration properties of an action for CloudWatch Logs.A builder forCloudWatchLogsActionProps
An implementation forCloudWatchLogsActionProps
(experimental) The action to capture an Amazon CloudWatch metric.(experimental) A fluent builder forCloudWatchPutMetricAction
.(experimental) Configuration properties of an action for CloudWatch metric.A builder forCloudWatchPutMetricActionProps
An implementation forCloudWatchPutMetricActionProps
(experimental) The action to change the state of an Amazon CloudWatch alarm.(experimental) A fluent builder forCloudWatchSetAlarmStateAction
.(experimental) Configuration properties of an action for CloudWatch alarm.A builder forCloudWatchSetAlarmStateActionProps
An implementation forCloudWatchSetAlarmStateActionProps
(experimental) Common properties shared by Actions it access to AWS service.A builder forCommonActionProps
An implementation forCommonActionProps
(experimental) The action to put the record from an MQTT message to the DynamoDB table.(experimental) A fluent builder forDynamoDBv2PutItemAction
.(experimental) Configuration properties of an action for the dynamodb table.A builder forDynamoDBv2PutItemActionProps
An implementation forDynamoDBv2PutItemActionProps
(experimental) The action to put the record from an MQTT message to the Kinesis Data Firehose stream.(experimental) A fluent builder forFirehosePutRecordAction
.(experimental) Configuration properties of an action for the Kinesis Data Firehose stream.A builder forFirehosePutRecordActionProps
An implementation forFirehosePutRecordActionProps
(experimental) Record Separator to be used to separate records.Example:A builder forHttpActionHeader
An implementation forHttpActionHeader
Example:A builder forHttpActionSigV4Auth
An implementation forHttpActionSigV4Auth
(experimental) The action to send data from an MQTT message to a web application or service.(experimental) A fluent builder forHttpsAction
.(experimental) Configuration properties of an HTTPS action.A builder forHttpsActionProps
An implementation forHttpsActionProps
(experimental) The action to put the message from an MQTT message to the IoT Events input.(experimental) A fluent builder forIotEventsPutMessageAction
.(experimental) Configuration properties of an action for the IoT Events.A builder forIotEventsPutMessageActionProps
An implementation forIotEventsPutMessageActionProps
(experimental) The action to put the record from an MQTT message to republish another MQTT topic.(experimental) A fluent builder forIotRepublishMqttAction
.(experimental) Configuration properties of an action to republish MQTT messages.A builder forIotRepublishMqttActionProps
An implementation forIotRepublishMqttActionProps
(experimental) The action to put the record from an MQTT message to the Kinesis Data stream.(experimental) A fluent builder forKinesisPutRecordAction
.(experimental) Configuration properties of an action for the Kinesis Data stream.A builder forKinesisPutRecordActionProps
An implementation forKinesisPutRecordActionProps
(experimental) The action to invoke an AWS Lambda function, passing in an MQTT message.(experimental) MQTT Quality of Service (QoS) indicates the level of assurance for delivery of an MQTT Message.(experimental) The action to write data to an Amazon OpenSearch Service domain.(experimental) A fluent builder forOpenSearchAction
.(experimental) Configuration properties of an action for Open Search.A builder forOpenSearchActionProps
An implementation forOpenSearchActionProps
(experimental) The action to write the data from an MQTT message to an Amazon S3 bucket.(experimental) A fluent builder forS3PutObjectAction
.(experimental) Configuration properties of an action for s3.A builder forS3PutObjectActionProps
An implementation forS3PutObjectActionProps
(experimental) SNS topic action message format options.(experimental) The action to write the data from an MQTT message to an Amazon SNS topic.(experimental) A fluent builder forSnsTopicAction
.(experimental) Configuration options for the SNS topic action.A builder forSnsTopicActionProps
An implementation forSnsTopicActionProps
(experimental) The action to write the data from an MQTT message to an Amazon SQS queue.(experimental) A fluent builder forSqsQueueAction
.(experimental) Configuration properties of an action for SQS.A builder forSqsQueueActionProps
An implementation forSqsQueueActionProps
(experimental) The action to put the record from an MQTT message to the Step Functions State Machine.(experimental) A fluent builder forStepFunctionsStateMachineAction
.(experimental) Configuration properties of an action for the Step Functions State Machine.A builder forStepFunctionsStateMachineActionProps
An implementation forStepFunctionsStateMachineActionProps