software.amazon.awscdk.services.iot.actions.alpha.package-info Maven / Gradle / Ivy
Show all versions of iot-actions-alpha Show documentation
/**
* 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")
* .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();
*
*
*
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());
*
*/
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
package software.amazon.awscdk.services.iot.actions.alpha;