

支援終止通知：2026 年 5 月 20 日， AWS 將終止對 的支援 AWS IoT Events。2026 年 5 月 20 日之後，您將無法再存取 AWS IoT Events 主控台或 AWS IoT Events 資源。如需詳細資訊，請參閱[AWS IoT Events 終止支援](https://docs.aws.amazon.com/iotevents/latest/developerguide/iotevents-end-of-support.html)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 中擷取 MQTT 訊息 AWS IoT Events
<a name="iotevents-commented-example-ingest-mqtt"></a>

如果您的感應器運算資源無法使用 `"BatchPutMessage"` API，但可以使用輕量型 MQTT 用戶端將其資料傳送至 AWS IoT Core 訊息中介裝置，您可以建立 AWS IoT Core 主題規則，將訊息資料重新導向至 AWS IoT Events 輸入。以下是 AWS IoT Events 主題規則的定義，該規則會從 MQTT 主題取得 `"areaId"`和 `"sensorId"`輸入欄位，並從訊息承載`"temp"`欄位取得 `"sensorData.temperature"` 欄位，並將這些資料擷取到我們的 中 AWS IoT Events `"temperatureInput"`。

CLI 命令：

```
aws iot create-topic-rule --cli-input-json file://temperatureTopicRule.json
```

檔案： `seedSetDesiredTemp.json`

```
{
  "ruleName": "temperatureTopicRule",
  "topicRulePayload": {
    "sql": "SELECT topic(3) as areaId, topic(4) as sensorId, temp as sensorData.temperature FROM 'update/temperature/#'",
    "description": "Ingest temperature sensor messages into IoT Events",
    "actions": [
      {
        "iotEvents": {
          "inputName": "temperatureInput",
          "roleArn": "arn:aws:iam::123456789012:role/service-role/anotheRole" 
        }
      }
    ],
    "ruleDisabled": false,
    "awsIotSqlVersion": "2016-03-23"
  }
}
```

回應：【無】

如果感應器傳送有關 主題的訊息`"update/temperature/Area51/03"`，其中包含下列承載。

```
{ "temp": 24.5 }
```

這會導致資料擷取至 AWS IoT Events ，如同已進行下列 `"BatchPutMessage"` API 呼叫一樣。

```
aws iotevents-data batch-put-message --cli-input-json file://spoofExample.json --cli-binary-format raw-in-base64-out
```

檔案： `spoofExample.json`

```
{
  "messages": [
    {
      "messageId": "54321",
      "inputName": "temperatureInput",
      "payload": "{\"sensorId\": \"03\", \"areaId\": \"Area51\", \"sensorData\": {\"temperature\": 24.5} }"
    }
  ]
}
```