1.204.0 • Published 11 months ago

@aws-cdk/aws-iot-actions v1.204.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
11 months ago

Actions for AWS IoT Rule


End-of-Support

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.

doc: https://docs.aws.amazon.com/cdk/v2/guide/migrating-v2.html


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
  • 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

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 iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'"),
  actions: [
    new actions.IotRepublishMqttAction('${topic()}/republish', {
      qualityOfService: actions.MqttQualityOfService.AT_LEAST_ONCE, // optional property, default is MqttQualityOfService.ZERO_OR_MORE_TIMES
    }),
  ],
});

Invoke a Lambda function

The code snippet below creates an AWS IoT Rule that invoke a Lambda function when it is triggered.

const func = new lambda.Function(this, 'MyFunction', {
  runtime: lambda.Runtime.NODEJS_14_X,
  handler: 'index.handler',
  code: lambda.Code.fromInline(`
    exports.handler = (event) => {
      console.log("It is test for lambda action of AWS IoT Rule.", event);
    };`
  ),
});

new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'"),
  actions: [new actions.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.

const bucket = new s3.Bucket(this, 'MyBucket');

new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
  actions: [new actions.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:

const bucket = new s3.Bucket(this, 'MyBucket');

new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323(
    "SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'",
  ),
  actions: [
    new actions.S3PutObjectAction(bucket, {
      key: '${year}/${month}/${day}/${topic(2)}',
    }),
  ],
});

If you wanna set access control to the S3 bucket object, you can specify accessControl as following:

const bucket = new s3.Bucket(this, 'MyBucket');

new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323("SELECT * FROM 'device/+/data'"),
  actions: [
    new actions.S3PutObjectAction(bucket, {
      accessControl: s3.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.

import * as logs from '@aws-cdk/aws-logs';

const logGroup = new logs.LogGroup(this, 'MyLogGroup');

new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
  actions: [new actions.CloudWatchLogsAction(logGroup)],
});

Capture CloudWatch metrics

The code snippet below creates an AWS IoT Rule that capture CloudWatch metrics when it is triggered.

const topicRule = new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323(
    "SELECT topic(2) as device_id, namespace, unit, value, timestamp FROM 'device/+/data'",
  ),
  actions: [
    new actions.CloudWatchPutMetricAction({
      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:

import * as cloudwatch from '@aws-cdk/aws-cloudwatch';

const metric = new cloudwatch.Metric({
  namespace: 'MyNamespace',
  metricName: 'MyMetric',
  dimensions: { MyDimension: 'MyDimensionValue' },
});
const alarm = new cloudwatch.Alarm(this, 'MyAlarm', {
  metric: metric,
  threshold: 100,
  evaluationPeriods: 3,
  datapointsToAlarm: 2,
});

const topicRule = new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
  actions: [
    new actions.CloudWatchSetAlarmStateAction(alarm, {
      reason: 'AWS Iot Rule action is triggered',
      alarmStateToSet: cloudwatch.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.

import * as kinesis from '@aws-cdk/aws-kinesis';

const stream = new kinesis.Stream(this, 'MyStream');

const topicRule = new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323("SELECT * FROM 'device/+/data'"),
  actions: [
    new actions.KinesisPutRecordAction(stream, {
      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.

import * as firehose from '@aws-cdk/aws-kinesisfirehose';
import * as destinations from '@aws-cdk/aws-kinesisfirehose-destinations';

const bucket = new s3.Bucket(this, 'MyBucket');
const stream = new firehose.DeliveryStream(this, 'MyStream', {
  destinations: [new destinations.S3Bucket(bucket)],
});

const topicRule = new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323("SELECT * FROM 'device/+/data'"),
  actions: [
    new actions.FirehosePutRecordAction(stream, {
      batchMode: true,
      recordSeparator: actions.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:

import * as sqs from '@aws-cdk/aws-sqs';

const queue = new sqs.Queue(this, 'MyQueue');

const topicRule = new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323(
    "SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'",
  ),
  actions: [
    new actions.SqsQueueAction(queue, {
      useBase64: true, // optional property, default is 'false'
    }),
  ],
});

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 * as sns from '@aws-cdk/aws-sns';

const topic = new sns.Topic(this, 'MyTopic');

const topicRule = new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323(
    "SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'",
  ),
  actions: [
    new actions.SnsTopicAction(topic, {
      messageFormat: actions.SnsActionMessageFormat.JSON, // optional property, default is SnsActionMessageFormat.RAW
    }),
  ],
});
1.203.0

11 months ago

1.204.0

11 months ago

1.201.0

12 months ago

1.199.0

1 year ago

1.202.0

12 months ago

1.200.0

1 year ago

1.195.0

1 year ago

1.193.0

1 year ago

1.197.0

1 year ago

1.192.0

1 year ago

1.196.0

1 year ago

1.194.0

1 year ago

1.198.1

1 year ago

1.198.0

1 year ago

1.185.0

1 year ago

1.189.0

1 year ago

1.187.0

1 year ago

1.191.0

1 year ago

1.188.0

1 year ago

1.186.0

1 year ago

1.186.1

1 year ago

1.190.0

1 year ago

1.181.0

1 year ago

1.183.0

1 year ago

1.181.1

1 year ago

1.178.0

2 years ago

1.180.0

2 years ago

1.184.0

1 year ago

1.184.1

1 year ago

1.182.0

1 year ago

1.177.0

2 years ago

1.179.0

2 years ago

1.176.0

2 years ago

1.175.0

2 years ago

1.170.0

2 years ago

1.174.0

2 years ago

1.172.0

2 years ago

1.170.1

2 years ago

1.169.0

2 years ago

1.173.0

2 years ago

1.171.0

2 years ago

1.163.0

2 years ago

1.162.0

2 years ago

1.161.0

2 years ago

1.160.0

2 years ago

1.167.0

2 years ago

1.166.1

2 years ago

1.165.0

2 years ago

1.163.2

2 years ago

1.164.0

2 years ago

1.163.1

2 years ago

1.168.0

2 years ago

1.158.0

2 years ago

1.159.0

2 years ago

1.154.0

2 years ago

1.155.0

2 years ago

1.156.0

2 years ago

1.157.0

2 years ago

1.156.1

2 years ago

1.148.0

2 years ago

1.149.0

2 years ago

1.153.0

2 years ago

1.153.1

2 years ago

1.150.0

2 years ago

1.151.0

2 years ago

1.152.0

2 years ago

1.146.0

2 years ago

1.147.0

2 years ago

1.142.0

2 years ago

1.143.0

2 years ago

1.144.0

2 years ago

1.145.0

2 years ago

1.140.0

2 years ago

1.141.0

2 years ago

1.138.2

2 years ago

1.139.0

2 years ago

1.138.1

2 years ago

1.136.0

2 years ago

1.135.0

2 years ago

1.138.0

2 years ago

1.137.0

2 years ago

1.132.0

2 years ago

1.131.0

2 years ago

1.134.0

2 years ago

1.133.0

2 years ago

1.130.0

3 years ago