# @aws-cdk/aws-iot-actions-alpha 中文文档

规范地址：https://npmcn.com/package/%40aws-cdk/aws-iot-actions-alpha/

版本：2\.275\.0-alpha\.0
npm 元数据更新时间：2026-10-26T19:19:32.554Z
中文译文更新时间：2026-11-07T14:18:00.318Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40aws-cdk/aws-iot-actions-alpha
原文对照：https://npmcn.com/package/%40aws-cdk/aws-iot-actions-alpha/?lang=original
项目主页：https://github.com/aws/aws-cdk
源码仓库：https://github.com/aws/aws-cdk

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：627 kB，57 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>= 20\.0\.0
- 运行依赖：1 个
- peer 依赖：constructs ^10\.5\.0，aws-cdk-lib ^2\.275\.0，@aws-cdk/aws-iot-alpha 2\.275\.0-alpha\.0，@aws-cdk/aws-iotevents-alpha 2\.275\.0-alpha\.0
- 首次发布：2021-10-26T11:16:56.482Z
- 最新版本发布：2026-10-26T19:19:31.257Z
- 近 30 天下载量：22271（2026-10-08 至 2026-11-06，npm 官方下载统计）
- 已发布版本数：376（近 12 个月 64 个）

## 中文简介

AWS IOT 接收规则动作

## 安装

```sh
npm install @aws-cdk/aws-iot-actions-alpha
```

也可以使用：`pnpm add @aws-cdk/aws-iot-actions-alpha`、`yarn add @aws-cdk/aws-iot-actions-alpha`、`bun add @aws-cdk/aws-iot-actions-alpha`

## README

# AWS IOT 规则的行动

* * *

