Package: aws-cdk.aws-iot-actions
Actions for AWS IoT Rule---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 theMigrating to AWS CDK v2guide.This library contains integration classes to send data to any number of
supported AWS Services. Instances of these classes should be passed toTopicRuledefined in@aws-cdk/aws-iot.Currently supported are:Republish a message to another MQTT topicInvoke a Lambda functionPut objects to a S3 bucketPut logs to CloudWatch LogsCapture CloudWatch metricsChange state for a CloudWatch alarmPut records to Kinesis Data streamPut records to Kinesis Data Firehose streamSend messages to SQS queuesPublish messages on SNS topicsRepublish a message to another MQTT topicThe code snippet below creates an AWS IoT Rule that republish a message to
another MQTT topic when it is triggered.iot.TopicRule(self,"TopicRule",sql=iot.IotSql.from_string_as_ver20160323("SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'"),actions=[actions.IotRepublishMqttAction("${topic()}/republish",quality_of_service=actions.MqttQualityOfService.AT_LEAST_ONCE)])Invoke a Lambda functionThe code snippet below creates an AWS IoT Rule that invoke a Lambda function
when it is triggered.func=lambda_.Function(self,"MyFunction",runtime=lambda_.Runtime.NODEJS_14_X,handler="index.handler",code=lambda_.Code.from_inline("""exports.handler = (event) => {console.log("It is test for lambda action of AWS IoT Rule.", event);};"""))iot.TopicRule(self,"TopicRule",sql=iot.IotSql.from_string_as_ver20160323("SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'"),actions=[actions.LambdaFunctionAction(func)])Put objects to a S3 bucketThe code snippet below creates an AWS IoT Rule that put objects to a S3 bucket
when it is triggered.bucket=s3.Bucket(self,"MyBucket")iot.TopicRule(self,"TopicRule",sql=iot.IotSql.from_string_as_ver20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),actions=[actions.S3PutObjectAction(bucket)])The propertykeyofS3PutObjectActionis given the value${topic()}/${timestamp()}by default. This${topic()}and${timestamp()}is called Substitution templates. For more information seethis documentation.
In above sample,${topic()}is replaced by a given MQTT topic asdevice/001/data. And${timestamp()}is replaced
by the number of the current timestamp in milliseconds as1636289461203. So if the MQTT broker receives an MQTT topicdevice/001/dataon2021-11-07T00:00:00.000Z, the S3 bucket object will be put todevice/001/data/1636243200000.You can also set specifickeyas following:bucket=s3.Bucket(self,"MyBucket")iot.TopicRule(self,"TopicRule",sql=iot.IotSql.from_string_as_ver20160323("SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'"),actions=[actions.S3PutObjectAction(bucket,key="${year}/${month}/${day}/${topic(2)}")])If you wanna set access control to the S3 bucket object, you can specifyaccessControlas following:bucket=s3.Bucket(self,"MyBucket")iot.TopicRule(self,"TopicRule",sql=iot.IotSql.from_string_as_ver20160323("SELECT * FROM 'device/+/data'"),actions=[actions.S3PutObjectAction(bucket,access_control=s3.BucketAccessControl.PUBLIC_READ)])Put logs to CloudWatch LogsThe code snippet below creates an AWS IoT Rule that put logs to CloudWatch Logs
when it is triggered.importaws_cdk.aws_logsaslogslog_group=logs.LogGroup(self,"MyLogGroup")iot.TopicRule(self,"TopicRule",sql=iot.IotSql.from_string_as_ver20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),actions=[actions.CloudWatchLogsAction(log_group)])Capture CloudWatch metricsThe code snippet below creates an AWS IoT Rule that capture CloudWatch metrics
when it is triggered.topic_rule=iot.TopicRule(self,"TopicRule",sql=iot.IotSql.from_string_as_ver20160323("SELECT topic(2) as device_id, namespace, unit, value, timestamp FROM 'device/+/data'"),actions=[actions.CloudWatchPutMetricAction(metric_name="${topic(2)}",metric_namespace="${namespace}",metric_unit="${unit}",metric_value="${value}",metric_timestamp="${timestamp}")])Change the state of an Amazon CloudWatch alarmThe code snippet below creates an AWS IoT Rule that changes the state of an Amazon CloudWatch alarm when it is triggered:importaws_cdk.aws_cloudwatchascloudwatchmetric=cloudwatch.Metric(namespace="MyNamespace",metric_name="MyMetric",dimensions={"MyDimension":"MyDimensionValue"})alarm=cloudwatch.Alarm(self,"MyAlarm",metric=metric,threshold=100,evaluation_periods=3,datapoints_to_alarm=2)topic_rule=iot.TopicRule(self,"TopicRule",sql=iot.IotSql.from_string_as_ver20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),actions=[actions.CloudWatchSetAlarmStateAction(alarm,reason="AWS Iot Rule action is triggered",alarm_state_to_set=cloudwatch.AlarmState.ALARM)])Put records to Kinesis Data streamThe code snippet below creates an AWS IoT Rule that put records to Kinesis Data
stream when it is triggered.importaws_cdk.aws_kinesisaskinesisstream=kinesis.Stream(self,"MyStream")topic_rule=iot.TopicRule(self,"TopicRule",sql=iot.IotSql.from_string_as_ver20160323("SELECT * FROM 'device/+/data'"),actions=[actions.KinesisPutRecordAction(stream,partition_key="${newuuid()}")])Put records to Kinesis Data Firehose streamThe code snippet below creates an AWS IoT Rule that put records to Put records
to Kinesis Data Firehose stream when it is triggered.importaws_cdk.aws_kinesisfirehoseasfirehoseimportaws_cdk.aws_kinesisfirehose_destinationsasdestinationsbucket=s3.Bucket(self,"MyBucket")stream=firehose.DeliveryStream(self,"MyStream",destinations=[destinations.S3Bucket(bucket)])topic_rule=iot.TopicRule(self,"TopicRule",sql=iot.IotSql.from_string_as_ver20160323("SELECT * FROM 'device/+/data'"),actions=[actions.FirehosePutRecordAction(stream,batch_mode=True,record_separator=actions.FirehoseRecordSeparator.NEWLINE)])Send messages to an SQS queueThe code snippet below creates an AWS IoT Rule that send messages
to an SQS queue when it is triggered:importaws_cdk.aws_sqsassqsqueue=sqs.Queue(self,"MyQueue")topic_rule=iot.TopicRule(self,"TopicRule",sql=iot.IotSql.from_string_as_ver20160323("SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'"),actions=[actions.SqsQueueAction(queue,use_base64=True)])Publish messages on an SNS topicThe code snippet below creates and AWS IoT Rule that publishes messages to an SNS topic when it is triggered:importaws_cdk.aws_snsassnstopic=sns.Topic(self,"MyTopic")topic_rule=iot.TopicRule(self,"TopicRule",sql=iot.IotSql.from_string_as_ver20160323("SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'"),actions=[actions.SnsTopicAction(topic,message_format=actions.SnsActionMessageFormat.JSON)])