Skip to content
v2.274.0

AWS::IoTEvents Construct Library ​

Version 2.274.0-alpha.0

Separate package

@aws-cdk/aws-iotevents-alpha is not part of aws-cdk-lib — install it separately:

sh
npm install @aws-cdk/aws-iotevents-alpha
pip install aws-cdk.aws-iotevents-alpha

This module is experimental (alpha): its API may change in any release, without a major version bump.


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.

ts
import * as iotevents from '@aws-cdk/aws-iotevents-alpha';
import * as actions from '@aws-cdk/aws-iotevents-actions-alpha';
import * as lambda from 'aws-cdk-lib/aws-lambda';

declare const func: lambda.IFunction;

const input = new iotevents.Input(this, 'MyInput', {
  inputName: 'my_input', // optional
  attributeJsonPaths: ['payload.deviceId', 'payload.temperature'],
});

const warmState = new iotevents.State({
  stateName: 'warm',
  onEnter: [{
    eventName: 'test-enter-event',
    condition: iotevents.Expression.currentInput(input),
    actions: [new actions.LambdaInvokeAction(func)], // optional
  }],
  onInput: [{ // optional
    eventName: 'test-input-event',
    actions: [new actions.LambdaInvokeAction(func)],
  }],
  onExit: [{ // optional
    eventName: 'test-exit-event',
    actions: [new actions.LambdaInvokeAction(func)],
  }],
});
const coldState = new iotevents.State({
  stateName: 'cold',
});

// transit to coldState when temperature is less than 15
warmState.transitionTo(coldState, {
  eventName: 'to_coldState', // optional property, default by combining the names of the States
  when: iotevents.Expression.lt(
    iotevents.Expression.inputAttribute(input, 'payload.temperature'),
    iotevents.Expression.fromString('15'),
  ),
  executing: [new actions.LambdaInvokeAction(func)], // optional
});
// transit to warmState when temperature is greater than or equal to 15
coldState.transitionTo(warmState, {
  when: iotevents.Expression.gte(
    iotevents.Expression.inputAttribute(input, 'payload.temperature'),
    iotevents.Expression.fromString('15'),
  ),
});

new iotevents.DetectorModel(this, 'MyDetectorModel', {
  detectorModelName: 'test-detector-model', // optional
  description: 'test-detector-model-description', // optional property, default is none
  evaluationMethod: iotevents.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,
});
python
import aws_cdk.aws_iotevents_alpha as iotevents
import aws_cdk.aws_iotevents_actions_alpha as actions
import aws_cdk.aws_lambda as lambda_

# func: lambda.IFunction


input = iotevents.Input(self, "MyInput",
    input_name="my_input",  # optional
    attribute_json_paths=["payload.deviceId", "payload.temperature"]
)

warm_state = iotevents.State(
    state_name="warm",
    on_enter=[iotevents.Event(
        event_name="test-enter-event",
        condition=iotevents.Expression.current_input(input),
        actions=[actions.LambdaInvokeAction(func)]
    )],
    on_input=[iotevents.Event( # optional
        event_name="test-input-event",
        actions=[actions.LambdaInvokeAction(func)])],
    on_exit=[iotevents.Event( # optional
        event_name="test-exit-event",
        actions=[actions.LambdaInvokeAction(func)])]
)
cold_state = iotevents.State(
    state_name="cold"
)

# transit to coldState when temperature is less than 15
warm_state.transition_to(cold_state,
    event_name="to_coldState",  # optional property, default by combining the names of the States
    when=iotevents.Expression.lt(
        iotevents.Expression.input_attribute(input, "payload.temperature"),
        iotevents.Expression.from_string("15")),
    executing=[actions.LambdaInvokeAction(func)]
)
# transit to warmState when temperature is greater than or equal to 15
cold_state.transition_to(warm_state,
    when=iotevents.Expression.gte(
        iotevents.Expression.input_attribute(input, "payload.temperature"),
        iotevents.Expression.from_string("15"))
)

iotevents.DetectorModel(self, "MyDetectorModel",
    detector_model_name="test-detector-model",  # optional
    description="test-detector-model-description",  # optional property, default is none
    evaluation_method=iotevents.EventEvaluation.SERIAL,  # optional property, default is iotevents.EventEvaluation.BATCH
    detector_key="payload.deviceId",  # optional property, default is none and single detector instance will be created and all inputs will be routed to it
    initial_state=warm_state
)
java
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();
csharp
using Amazon.CDK.AWS.IoTEvents.Alpha;
using Amazon.CDK.AWS.IoTEvents.Actions.Alpha;
using Amazon.CDK.AWS.Lambda;

IFunction func;


var input = new Input(this, "MyInput", new InputProps {
    InputName = "my_input",  // optional
    AttributeJsonPaths = new [] { "payload.deviceId", "payload.temperature" }
});

var warmState = new State(new StateProps {
    StateName = "warm",
    OnEnter = new [] { new Event {
        EventName = "test-enter-event",
        Condition = Expression.CurrentInput(input),
        Actions = new [] { new LambdaInvokeAction(func) }
    } },
    OnInput = new [] { new Event {  // optional
        EventName = "test-input-event",
        Actions = new [] { new LambdaInvokeAction(func) } } },
    OnExit = new [] { new Event {  // optional
        EventName = "test-exit-event",
        Actions = new [] { new LambdaInvokeAction(func) } } }
});
var coldState = new State(new StateProps {
    StateName = "cold"
});

