Namespace Amazon.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 the Migrating to AWS CDK v2 guide.
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:
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.
new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'"),
Actions = new [] {
new IotRepublishMqttAction("${topic()}/republish", new IotRepublishMqttActionProps {
QualityOfService = MqttQualityOfService.AT_LEAST_ONCE
}) }
});
Invoke a Lambda function
The code snippet below creates an AWS IoT Rule that invoke a Lambda function when it is triggered.
var func = new Function(this, "MyFunction", new FunctionProps {
Runtime = Runtime.NODEJS_14_X,
Handler = "index.handler",
Code = Code.FromInline(@"
exports.handler = (event) => {
console.log(""It is test for lambda action of AWS IoT Rule."", event);
};")
});
new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'"),
Actions = new [] { new LambdaFunctionAction(func) }
});
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.
var bucket = new Bucket(this, "MyBucket");
new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
Actions = new [] { new S3PutObjectAction(bucket) }
});
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:
var bucket = new Bucket(this, "MyBucket");
new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'"),
Actions = new [] {
new S3PutObjectAction(bucket, new S3PutObjectActionProps {
Key = "${year}/${month}/${day}/${topic(2)}"
}) }
});
If you wanna set access control to the S3 bucket object, you can specify accessControl as following:
var bucket = new Bucket(this, "MyBucket");
new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT * FROM 'device/+/data'"),
Actions = new [] {
new S3PutObjectAction(bucket, new S3PutObjectActionProps {
AccessControl = BucketAccessControl.PUBLIC_READ
}) }
});
Put logs to CloudWatch Logs
The code snippet below creates an AWS IoT Rule that put logs to CloudWatch Logs when it is triggered.
using Amazon.CDK.AWS.Logs;
var logGroup = new LogGroup(this, "MyLogGroup");
new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
Actions = new [] { new CloudWatchLogsAction(logGroup) }
});
Capture CloudWatch metrics
The code snippet below creates an AWS IoT Rule that capture CloudWatch metrics when it is triggered.
var topicRule = new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id, namespace, unit, value, timestamp FROM 'device/+/data'"),
Actions = new [] {
new CloudWatchPutMetricAction(new CloudWatchPutMetricActionProps {
MetricName = "${topic(2)}",
MetricNamespace = "${namespace}",
MetricUnit = "${unit}",
MetricValue = "${value}",
MetricTimestamp = "${timestamp}"
}) }
});
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:
using Amazon.CDK.AWS.CloudWatch;
var metric = new Metric(new MetricProps {
Namespace = "MyNamespace",
MetricName = "MyMetric",
Dimensions = new Dictionary<string, object> { { "MyDimension", "MyDimensionValue" } }
});
var alarm = new Alarm(this, "MyAlarm", new AlarmProps {
Metric = metric,
Threshold = 100,
EvaluationPeriods = 3,
DatapointsToAlarm = 2
});
var topicRule = new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
Actions = new [] {
new CloudWatchSetAlarmStateAction(alarm, new CloudWatchSetAlarmStateActionProps {
Reason = "AWS Iot Rule action is triggered",
AlarmStateToSet = AlarmState.ALARM
}) }
});
Put records to Kinesis Data stream
The code snippet below creates an AWS IoT Rule that put records to Kinesis Data stream when it is triggered.
using Amazon.CDK.AWS.Kinesis;
var stream = new Stream(this, "MyStream");
var topicRule = new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT * FROM 'device/+/data'"),
Actions = new [] {
new KinesisPutRecordAction(stream, new KinesisPutRecordActionProps {
PartitionKey = "${newuuid()}"
}) }
});
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.
using Amazon.CDK.AWS.KinesisFirehose;
using Amazon.CDK.AWS.KinesisFirehose.Destinations;
var bucket = new Bucket(this, "MyBucket");
var stream = new DeliveryStream(this, "MyStream", new DeliveryStreamProps {
Destinations = new [] { new S3Bucket(bucket) }
});
var topicRule = new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT * FROM 'device/+/data'"),
Actions = new [] {
new FirehosePutRecordAction(stream, new FirehosePutRecordActionProps {
BatchMode = true,
RecordSeparator = FirehoseRecordSeparator.NEWLINE
}) }
});
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:
using Amazon.CDK.AWS.SQS;
var queue = new Queue(this, "MyQueue");
var topicRule = new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'"),
Actions = new [] {
new SqsQueueAction(queue, new SqsQueueActionProps {
UseBase64 = true
}) }
});
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:
using Amazon.CDK.AWS.SNS;
var topic = new Topic(this, "MyTopic");
var topicRule = new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'"),
Actions = new [] {
new SnsTopicAction(topic, new SnsTopicActionProps {
MessageFormat = SnsActionMessageFormat.JSON
}) }
});
Classes
| CloudWatchLogsAction | (experimental) The action to send data to Amazon CloudWatch Logs. |
| CloudWatchLogsActionProps | (experimental) Configuration properties of an action for CloudWatch Logs. |
| CloudWatchPutMetricAction | (experimental) The action to capture an Amazon CloudWatch metric. |
| CloudWatchPutMetricActionProps | (experimental) Configuration properties of an action for CloudWatch metric. |
| CloudWatchSetAlarmStateAction | (experimental) The action to change the state of an Amazon CloudWatch alarm. |
| CloudWatchSetAlarmStateActionProps | (experimental) Configuration properties of an action for CloudWatch alarm. |
| CommonActionProps | (experimental) Common properties shared by Actions it access to AWS service. |
| FirehosePutRecordAction | (experimental) The action to put the record from an MQTT message to the Kinesis Data Firehose stream. |
| FirehosePutRecordActionProps | (experimental) Configuration properties of an action for the Kinesis Data Firehose stream. |
| FirehoseRecordSeparator | (experimental) Record Separator to be used to separate records. |
| IotRepublishMqttAction | (experimental) The action to put the record from an MQTT message to republish another MQTT topic. |
| IotRepublishMqttActionProps | (experimental) Configuration properties of an action to republish MQTT messages. |
| KinesisPutRecordAction | (experimental) The action to put the record from an MQTT message to the Kinesis Data stream. |
| KinesisPutRecordActionProps | (experimental) Configuration properties of an action for the Kinesis Data stream. |
| LambdaFunctionAction | (experimental) The action to invoke an AWS Lambda function, passing in an MQTT message. |
| MqttQualityOfService | (experimental) MQTT Quality of Service (QoS) indicates the level of assurance for delivery of an MQTT Message. |
| S3PutObjectAction | (experimental) The action to write the data from an MQTT message to an Amazon S3 bucket. |
| S3PutObjectActionProps | (experimental) Configuration properties of an action for s3. |
| SnsActionMessageFormat | (experimental) SNS topic action message format options. |
| SnsTopicAction | (experimental) The action to write the data from an MQTT message to an Amazon SNS topic. |
| SnsTopicActionProps | (experimental) Configuration options for the SNS topic action. |
| SqsQueueAction | (experimental) The action to write the data from an MQTT message to an Amazon SQS queue. |
| SqsQueueActionProps | (experimental) Configuration properties of an action for SQS. |
Interfaces
| ICloudWatchLogsActionProps | (experimental) Configuration properties of an action for CloudWatch Logs. |
| ICloudWatchPutMetricActionProps | (experimental) Configuration properties of an action for CloudWatch metric. |
| ICloudWatchSetAlarmStateActionProps | (experimental) Configuration properties of an action for CloudWatch alarm. |
| ICommonActionProps | (experimental) Common properties shared by Actions it access to AWS service. |
| IFirehosePutRecordActionProps | (experimental) Configuration properties of an action for the Kinesis Data Firehose stream. |
| IIotRepublishMqttActionProps | (experimental) Configuration properties of an action to republish MQTT messages. |
| IKinesisPutRecordActionProps | (experimental) Configuration properties of an action for the Kinesis Data stream. |
| IS3PutObjectActionProps | (experimental) Configuration properties of an action for s3. |
| ISnsTopicActionProps | (experimental) Configuration options for the SNS topic action. |
| ISqsQueueActionProps | (experimental) Configuration properties of an action for SQS. |