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

使用 AWS CLI 的 AWS IoT Events-Data示例

以下代码示例演示了如何通过将 AWS Command Line Interface与 AWS IoT Events-Data 结合使用,来执行操作和实现常见场景。

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

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

主题

操作

以下代码示例演示了如何使用 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": [] }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 BatchPutMessage

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 BatchPutMessage

以下代码示例演示了如何使用 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": [] }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 BatchUpdateDetector

以下代码示例演示了如何使用 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" } }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 CreateDetectorModel

以下代码示例演示了如何使用 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" } }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 CreateInput

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 CreateInput

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

AWS CLI

删除检测器模型

以下 delete-detector-model 示例删除了一个检测器模型。该检测器模型的所有活动实例也被删除。

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

此命令不生成任何输出。

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 DeleteDetectorModel

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

AWS CLI

删除输入

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

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

此命令不生成任何输出。

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 DeleteInput

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 DeleteInput

以下代码示例演示了如何使用 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" } } }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 DescribeDetectorModel

以下代码示例演示了如何使用 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" } }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 DescribeDetector

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 DescribeDetector

以下代码示例演示了如何使用 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" } ] } } }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 DescribeInput

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 DescribeInput

以下代码示例演示了如何使用 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" } }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 DescribeLoggingOptions

以下代码示例演示了如何使用 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" } ] }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 ListDetectorModelVersions

以下代码示例演示了如何使用 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." } ] }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 ListDetectorModels

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 ListDetectorModels

以下代码示例演示了如何使用 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" } ] }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 ListDetectors

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 ListDetectors

以下代码示例演示了如何使用 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" }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 ListInputs

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 ListInputs

以下代码示例演示了如何使用 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" } ] }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 ListTagsForResource

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

AWS CLI

设置日志选项

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

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" } ] } }

此命令不生成任何输出。

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 PutLoggingOptions

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 PutLoggingOptions

以下代码示例演示了如何使用 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" } ] }

此命令不生成任何输出。

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 TagResource

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 TagResource

以下代码示例演示了如何使用 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" ] }

此命令不生成任何输出。

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 UntagResource

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 UntagResource

以下代码示例演示了如何使用 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" } }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 UpdateDetectorModel

以下代码示例演示了如何使用 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" } }

有关更多信息,请参阅《AWS IoT Events 开发人员指南》*中的 UpdateInput

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 UpdateInput