![cdk-constructs: Experimental](https://img.shields.io/badge/cdk--constructs-experimental-important.svg?style=for-the-badge)

> 本单元中更高层次的构件的API是实验性的并正在积极开发中. 在今后任何版本中,它们都须作非背向相容的修改或去除。 这些是 不适用 [语义版本](https://semver.org/) 模型和断开的更改 在发行笔记中宣布. 这意味着虽然你可能使用它们,但可能需要更新 当更新到此软件包的更新版本时,您的源代码.

* * *

此库包含集成类,以发送数据到任意数量 支持AWS服务. 这些班级的情况应传至 `TopicRule` 定义 `aws-cdk-lib/aws-iot`。 。 。 .

目前得到支持的有:

-   向另一个 MQTT 主题重现消息
-   启动 Lambda 函数
-   将对象放入 S3 桶
-   将日志放入云表日志
-   捕捉云表测量标准
-   更改云表提醒的状态
-   将记录放入 Kinesis 数据流
-   将记录放入 Amazon 数据 Firehose 流
-   向 SQS 队列发送信件
-   发布关于 SNS 主题的信息
-   将信件写入 DynamoDB 的列中
-   插入消息 IOT 事件输入
-   向 HTTPS 端点发送信件

## 向另一个 MQTT 主题重现消息

下面的代码片段创建了 AWS IOT 规则,将信件重印到 当 MQTT 主题被触发时 .

```ts
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
    }),
  ],
});
```

## 启动 Lambda 函数

下面的代码片段创建了 AWS IOT 规则,它引用了 Lambda 函数 当它被触发.

```ts
const func = new lambda.Function(this, 'MyFunction', {
  runtime: lambda.Runtime.NODEJS_LATEST,
  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)],
});
```

## 将对象放入 S3 桶

下面的代码片段创建 AWS IOT 规则,将对象放入 S3 桶 当它被触发.

```ts
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)],
});
```

财产 `key` 联 合 国 `S3PutObjectAction` 给出值 `${topic()}/${timestamp()}` 默认。 这个 `${topic()}` 财务报告和审定财务报表 `${timestamp()}` 称为替代模板。 更多信息见 [此文档](https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html)。 。 。 . 在以上样本中, `${topic()}` 被给定的 MQTT 主题替换为 `device/001/data`还有 `${timestamp()}` 替换 以毫秒为单位的当前时间戳数 `1636289461203`因此,如果 MQTT 经纪人收到 MQTT 话题 `device/001/data` 打开 `2021-11-07T00:00:00.000Z`S3桶对象将被放置到 `device/001/data/1636243200000`。 。 。 .

您也可以设置特定 `key` 现改为:

```ts
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)}',
    }),
  ],
});
```

如果您想要设置 S3 桶对象的访问控制, 您可以指定 `accessControl` 现改为:

```ts
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,
    }),
  ],
});
```

## 将日志放入云表日志

下面的代码片段创建了 AWS IOT 规则,将日志放入 Cloud Watch Logs 当它被触发.

```ts
import * as logs from 'aws-cdk-lib/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)],
});
```

## 捕捉云表测量标准

下面的代码片段创建了 AWS IOT 规则,用于捕捉 Cloud Watch 度量衡 当它被触发.

```ts
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}',
    }),
  ],
});
```

## 启动 Step 函数状态机器

下面的代码片段创建了 AWS IOT 规则,启动 Step 函数状态机器 当它被触发.

```ts
const stateMachine = new stepfunctions.StateMachine(this, 'SM', {
  definitionBody: stepfunctions.DefinitionBody.fromChainable(new stepfunctions.Wait(this, 'Hello', { time: stepfunctions.WaitTime.duration(Duration.seconds(10)) })),
});

new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323("SELECT * FROM 'device/+/data'"),
  actions: [
    new actions.StepFunctionsStateMachineAction(stateMachine),
  ],
});
```

## 更改亚马逊云表提醒的状态

下面的代码片段创建了 AWS IOT 规则, 当亚马逊云表提醒被触发时会改变其状态 :

```ts
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';

const metric = new cloudwatch.Metric({
  namespace: 'MyNamespace',
  metricName: 'MyMetric',
  dimensionsMap: { 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,
    }),
  ],
});
```

## 将记录放入 Kinesis 数据流

下面的代码片段创建了 AWS IOT 规则,将记录放入 Kinesis 数据 当它被触发时会流出.

```ts
import * as kinesis from 'aws-cdk-lib/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()}',
    }),
  ],
});
```

## 将记录放入 Amazon 数据 Firehose 流

下面的代码片段创建 AWS IOT 规则,将记录放入记录 当它被触发时,它会变成亚马逊数据Firehose流.

```ts
import * as firehose from 'aws-cdk-lib/aws-kinesisfirehose';

const bucket = new s3.Bucket(this, 'MyBucket');
const stream = new firehose.DeliveryStream(this, 'MyStream', {
  destination: new firehose.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,
    }),
  ],
});
```

## 将信件发送到 SQS 队列

下面的代码片段创建了发送消息的 AWS IOT 规则 当SQS队列被触发时 :

```ts
import * as sqs from 'aws-cdk-lib/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'
    }),
  ],
});
```

## 就 SNS 专题发布信件

下面的代码片段创建和AWS IOT规则,当SNS主题被触发时发布消息:

```ts
import * as sns from 'aws-cdk-lib/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
    }),
  ],
});
```

## 将信件的属性写入 DynamoDB

下面的代码片段创建了 AWS IOT 规则,用于写入全部或部分 使用 DynamoDBv2 动作向 DynamoDB 发送 MQTT 消息.

```ts
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';

declare const table: dynamodb.Table;

const topicRule = new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323(
    "SELECT * FROM 'device/+/data'",
  ),
  actions: [
    new actions.DynamoDBv2PutItemAction(table)
  ],
});
```

## 插入消息 IOT 事件输入

下面的代码片段创建了 AWS IOT 规则,将消息放入 当IOT 事件被触发时, 向 IOT 事件输入:

```ts
import * as iotevents from '@aws-cdk/aws-iotevents-alpha';
import * as iam from 'aws-cdk-lib/aws-iam';

declare const role: iam.IRole;

const input = new iotevents.Input(this, 'MyInput', {
  attributeJsonPaths: ['payload.temperature', 'payload.transactionId'],
});
const topicRule = new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323(
    "SELECT * FROM 'device/+/data'",
  ),
  actions: [
    new actions.IotEventsPutMessageAction(input, {
      batchMode: true, // optional property, default is 'false'
      messageId: '${payload.transactionId}', // optional property, default is a new UUID
      role: role, // optional property, default is a new UUID
    }),
  ],
});
```

## 向 HTTPS 端点发送信件

下面的代码片段创建了发送消息的 AWS IOT 规则 当触发时, 到 HTTPS 端点 :

```ts
const topicRule = new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323(
    "SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'",
  ),
});

topicRule.addAction(
  new actions.HttpsAction('https://example.com/endpoint', {
    confirmationUrl: 'https://example.com',
    headers: [
      { key: 'key0', value: 'value0' },
      { key: 'key1', value: 'value1' },
    ],
    auth: { serviceName: 'serviceName', signingRegion: 'us-east-1' },
  }),
);
```

可以使批量工作降低成本并提高效率:

```ts
import { Size } from 'aws-cdk-lib';

declare const topicRule: iot.TopicRule;

topicRule.addAction(
  new actions.HttpsAction('https://example.com/endpoint', {
    batchConfig: {
      maxBatchOpenDuration: Duration.millis(100),
      maxBatchSize: 5,
      maxBatchSizeBytes: Size.kibibytes(1),
    },
  }),
);
```

有关批量配置的更多信息,请参见: [AWS IOT 核心文件](https://docs.aws.amazon.com/iot/latest/developerguide/http_batching.html)。 。 。 .

## 将数据写入打开搜索服务

下面的代码片段创建 AWS IOT 规则来写入数据 打开搜索服务,当它被触发时:

```ts
import * as opensearch from 'aws-cdk-lib/aws-opensearchservice';
declare const domain: opensearch.Domain;

const topicRule = new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323(
    "SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'",
  ),
});

topicRule.addAction(new actions.OpenSearchAction(domain, {
  id: 'my-id',
  index: 'my-index',
  type: 'my-type',
}));
```

## 常见问题

### @aws-cdk/aws-iot-actions-alpha 是什么？

AWS IOT 接收规则动作

### 如何安装 @aws-cdk/aws-iot-actions-alpha？

在项目目录运行 npm install @aws-cdk/aws-iot-actions-alpha，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：constructs（^10\.5\.0）、aws-cdk-lib（^2\.275\.0）、@aws-cdk/aws-iot-alpha（2\.275\.0-alpha\.0）、@aws-cdk/aws-iotevents-alpha（2\.275\.0-alpha\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @aws-cdk/aws-iot-actions-alpha 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @aws-cdk/aws-iot-actions-alpha 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @aws-cdk/aws-iot-actions-alpha 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>= 20\.0\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @aws-cdk/aws-iot-actions-alpha 可以商用吗？

@aws-cdk/aws-iot-actions-alpha 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @aws-cdk/aws-iot-actions-alpha 的下载量有多少？

根据 npm 官方下载统计，@aws-cdk/aws-iot-actions-alpha 在 2026/10/08 – 2026/11/06 这 30 天内被下载 22,271 次，日均约 742 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @aws-cdk/aws-iot-actions-alpha 还在维护吗？

最新版本 2\.275\.0-alpha\.0 发布于 2026/10/27（12 天前）；至今共发布 376 个版本，其中过去 12 个月发布了 64 个；首次发布于 2021/10/26。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