// transit to coldState when temperature is less than 15
warmState.TransitionTo(coldState, new TransitionOptions {
    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 = new [] { new LambdaInvokeAction(func) }
});
// transit to warmState when temperature is greater than or equal to 15
coldState.TransitionTo(warmState, new TransitionOptions {
    When = Expression.Gte(Expression.InputAttribute(input, "payload.temperature"), Expression.FromString("15"))
});

new DetectorModel(this, "MyDetectorModel", new DetectorModelProps {
    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
});
go
import "github.com/aws/aws-cdk-go/awscdkioteventsalpha"
import "github.com/aws/aws-cdk-go/awscdkioteventsactionsalpha"
import lambda "github.com/aws/aws-cdk-go/awscdk"

var func IFunction


input := iotevents.NewInput(this, jsii.String("MyInput"), &InputProps{
	InputName: jsii.String("my_input"),
	 // optional
	AttributeJsonPaths: []*string{
		jsii.String("payload.deviceId"),
		jsii.String("payload.temperature"),
	},
})

warmState := iotevents.NewState(&StateProps{
	StateName: jsii.String("warm"),
	OnEnter: []Event{
		&Event{
			EventName: jsii.String("test-enter-event"),
			Condition: iotevents.Expression_CurrentInput(input),
			Actions: []IAction{
				actions.NewLambdaInvokeAction(func),
			},
		},
	},
	OnInput: []Event{
		&Event{
			 // optional
			EventName: jsii.String("test-input-event"),
			Actions: []IAction{
				actions.NewLambdaInvokeAction(func),
			},
		},
	},
	OnExit: []Event{
		&Event{
			 // optional
			EventName: jsii.String("test-exit-event"),
			Actions: []IAction{
				actions.NewLambdaInvokeAction(func),
			},
		},
	},
})
coldState := iotevents.NewState(&StateProps{
	StateName: jsii.String("cold"),
})

// transit to coldState when temperature is less than 15
warmState.TransitionTo(coldState, &TransitionOptions{
	EventName: jsii.String("to_coldState"),
	 // optional property, default by combining the names of the States
	When: iotevents.Expression_Lt(iotevents.Expression_InputAttribute(input, jsii.String("payload.temperature")), iotevents.Expression_FromString(jsii.String("15"))),
	Executing: []IAction{
		actions.NewLambdaInvokeAction(func),
	},
})
// transit to warmState when temperature is greater than or equal to 15
coldState.TransitionTo(warmState, &TransitionOptions{
	When: iotevents.Expression_Gte(iotevents.Expression_*InputAttribute(input, jsii.String("payload.temperature")), iotevents.Expression_*FromString(jsii.String("15"))),
})

iotevents.NewDetectorModel(this, jsii.String("MyDetectorModel"), &DetectorModelProps{
	DetectorModelName: jsii.String("test-detector-model"),
	 // optional
	Description: jsii.String("test-detector-model-description"),
	 // optional property, default is none
	EvaluationMethod: iotevents.EventEvaluation_SERIAL,
	 // optional property, default is iotevents.EventEvaluation.BATCH
	DetectorKey: jsii.String("payload.deviceId"),
	 // optional property, default is none and single detector instance will be created and all inputs will be routed to it
	InitialState: warmState,
})

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

ts
import * as iam from 'aws-cdk-lib/aws-iam';
import * as iotevents from '@aws-cdk/aws-iotevents-alpha';

declare const grantable: iam.IGrantable;
const input = iotevents.Input.fromInputName(this, 'MyInput', 'my_input');

input.grantWrite(grantable);
python
import aws_cdk.aws_iam as iam
import aws_cdk.aws_iotevents_alpha as iotevents

# grantable: iam.IGrantable

input = iotevents.Input.from_input_name(self, "MyInput", "my_input")

input.grant_write(grantable)
java
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);
csharp
using Amazon.CDK.AWS.IAM;
using Amazon.CDK.AWS.IoTEvents.Alpha;

IGrantable grantable;

var input = Input.FromInputName(this, "MyInput", "my_input");

input.GrantWrite(grantable);
go
import iam "github.com/aws/aws-cdk-go/awscdk"
import iotevents "github.com/aws/aws-cdk-go/awscdkioteventsalpha"

var grantable IGrantable

input := iotevents.Input_FromInputName(this, jsii.String("MyInput"), jsii.String("my_input"))

input.GrantWrite(grantable)

Types ​

NameKindSummary
DetectorModelConstruct (L2)Defines an AWS IoT Events detector model in this stack.
InputConstruct (L2)Defines an AWS IoT Events input in this stack.
ActionBindOptionsStructOptions when binding a Action to a detector model.
ActionConfigStructProperties for a AWS IoT Events action.
DetectorModelPropsStructProperties for defining an AWS IoT Events detector model.
EventStructSpecifies the actions to be performed when the condition evaluates to true.
EventEvaluationEnumInformation about the order in which events are evaluated and how actions are executed.
ExpressionClassExpression for events in Detector Model state.
IActionInterfaceAn abstract action for DetectorModel.
IDetectorModelInterfaceRepresents an AWS IoT Events detector model.
IInputInterfaceRepresents an AWS IoT Events input.
InputPropsStructProperties for defining an AWS IoT Events input.
StateClassDefines a state of a detector.
StatePropsStructProperties for defining a state of a detector.
TransitionOptionsStructProperties for options of state transition.