There are more AWS SDK examples available in the AWS Doc SDK Examples
AWS IoT Events examples using AWS CLI
The following code examples show you how to perform actions and implement common scenarios by using the AWS Command Line Interface with AWS IoT Events.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to use batch-put-message.
- AWS CLI
-
To send messages (inputs) to AWS IoT Events
The following
batch-put-messageexample sends a set of messages to the AWS IoT Events system. Each message payload is transformed into the input you specify (inputName) and ingested into any detectors that monitor that input. If multiple messages are sent, the order in which the messages are processed isn't guaranteed. To guarantee ordering, you must send messages one at a time and wait for a successful response.aws iotevents-data batch-put-message \ --cli-input-jsonfile://highPressureMessage.jsonContents of
highPressureMessage.json:{ "messages": [ { "messageId": "00001", "inputName": "PressureInput", "payload": "{\"motorid\": \"Fulton-A32\", \"sensorData\": {\"pressure\": 80, \"temperature\": 39} }" } ] }Output:
{ "BatchPutMessageErrorEntries": [] }For more information, see BatchPutMessage in the AWS IoT Events API Reference.
-
For API details, see BatchPutMessage
in AWS CLI Command Reference.
-
The following code example shows how to use batch-update-detector.
- AWS CLI
-
To update a detector (instance)
The following
batch-update-detectorexample updates the state, variable values, and timer settings of one or more detectors (instances) of a specified detector model.aws iotevents-data batch-update-detector \ --cli-input-jsonfile://budFulton-A32.jsonContents of
budFulton-A32.json:{ "detectors": [ { "messageId": "00001", "detectorModelName": "motorDetectorModel", "keyValue": "Fulton-A32", "state": { "stateName": "Normal", "variables": [ { "name": "pressureThresholdBreached", "value": "0" } ], "timers": [ ] } } ] }Output:
{ "batchUpdateDetectorErrorEntries": [] }For more information, see BatchUpdateDetector in the AWS IoT Events API Reference.
-
For API details, see BatchUpdateDetector
in AWS CLI Command Reference.
-
The following code example shows how to use create-detector-model.
- AWS CLI
-
To create a detector model
The following
create-detector-modelexample creates a detector model with its configuration specified by a parameter file.aws iotevents create-detector-model \ --cli-input-jsonfile://motorDetectorModel.jsonContents of
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" }Output:
{ "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" } }For more information, see CreateDetectorModel in the AWS IoT Events API Reference.
-
For API details, see CreateDetectorModel
in AWS CLI Command Reference.
-
The following code example shows how to use create-input.
- AWS CLI
-
To create an input
The following
create-inputexample creates an input.aws iotevents create-input \ --cli-input-jsonfile://pressureInput.jsonContents of
pressureInput.json:{ "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor", "inputDefinition": { "attributes": [ { "jsonPath": "sensorData.pressure" }, { "jsonPath": "motorid" } ] } }Output:
{ "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" } }For more information, see CreateInput in the AWS IoT Events API Reference.
-
For API details, see CreateInput
in AWS CLI Command Reference.
-
The following code example shows how to use delete-detector-model.
- AWS CLI
-
To delete a detector model
The following
delete-detector-modelexample deletes the specified detector model. Any active instances of the detector model are also deleted.aws iotevents delete-detector-model \ --detector-model-namemotorDetectorModelThis command produces no output.
For more information, see DeleteDetectorModel in the AWS IoT Events API Reference.
-
For API details, see DeleteDetectorModel
in AWS CLI Command Reference.
-
The following code example shows how to use delete-input.
- AWS CLI
-
To delete an input
The following
delete-inputexample deletes the specified input.aws iotevents delete-input \ --input-namePressureInputThis command produces no output.
For more information, see DeleteInput in the AWS IoT Events API Reference.
-
For API details, see DeleteInput
in AWS CLI Command Reference.
-
The following code example shows how to use describe-detector-model.
- AWS CLI
-
To get information about a detector model
The following
describe-detector-modelexample displays details for the specified detector model. Because theversionparameter is not specified, information about the latest version is returned.aws iotevents describe-detector-model \ --detector-model-namemotorDetectorModelOutput:
{ "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" } } }For more information, see DescribeDetectorModel in the AWS IoT Events API Reference.
-
For API details, see DescribeDetectorModel
in AWS CLI Command Reference.
-
The following code example shows how to use describe-detector.
- AWS CLI
-
To get information about a detector (instance).
The following
describe-detectorexample displays details for the specified detector (instance).aws iotevents-data describe-detector \ --detector-model-namemotorDetectorModel\ --key-value"Fulton-A32"Output:
{ "detector": { "lastUpdateTime": 1560797852.776, "creationTime": 1560797852.775, "state": { "variables": [ { "name": "pressureThresholdBreached", "value": "3" } ], "stateName": "Dangerous", "timers": [] }, "keyValue": "Fulton-A32", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" } }For more information, see DescribeDetector in the AWS IoT Events API Reference.
-
For API details, see DescribeDetector
in AWS CLI Command Reference.
-
The following code example shows how to use describe-input.
- AWS CLI
-
To get information about an input
The following
describe-inputexample displays details for the specified input.aws iotevents describe-input \ --input-namePressureInputOutput:
{ "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" } ] } } }For more information, see DescribeInput in the AWS IoT Events API Reference.
-
For API details, see DescribeInput
in AWS CLI Command Reference.
-
The following code example shows how to use describe-logging-options.
- AWS CLI
-
To get information about logging settings
The following
describe-logging-optionsexample retrieves the current settings of the AWS IoT Events logging options.aws iotevents describe-logging-optionsOutput:
{ "loggingOptions": { "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "enabled": false, "level": "ERROR" } }For more information, see DescribeLoggingOptions in the AWS IoT Events API Reference.
-
For API details, see DescribeLoggingOptions
in AWS CLI Command Reference.
-
The following code example shows how to use list-detector-model-versions.
- AWS CLI
-
To get information about versions of a detector model
The following
list-detector-model-versionsexample Lists all the versions of a detector model. Only the metadata associated with each detector model version is returned.aws iotevents list-detector-model-versions \ --detector-model-namemotorDetectorModelOutput:
{ "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" } ] }For more information, see ListDetectorModelVersions in the AWS IoT Events API Reference.
-
For API details, see ListDetectorModelVersions
in AWS CLI Command Reference.
-
The following code example shows how to use list-detector-models.
- AWS CLI
-
To get a list of your detector models
The following
list-detector-modelsexample Lists the detector models you have created. Only the metadata associated with each detector model is returned.aws iotevents list-detector-modelsOutput:
{ "detectorModelSummaries": [ { "detectorModelName": "motorDetectorModel", "creationTime": 1552072424.212 "detectorModelDescription": "Detect overpressure in a motor." } ] }For more information, see ListDetectorModels in the AWS IoT Events API Reference.
-
For API details, see ListDetectorModels
in AWS CLI Command Reference.
-
The following code example shows how to use list-detectors.
- AWS CLI
-
To get a list of detectors for a detector model
The following
list-detectorsexample lists the detectors (the instances of a detector model) in your account.aws iotevents-data list-detectors \ --detector-model-namemotorDetectorModelOutput:
{ "detectorSummaries": [ { "lastUpdateTime": 1558129925.2, "creationTime": 1552073155.527, "state": { "stateName": "Normal" }, "keyValue": "Fulton-A32", "detectorModelName": "motorDetectorModel", "detectorModelVersion": "1" } ] }For more information, see ListDetectors in the AWS IoT Events API Reference.
-
For API details, see ListDetectors
in AWS CLI Command Reference.
-
The following code example shows how to use list-inputs.
- AWS CLI
-
To list inputs
The following
list-inputsexample lists the inputs you have created in your account.aws iotevents list-inputsThis command produces no output. Output:
{ { "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" } }For more information, see ListInputs in the AWS IoT Events API Reference.
-
For API details, see ListInputs
in AWS CLI Command Reference.
-
The following code example shows how to use list-tags-for-resource.
- AWS CLI
-
To list tags assigned to a resource.
The following
list-tags-for-resourceexample lists the tag key names and values you have assigned to the resource.aws iotevents list-tags-for-resource \ --resource-arn"arn:aws:iotevents:us-west-2:123456789012:input/PressureInput"Output:
{ "tags": [ { "value": "motor", "key": "deviceType" } ] }For more information, see ListTagsForResource in the AWS IoT Events API Reference.
-
For API details, see ListTagsForResource
in AWS CLI Command Reference.
-
The following code example shows how to use put-logging-options.
- AWS CLI
-
To set logging options
The following
put-logging-optionsexample sets or updates the AWS IoT Events logging options. If you update the value of anyloggingOptions` 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 ``roleArnfield (for example, to correct an invalid policy) it can take up to five minutes for that change to take effect.aws iotevents put-logging-options \ --cli-input-jsonfile://logging-options.jsonContents of
logging-options.json:{ "loggingOptions": { "roleArn": "arn:aws:iam::123456789012:role/IoTEventsRole", "level": "DEBUG", "enabled": true, "detectorDebugOptions": [ { "detectorModelName": "motorDetectorModel", "keyValue": "Fulton-A32" } ] } }This command produces no output.
For more information, see PutLoggingOptions in the AWS IoT Events API Reference.
-
For API details, see PutLoggingOptions
in AWS CLI Command Reference.
-
The following code example shows how to use tag-resource.
- AWS CLI
-
To add tags to a resource
The following
tag-resourceexample adds or modifies (if keydeviceTypealready exists) the tag attached the specified resource.aws iotevents tag-resource \ --cli-input-jsonfile://pressureInput.tag.jsonContents of
pressureInput.tag.json:{ "resourceArn": "arn:aws:iotevents:us-west-2:123456789012:input/PressureInput", "tags": [ { "key": "deviceType", "value": "motor" } ] }This command produces no output.
For more information, see TagResource in the AWS IoT Events API Reference.
-
For API details, see TagResource
in AWS CLI Command Reference.
-
The following code example shows how to use untag-resource.
- AWS CLI
-
To remove tags from a resource
The following
untag-resourceexample removes the tag with the specified key name from the specified resource.aws iotevents untag-resource \ --resource-arnarn:aws:iotevents:us-west-2:123456789012:input/PressureInput\ --tagkeysdeviceTypeThis command produces no output.
For more information, see UntagResource in the AWS IoT Events API Reference.
-
For API details, see UntagResource
in AWS CLI Command Reference.
-
The following code example shows how to use update-detector-model.
- AWS CLI
-
To update a detector model
The following
update-detector-modelexample updates the specified detector model. Detectors (instances) spawned by the previous version are deleted and then re-created as new inputs arrive.aws iotevents update-detector-model \ --cli-input-jsonfile://motorDetectorModel.update.jsonContents of
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" }Output:
{ "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" } }For more information, see UpdateDetectorModel in the AWS IoT Events API Reference.
-
For API details, see UpdateDetectorModel
in AWS CLI Command Reference.
-
The following code example shows how to use update-input.
- AWS CLI
-
To update an input
The following
update-inputexample updates the specified input with a new description and definition.aws iotevents update-input \ --cli-input-jsonfile://pressureInput.jsonContents of
pressureInput.json:{ "inputName": "PressureInput", "inputDescription": "Pressure readings from a motor", "inputDefinition": { "attributes": [ { "jsonPath": "sensorData.pressure" }, { "jsonPath": "motorid" } ] } }Output:
{ "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" } }For more information, see UpdateInput in the AWS IoT Events API Reference.
-
For API details, see UpdateInput
in AWS CLI Command Reference.
-