AWS IoT Events 範例使用 AWS CLI - AWS Command Line Interface

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

AWS IoT Events 範例使用 AWS CLI

下列程式碼範例說明如何使用 AWS Command Line Interface 與來執行動作及實作常見案例 AWS IoT Events。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境和跨服務範例中查看內容中的動作。

Scenarios (案例) 是向您展示如何呼叫相同服務中的多個函數來完成特定任務的程式碼範例。

每個範例都包含一個連結 GitHub,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例會示範如何使用batch-put-message

AWS CLI

將訊息 (輸入) 傳送至 AWS IoT Events

下列batch-put-message範例會將一組訊息傳送至 AWS IoT Events 系統。每個消息有效負載都轉換為您指定的輸入(inputName),並導入到監視該輸入的任何檢測器中。如果傳送多個郵件,則無法保證郵件的處理順序。為了保證訂購,您必須一次發送一個消息並等待成功響應。

aws iotevents-data batch-put-message \ --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 API 參考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 API 參考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 API 參考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 API 參考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 API 參考DeleteDetectorModel中的。

下列程式碼範例會示範如何使用delete-input

AWS CLI

刪除輸入的步驟

下列delete-input範例會刪除指定的輸入。

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

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IoT Events API 參考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 API 參考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 API 參考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 API 參考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 API 參考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 API 參考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 API 參考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 API 參考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 API 參考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 API 參考ListTagsForResource中的。

下列程式碼範例會示範如何使用put-logging-options

AWS CLI

若要設定記錄選項

下列put-logging-options範例會設定或更新 AWS IoT Events 記錄選項。如果您更新任何loggingOptions` field, it can take up to one minute for the change to take effect. Also, if you change the policy attached to the role you specified in the ``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" } ] } }

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IoT Events API 參考PutLoggingOptions中的。

下列程式碼範例會示範如何使用tag-resource

AWS CLI

若要將標籤新增至資源

下面的tag-resource例子添加或修改(如果 key deviceType 已經存在)附加指定資源的標籤。

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 API 參考TagResource中的。

  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考TagResource中的。

下列程式碼範例會示範如何使用untag-resource

AWS CLI

若要從資源中移除標籤

下列untag-resource範例會從指定的資源中移除具有指定索引鍵名稱的標籤。

aws iotevents untag-resource \ --resource-arn arn:aws:iotevents:us-west-2:123456789012:input/PressureInput \ --tagkeys deviceType

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IoT Events API 參考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 API 參考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 API 參考UpdateInput中的。

  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考UpdateInput中的。