AWS IoT Events-Data 使用示例 AWS CLI - AWS Command Line Interface

本文档 AWS CLI 仅适用于版本 1。有关版本 2 的文档 AWS CLI,请参阅版本 2 用户指南

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

AWS IoT Events-Data 使用示例 AWS CLI

以下代码示例向您展示了如何使用with来执行操作和实现常见场景 AWS IoT Events-Data。 AWS Command Line Interface

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例显示了如何使用batch-put-message

AWS CLI

向 AWS IoT Events 发送消息(输入)

以下batch-put-message示例向 AWS IoT Events 系统发送了一组消息。每个消息有效载荷都将转换为您指定的输入 (inputName),并输入到任何监视该输入的检测器中。如果发送了多条消息,则无法保证消息的处理顺序。为了保证顺序,您必须逐一发送一条消息,然后等待成功响应。

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

highPressureMessage.json 的内容:

{ "messages": [ { "messageId": "00001", "inputName": "PressureInput", "payload": "{\"motorid\": \"Fulton-A32\", \"sensorData\": {\"pressure\": 80, \"temperature\": 39} }" } ] }

输出:

{ "BatchPutMessageErrorEntries": [] }

有关更多信息,请参阅 BatchPutMessage AWSIoT Events 开发者指南*

以下代码示例显示了如何使用batch-update-detector

AWS CLI

更新探测器(实例)

以下batch-update-detector示例更新了指定探测器模型的一个或多个探测器(实例)的状态、变量值和计时器设置。

aws iotevents-data batch-update-detector \ --cli-input-json file://budFulton-A32.json

budFulton-A32.json 的内容:

{ "detectors": [ { "messageId": "00001", "detectorModelName": "motorDetectorModel", "keyValue": "Fulton-A32", "state": { "stateName": "Normal", "variables": [ { "name": "pressureThresholdBreached", "value": "0" } ], "timers": [ ] } } ] }

输出:

{ "batchUpdateDetectorErrorEntries": [] }

有关更多信息,请参阅 BatchUpdateDetector AWSIoT Events 开发者指南*

以下代码示例显示了如何使用create-detector-model

AWS CLI

创建探测器模型

以下create-detector-model示例创建探测器模型。

aws iotevents create-detector-model \ --cli-input-json file://motorDetectorModel.json

motorDetectorModel.json 的内容:

{ "detectorModelName": "motorDetectorModel", "detectorModelDefinition": { "states": [ { "stateName": "Normal", "onEnter": { "events": [ { "eventName": "init", "condition": "true", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "0" } } ] } ] }, "onInput": { "transitionEvents": [ { "eventName": "Overpressurized", "condition": "$input.PressureInput.sensorData.pressure > 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached + 3" } } ], "nextState": "Dangerous" } ] } }, { "stateName": "Dangerous", "onEnter": { "events": [ { "eventName": "Pressure Threshold Breached", "condition": "$variable.pressureThresholdBreached > 1", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:underPressureAction" } } ] } ] }, "onInput": { "events": [ { "eventName": "Overpressurized", "condition": "$input.PressureInput.sensorData.pressure > 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "3" } } ] }, { "eventName": "Pressure Okay", "condition": "$input.PressureInput.sensorData.pressure <= 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached - 1" } } ] } ], "transitionEvents": [ { "eventName": "BackToNormal", "condition": "$input.PressureInput.sensorData.pressure <= 70 && $variable.pressureThresholdBreached <= 1", "nextState": "Normal" } ] }, "onExit": { "events": [ { "eventName": "Normal Pressure Restored", "condition": "true", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:pressureClearedAction" } } ] } ] } } ], "initialStateName": "Normal" }, "key": "motorid", "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole" }

输出:

{ "detectorModelConfiguration": { "status": "ACTIVATING", "lastUpdateTime": 1560796816.077, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "creationTime": 1560796816.077, "detectorModelArn": "arn:aws:iotevents:us-west-2:123456789012:detectorModel/motorDetectorModel", "key": "motorid", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" } }

有关更多信息,请参阅 CreateDetectorModel AWSIoT Events 开发者指南*

以下代码示例显示了如何使用create-input

AWS CLI

创建输入

以下create-input示例创建了一个输入。

aws iotevents create-input \ --cli-input-json file://pressureInput.json

pressureInput.json 的内容:

{ "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor", "inputDefinition": { "attributes": [ { "jsonPath": "sensorData.pressure" }, { "jsonPath": "motorid" } ] } }

输出:

{ "inputConfiguration": { "status": "ACTIVE", "inputArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "lastUpdateTime": 1560795312.542, "creationTime": 1560795312.542, "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor" } }

有关更多信息,请参阅 CreateInput AWSIoT Events 开发者指南*

以下代码示例显示了如何使用delete-detector-model

AWS CLI

删除探测器模型

以下delete-detector-model示例删除探测器模型。探测器模型的所有活动实例也将被删除。

aws iotevents delete-detector-model \ --detector-model-name motorDetectorModel*

此命令不生成任何输出。

有关更多信息,请参阅 DeleteDetectorModel AWSIoT Events 开发者指南*

以下代码示例显示了如何使用delete-input

AWS CLI

删除输入

以下delete-input示例删除了输入。

aws iotevents delete-input \ --input-name PressureInput

此命令不生成任何输出。

有关更多信息,请参阅 DeleteInput AWSIoT Events 开发者指南*

以下代码示例显示了如何使用describe-detector-model

AWS CLI

获取有关探测器模型的信息

以下describe-detector-model示例描述了探测器模型。如果未指定该version参数,则该命令将返回有关最新版本的信息。

aws iotevents describe-detector-model \ --detector-model-name motorDetectorModel

输出:

{ "detectorModel": { "detectorModelConfiguration": { "status": "ACTIVE", "lastUpdateTime": 1560796816.077, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "creationTime": 1560796816.077, "detectorModelArn": "arn:aws:iotevents:us-west-2:123456789012:detectorModel/motorDetectorModel", "key": "motorid", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" }, "detectorModelDefinition": { "states": [ { "onInput": { "transitionEvents": [ { "eventName": "Overpressurized", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached + 3" } } ], "condition": "$input.PressureInput.sensorData.pressure > 70", "nextState": "Dangerous" } ], "events": [] }, "stateName": "Normal", "onEnter": { "events": [ { "eventName": "init", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "0" } } ], "condition": "true" } ] }, "onExit": { "events": [] } }, { "onInput": { "transitionEvents": [ { "eventName": "BackToNormal", "actions": [], "condition": "$input.PressureInput.sensorData.pressure <= 70 && $variable.pressureThresholdBreached <= 1", "nextState": "Normal" } ], "events": [ { "eventName": "Overpressurized", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "3" } } ], "condition": "$input.PressureInput.sensorData.pressure > 70" }, { "eventName": "Pressure Okay", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached - 1" } } ], "condition": "$input.PressureInput.sensorData.pressure <= 70" } ] }, "stateName": "Dangerous", "onEnter": { "events": [ { "eventName": "Pressure Threshold Breached", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:underPressureAction" } } ], "condition": "$variable.pressureThresholdBreached > 1" } ] }, "onExit": { "events": [ { "eventName": "Normal Pressure Restored", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:pressureClearedAction" } } ], "condition": "true" } ] } } ], "initialStateName": "Normal" } } }

有关更多信息,请参阅 DescribeDetectorModel AWSIoT Events 开发者指南*

以下代码示例显示了如何使用describe-detector

AWS CLI

获取有关探测器(实例)的信息

以下describe-detector示例返回有关指定检测器(实例)的信息。

aws iotevents-data describe-detector \ --detector-model-name motorDetectorModel \ --key-value "Fulton-A32"

输出:

{ "detector": { "lastUpdateTime": 1560797852.776, "creationTime": 1560797852.775, "state": { "variables": [ { "name": "pressureThresholdBreached", "value": "3" } ], "stateName": "Dangerous", "timers": [] }, "keyValue": "Fulton-A32", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" } }

有关更多信息,请参阅 DescribeDetector AWSIoT Events 开发者指南*

以下代码示例显示了如何使用describe-input

AWS CLI

获取有关输入的信息

以下describe-input示例检索输入的详细信息。

aws iotevents describe-input \ --input-name PressureInput

输出:

{ "input": { "inputConfiguration": { "status": "ACTIVE", "inputArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "lastUpdateTime": 1560795312.542, "creationTime": 1560795312.542, "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor" }, "inputDefinition": { "attributes": [ { "jsonPath": "sensorData.pressure" }, { "jsonPath": "motorid" } ] } } }

有关更多信息,请参阅 DescribeInput AWSIoT Events 开发者指南*

以下代码示例显示了如何使用describe-logging-options

AWS CLI

获取有关日志设置的信息

以下describe-logging-options示例检索当前的 AWS IoT Events 日志选项。

aws iotevents describe-logging-options

输出:

{ "loggingOptions": { "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "enabled": false, "level": "ERROR" } }

有关更多信息,请参阅 DescribeLoggingOptions AWSIoT Events 开发者指南*

以下代码示例显示了如何使用list-detector-model-versions

AWS CLI

获取有关探测器模型版本的信息

以下list-detector-model-versions示例列出了探测器模型的所有版本。仅返回与每个检测器模型版本关联的元数据。

aws iotevents list-detector-model-versions \ --detector-model-name motorDetectorModel

输出:

{ "detectorModelVersionSummaries": [ { "status": "ACTIVE", "lastUpdateTime": 1560796816.077, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "creationTime": 1560796816.077, "detectorModelArn": "arn:aws:iotevents:us-west-2:123456789012:detectorModel/motorDetectorModel", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" } ] }

有关更多信息,请参阅 ListDetectorModelVersions AWSIoT Events 开发者指南*

以下代码示例显示了如何使用list-detector-models

AWS CLI

获取您的探测器型号列表

以下list-detector-models示例列出了您创建的探测器模型。仅返回与每个检测器模型关联的元数据。

aws iotevents list-detector-models

输出:

{ "detectorModelSummaries": [ { "detectorModelName": "motorDetectorModel", "creationTime": 1552072424.212 "detectorModelDescription": "Detect overpressure in a motor." } ] }

有关更多信息,请参阅 ListDetectorModels AWSIoT Events 开发者指南*

以下代码示例显示了如何使用list-detectors

AWS CLI

获取探测器型号的探测器列表

以下list-detectors示例列出了探测器(探测器模型的实例)。

aws iotevents-data list-detectors \ --detector-model-name motorDetectorModel

输出:

{ "detectorSummaries": [ { "lastUpdateTime": 1558129925.2, "creationTime": 1552073155.527, "state": { "stateName": "Normal" }, "keyValue": "Fulton-A32", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" } ] }

有关更多信息,请参阅 ListDetectors AWSIoT Events 开发者指南*

以下代码示例显示了如何使用list-inputs

AWS CLI

列出输入

以下list-inputs示例列出了您创建的输入。

aws iotevents list-inputs

输出:

{ "status": "ACTIVE", "inputArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "lastUpdateTime": 1551742986.768, "creationTime": 1551742986.768, "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor" }

有关更多信息,请参阅 ListInputs AWSIoT Events 开发者指南*

以下代码示例显示了如何使用list-tags-for-resource

AWS CLI

列出分配给资源的标签

以下list-tags-for-resource示例列出了您为资源分配的标签(元数据)。

aws iotevents list-tags-for-resource \ --resource-arn "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput"

输出:

{ "tags": [ { "value": "motor", "key": "deviceType" } ] }

有关更多信息,请参阅 ListTagsForResource AWSIoT Events 开发者指南*

以下代码示例显示了如何使用put-logging-options

AWS CLI

设置日志选项

以下list-tags-for-resource示例设置或更新 AWS IoT Events 日志选项。如果您更新任何 loggingOptions 字段的值,则最多需要一分钟,更改才能生效。此外,如果您更改附加到您在roleArn字段中指定的角色的策略(例如,更正无效的策略),则该更改最多需要五分钟才能生效。

aws iotevents put-logging-options \ --cli-input-json file://logging-options.json

logging-options.json 的内容:

{ "loggingOptions": { "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "level": "DEBUG", "enabled": true, "detectorDebugOptions": [ { "detectorModelName": "motorDetectorModel", "keyValue": "Fulton-A32" } ] } }

此命令不生成任何输出。

有关更多信息,请参阅 PutLoggingOptions AWSIoT Events 开发者指南*

以下代码示例显示了如何使用tag-resource

AWS CLI

为资源添加标签

以下tag-resource示例添加或修改给定资源的标签。标签是可用于管理资源的元数据。

aws iotevents tag-resource \ --cli-input-json file://pressureInput.tag.json

pressureInput.tag.json 的内容:

{ "resourceArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "tags": [ { "key": "deviceType", "value": "motor" } ] }

此命令不生成任何输出。

有关更多信息,请参阅 TagResource AWSIoT Events 开发者指南*

以下代码示例显示了如何使用untag-resource

AWS CLI

要从资源中删除标签

以下untag-resource示例从资源中移除指定的标签。

aws iotevents untag-resource \ --cli-input-json file://pressureInput.untag.json

pressureInput.untag.json 的内容:

{ "resourceArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "tagKeys": [ "deviceType" ] }

此命令不生成任何输出。

有关更多信息,请参阅 UntagResource AWSIoT Events 开发者指南*

以下代码示例显示了如何使用update-detector-model

AWS CLI

更新探测器模型

以下update-detector-model示例更新探测器模型。先前版本生成的探测器(实例)会被删除,然后在新输入到来时重新创建。

aws iotevents update-detector-model \ --cli-input-json file://motorDetectorModel.update.json

motorDetectorModel.update.json 的内容:

{ "detectorModelName": "motorDetectorModel", "detectorModelDefinition": { "states": [ { "stateName": "Normal", "onEnter": { "events": [ { "eventName": "init", "condition": "true", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "0" } } ] } ] }, "onInput": { "transitionEvents": [ { "eventName": "Overpressurized", "condition": "$input.PressureInput.sensorData.pressure > 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached + 3" } } ], "nextState": "Dangerous" } ] } }, { "stateName": "Dangerous", "onEnter": { "events": [ { "eventName": "Pressure Threshold Breached", "condition": "$variable.pressureThresholdBreached > 1", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:underPressureAction" } } ] } ] }, "onInput": { "events": [ { "eventName": "Overpressurized", "condition": "$input.PressureInput.sensorData.pressure > 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "3" } } ] }, { "eventName": "Pressure Okay", "condition": "$input.PressureInput.sensorData.pressure <= 70", "actions": [ { "setVariable": { "variableName": "pressureThresholdBreached", "value": "$variable.pressureThresholdBreached - 1" } } ] } ], "transitionEvents": [ { "eventName": "BackToNormal", "condition": "$input.PressureInput.sensorData.pressure <= 70 && $variable.pressureThresholdBreached <= 1", "nextState": "Normal" } ] }, "onExit": { "events": [ { "eventName": "Normal Pressure Restored", "condition": "true", "actions": [ { "sns": { "targetArn": "arn:aws:sns:us-east-1:123456789012:pressureClearedAction" } } ] } ] } } ], "initialStateName": "Normal" }, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole" }

输出:

{ "detectorModelConfiguration": { "status": "ACTIVATING", "lastUpdateTime": 1560799387.719, "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "creationTime": 1560799387.719, "detectorModelArn": "arn:aws:iotevents:us-west-2:123456789012:detectorModel/motorDetectorModel", "key": "motorid", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "2" } }

有关更多信息,请参阅 UpdateDetectorModel AWSIoT Events 开发者指南*

以下代码示例显示了如何使用update-input

AWS CLI

更新输入

以下update-input示例更新输入。

aws iotevents update-input \ --cli-input-json file://pressureInput.json

pressureInput.json 的内容:

{ "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor", "inputDefinition": { "attributes": [ { "jsonPath": "sensorData.pressure" }, { "jsonPath": "motorid" } ] } }

输出:

{ "inputConfiguration": { "status": "ACTIVE", "inputArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "lastUpdateTime": 1560795976.458, "creationTime": 1560795312.542, "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor" } }

有关更多信息,请参阅 UpdateInput AWSIoT Events 开发者指南*