AWS IoT Events を使用した例 AWS CLI - AWS Command Line Interface

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS IoT Events を使用した例 AWS CLI

次のコード例は、 AWS Command Line Interface で を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています AWS IoT Events。

アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

各例には、完全なソースコードへのリンクが含まれています。ここでは、コンテキストでコードを設定および実行する方法の手順を確認できます。

トピック

アクション

次の例は、batch-put-message を使用する方法を説明しています。

AWS CLI

AWS IoT Events にメッセージ (入力) を送信するには

次のbatch-put-message例では、一連のメッセージを AWS IoT Events システムに送信します。各メッセージペイロードは、指定した入力 ( ) inputName に変換され、その入力を監視するディテクターに取り込まれます。複数のメッセージが送信された場合、メッセージが処理される順序は保証されません。注文を保証するには、メッセージを一度に 1 つずつ送信し、正常なレスポンスを待つ必要があります。

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 イベントAPIリファレンスBatchPutMessage」の「」を参照してください。

  • API 詳細については、AWS CLI 「 コマンドリファレンスBatchPutMessage」の「」を参照してください。

次の例は、batch-update-detector を使用する方法を説明しています。

AWS CLI

ディテクターを更新するには (インスタンス)

次のbatch-update-detector例では、指定されたディテクターモデル内の 1 つ以上のディテクター (インスタンス) の状態、変数値、タイマー設定を更新します。

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「」を参照してください。

  • API 詳細については、AWS CLI 「 コマンドリファレンス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「」を参照してください。

  • API 詳細については、AWS CLI 「 コマンドリファレンス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「」を参照してください。

  • API 詳細については、AWS CLI 「 コマンドリファレンス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「」を参照してください。

  • API 詳細については、AWS CLI 「 コマンドリファレンス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 イベント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 イベント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 イベントAPIリファレンスDescribeLoggingOptions」の「」を参照してください。

  • API 詳細については、AWS CLI 「 コマンドリファレンス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 イベントAPIリファレンスListDetectorModelVersions」の「」を参照してください。

  • API 詳細については、AWS CLI 「 コマンドリファレンス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 イベントAPIリファレンス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 API Reference 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 Reference 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 Reference ListTagsForResource」の「」を参照してください。

  • API 詳細については、AWS CLI 「 コマンドリファレンス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 フィールドの値を更新すると (無効なポリシーを修正するなど)、その変更が有効になるまでに最大 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 APIリファレンスのPutLoggingOptions「」を参照してください。

  • API 詳細については、AWS CLI 「 コマンドリファレンスPutLoggingOptions」の「」を参照してください。

次の例は、tag-resource を使用する方法を説明しています。

AWS CLI

リソースにタグを追加するには

次のtag-resource例では、指定されたリソースがアタッチされたタグを追加または変更します (キー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「」を参照してください。

  • API 詳細については、AWS CLI 「 コマンドリファレンス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」の「」を参照してください。