Package software.amazon.awscdk.services.iotevents.alpha


@Stability(Experimental) package software.amazon.awscdk.services.iotevents.alpha

AWS::IoTEvents Construct Library

---

cdk-constructs: Experimental

The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.


AWS IoT Events enables you to monitor your equipment or device fleets for failures or changes in operation, and to trigger actions when such events occur.

DetectorModel

The following example creates an AWS IoT Events detector model to your stack. The detector model need a reference to at least one AWS IoT Events input. AWS IoT Events inputs enable the detector to get MQTT payload values from IoT Core rules.

You can define built-in actions to use a timer or set a variable, or send data to other AWS resources. See also @aws-cdk/aws-iotevents-actions-alpha for other actions.

 import software.amazon.awscdk.services.iotevents.alpha.*;
 import software.amazon.awscdk.services.iotevents.actions.alpha.*;
 import software.amazon.awscdk.services.lambda.*;
 
 IFunction func;
 
 
 Input input = Input.Builder.create(this, "MyInput")
         .inputName("my_input") // optional
         .attributeJsonPaths(List.of("payload.deviceId", "payload.temperature"))
         .build();
 
 State warmState = State.Builder.create()
         .stateName("warm")
         .onEnter(List.of(Event.builder()
                 .eventName("test-enter-event")
                 .condition(Expression.currentInput(input))
                 .actions(List.of(new LambdaInvokeAction(func)))
                 .build()))
         .onInput(List.of(Event.builder() // optional
                 .eventName("test-input-event")
                 .actions(List.of(new LambdaInvokeAction(func))).build()))
         .onExit(List.of(Event.builder() // optional
                 .eventName("test-exit-event")
                 .actions(List.of(new LambdaInvokeAction(func))).build()))
         .build();
 State coldState = State.Builder.create()
         .stateName("cold")
         .build();
 
 // transit to coldState when temperature is less than 15
 warmState.transitionTo(coldState, TransitionOptions.builder()
         .eventName("to_coldState") // optional property, default by combining the names of the States
         .when(Expression.lt(Expression.inputAttribute(input, "payload.temperature"), Expression.fromString("15")))
         .executing(List.of(new LambdaInvokeAction(func)))
         .build());
 // transit to warmState when temperature is greater than or equal to 15
 coldState.transitionTo(warmState, TransitionOptions.builder()
         .when(Expression.gte(Expression.inputAttribute(input, "payload.temperature"), Expression.fromString("15")))
         .build());
 
 DetectorModel.Builder.create(this, "MyDetectorModel")
         .detectorModelName("test-detector-model") // optional
         .description("test-detector-model-description") // optional property, default is none
         .evaluationMethod(EventEvaluation.SERIAL) // optional property, default is iotevents.EventEvaluation.BATCH
         .detectorKey("payload.deviceId") // optional property, default is none and single detector instance will be created and all inputs will be routed to it
         .initialState(warmState)
         .build();
 

To grant permissions to put messages in the input, you can use the grantWrite() method:

 import software.amazon.awscdk.services.iam.*;
 import software.amazon.awscdk.services.iotevents.alpha.*;
 
 IGrantable grantable;
 
 IInput input = Input.fromInputName(this, "MyInput", "my_input");
 
 input.grantWrite(grantable);