Skip to content
v2.274.0

Actions for AWS::IoTEvents Detector Model ​

Version 2.274.0-alpha.0

Separate package

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

sh
npm install @aws-cdk/aws-iotevents-actions-alpha
pip install aws-cdk.aws-iotevents-actions-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.


This library contains integration classes to specify actions of state events of Detector Model in @aws-cdk/aws-iotevents-alpha. Instances of these classes should be passed to State defined in @aws-cdk/aws-iotevents-alpha You can define built-in actions to use a timer or set a variable, or send data to other AWS resources.

This library contains integration classes to use a timer or set a variable, or send data to other AWS resources. AWS IoT Events can trigger actions when it detects a specified event or transition event.

Currently supported are:

  • Use timer
  • Set variable to detector instance
  • Invoke a Lambda function

Use timer ​

The code snippet below creates an Action that creates the timer with duration in seconds.

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

declare const input: iotevents.IInput;

const state = new iotevents.State({
  stateName: 'MyState',
  onEnter: [{
    eventName: 'test-event',
    condition: iotevents.Expression.currentInput(input),
    actions: [
      new actions.SetTimerAction('MyTimer', {
        duration: cdk.Duration.seconds(60),
      }),
    ],
  }],
});
python
import aws_cdk.aws_iotevents_alpha as iotevents
import aws_cdk.aws_iotevents_actions_alpha as actions

# input: iotevents.IInput

state = iotevents.State(
    state_name="MyState",
    on_enter=[iotevents.Event(
        event_name="test-event",
        condition=iotevents.Expression.current_input(input),
        actions=[
            actions.SetTimerAction("MyTimer", {
                "duration": cdk.Duration.seconds(60)
            })
        ]
    )]
)
java
import software.amazon.awscdk.services.iotevents.alpha.*;
import software.amazon.awscdk.services.iotevents.actions.alpha.*;

IInput input;

State state = State.Builder.create()
        .stateName("MyState")
        .onEnter(List.of(Event.builder()
                .eventName("test-event")
                .condition(Expression.currentInput(input))
                .actions(List.of(
                    new SetTimerAction("MyTimer", Map.of(
                            "duration", cdk.Duration.seconds(60)))))
                .build()))
        .build();
csharp
using Amazon.CDK.AWS.IoTEvents.Alpha;
using Amazon.CDK.AWS.IoTEvents.Actions.Alpha;

IInput input;

var state = new State(new StateProps {
    StateName = "MyState",
    OnEnter = new [] { new Event {
        EventName = "test-event",
        Condition = Expression.CurrentInput(input),
        Actions = new [] {
            new SetTimerAction("MyTimer", new Dictionary<string, object> {
                { "duration", cdk.Duration.Seconds(60) }
            }) }
    } }
});
go
import "github.com/aws/aws-cdk-go/awscdkioteventsalpha"
import actions "github.com/aws/aws-cdk-go/awscdkioteventsactionsalpha"

var input IInput

state := iotevents.NewState(&StateProps{
	StateName: jsii.String("MyState"),
	OnEnter: []Event{
		&Event{
			EventName: jsii.String("test-event"),
			Condition: iotevents.Expression_CurrentInput(input),
			Actions: []IAction{
				actions.NewSetTimerAction(jsii.String("MyTimer"), map[string]interface{}{
					"duration": cdk.Duration_seconds(jsii.Number(60)),
				}),
			},
		},
	},
})

Setting duration by IoT Events Expression:

ts
new actions.SetTimerAction('MyTimer', {
  durationExpression: iotevents.Expression.inputAttribute(input, 'payload.durationSeconds'),
})
python
actions.SetTimerAction("MyTimer",
    duration_expression=iotevents.Expression.input_attribute(input, "payload.durationSeconds")
)
java
SetTimerAction.Builder.create("MyTimer")
        .durationExpression(iotevents.Expression.inputAttribute(input, "payload.durationSeconds"))
        .build();
csharp
new actions.SetTimerAction("MyTimer", new Struct {
    DurationExpression = iotevents.Expression.InputAttribute(input, "payload.durationSeconds")
});
go
actions.NewSetTimerAction(jsii.String("MyTimer"), map[string]interface{}{
	"durationExpression": iotevents.Expression_inputAttribute(input, jsii.String("payload.durationSeconds")),
})

And the timer can be reset and cleared. Below is an example of general Device HeartBeat Detector Model:

ts
const online = new iotevents.State({
  stateName: 'Online',
  onEnter: [{
    eventName: 'enter-event',
    condition: iotevents.Expression.currentInput(input),
    actions: [
      new actions.SetTimerAction('MyTimer', {
        duration: cdk.Duration.seconds(60),
      }),
    ],
  }],
  onInput: [{
    eventName: 'input-event',
    condition: iotevents.Expression.currentInput(input),
    actions: [
      new actions.ResetTimerAction('MyTimer'),
    ],
  }],
  onExit: [{
    eventName: 'exit-event',
    actions: [
      new actions.ClearTimerAction('MyTimer'),
    ],
  }],
});
const offline = new iotevents.State({ stateName: 'Offline' });

online.transitionTo(offline, { when: iotevents.Expression.timeout('MyTimer') });
offline.transitionTo(online, { when: iotevents.Expression.currentInput(input) });
python
online = iotevents.State(
    state_name="Online",
    on_enter=[{
        "event_name": "enter-event",
        "condition": iotevents.Expression.current_input(input),
        "actions": [
            actions.SetTimerAction("MyTimer",
                duration=cdk.Duration.seconds(60)
            )
        ]
    }],
    on_input=[{
        "event_name": "input-event",
        "condition": iotevents.Expression.current_input(input),
        "actions": [
            actions.ResetTimerAction("MyTimer")
        ]
    }],
    on_exit=[{
        "event_name": "exit-event",
        "actions": [
            actions.ClearTimerAction("MyTimer")
        ]
    }]
)
offline = iotevents.State(state_name="Offline")

online.transition_to(offline, when=iotevents.Expression.timeout("MyTimer"))
offline.transition_to(online, when=iotevents.Expression.current_input(input))
java
Object online = State.Builder.create()
        .stateName("Online")
        .onEnter(List.of(Map.of(
                "eventName", "enter-event",
                "condition", iotevents.Expression.currentInput(input),
                "actions", List.of(
                    SetTimerAction.Builder.create("MyTimer")
                            .duration(cdk.Duration.seconds(60))
                            .build()))))
        .onInput(List.of(Map.of(
                "eventName", "input-event",
                "condition", iotevents.Expression.currentInput(input),
                "actions", List.of(
                    new ResetTimerAction("MyTimer")))))
        .onExit(List.of(Map.of(
                "eventName", "exit-event",
                "actions", List.of(
                    new ClearTimerAction("MyTimer")))))
        .build();
Object offline = State.Builder.create().stateName("Offline").build();

online.transitionTo(offline, Map.of("when", iotevents.Expression.timeout("MyTimer")));
offline.transitionTo(online, Map.of("when", iotevents.Expression.currentInput(input)));
csharp
var online = new iotevents.State(new Struct {
    StateName = "Online",
    OnEnter = new [] { new Struct {
        EventName = "enter-event",
        Condition = iotevents.Expression.CurrentInput(input),
        Actions = new [] {
            new actions.SetTimerAction("MyTimer", new Struct {
                Duration = cdk.Duration.Seconds(60)
            }) }
    } },
    OnInput = new [] { new Struct {
        EventName = "input-event",
        Condition = iotevents.Expression.CurrentInput(input),
        Actions = new [] {
            new actions.ResetTimerAction("MyTimer") }
    } },
    OnExit = new [] { new Struct {
        EventName = "exit-event",
        Actions = new [] {
            new actions.ClearTimerAction("MyTimer") }
    } }
});
var offline = new iotevents.State(new Struct { StateName = "Offline" });

online.TransitionTo(offline, new Struct { When = iotevents.Expression.Timeout("MyTimer") });
offline.TransitionTo(online, new Struct { When = iotevents.Expression.CurrentInput(input) });
go
online := iotevents.NewState(map[string]interface{}{
	"stateName": jsii.String("Online"),
	"onEnter": []map[string]interface{}{
		map[string]interface{}{
			"eventName": jsii.String("enter-event"),
			"condition": iotevents.Expression_currentInput(input),
			"actions": []interface{}{
				actions.NewSetTimerAction(jsii.String("MyTimer"), map[string]interface{}{
					"duration": cdk.Duration_seconds(jsii.Number(60)),
				}),
			},
		},
	},
	"onInput": []map[string]interface{}{
		map[string]interface{}{
			"eventName": jsii.String("input-event"),
			"condition": iotevents.Expression_currentInput(input),
			"actions": []interface{}{
				actions.NewResetTimerAction(jsii.String("MyTimer")),
			},
		},
	},
	"onExit": []map[string]interface{}{
		map[string]interface{}{
			"eventName": jsii.String("exit-event"),
			"actions": []interface{}{
				actions.NewClearTimerAction(jsii.String("MyTimer")),
			},
		},
	},
})
offline := iotevents.NewState(map[string]*string{
	"stateName": jsii.String("Offline"),
})

online.transitionTo(offline, map[string]interface{}{
	"when": iotevents.Expression_timeout(jsii.String("MyTimer")),
})
offline.transitionTo(online, map[string]interface{}{
	"when": iotevents.Expression_currentInput(input),
})

Set variable to detector instance ​

The code snippet below creates an Action that set variable to detector instance when it is triggered.

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

declare const input: iotevents.IInput;

const state = new iotevents.State({
  stateName: 'MyState',
  onEnter: [{
    eventName: 'test-event',
    condition: iotevents.Expression.currentInput(input),
    actions: [
      new actions.SetVariableAction(
        'MyVariable',
        iotevents.Expression.inputAttribute(input, 'payload.temperature'),
      ),
    ],
  }],
});
python
import aws_cdk.aws_iotevents_alpha as iotevents
import aws_cdk.aws_iotevents_actions_alpha as actions

# input: iotevents.IInput

state = iotevents.State(
    state_name="MyState",
    on_enter=[iotevents.Event(
        event_name="test-event",
        condition=iotevents.Expression.current_input(input),
        actions=[
            actions.SetVariableAction("MyVariable",
                iotevents.Expression.input_attribute(input, "payload.temperature"))
        ]
    )]
)
java
import software.amazon.awscdk.services.iotevents.alpha.*;
import software.amazon.awscdk.services.iotevents.actions.alpha.*;

IInput input;

State state = State.Builder.create()
        .stateName("MyState")
        .onEnter(List.of(Event.builder()
                .eventName("test-event")
                .condition(Expression.currentInput(input))
                .actions(List.of(
                    new SetVariableAction("MyVariable", Expression.inputAttribute(input, "payload.temperature"))))
                .build()))
        .build();
csharp
using Amazon.CDK.AWS.IoTEvents.Alpha;
using Amazon.CDK.AWS.IoTEvents.Actions.Alpha;

IInput input;

var state = new State(new StateProps {
    StateName = "MyState",
    OnEnter = new [] { new Event {
        EventName = "test-event",
        Condition = Expression.CurrentInput(input),
        Actions = new [] {
            new SetVariableAction("MyVariable", Expression.InputAttribute(input, "payload.temperature")) }
    } }
});
go
import "github.com/aws/aws-cdk-go/awscdkioteventsalpha"
import actions "github.com/aws/aws-cdk-go/awscdkioteventsactionsalpha"

var input IInput

state := iotevents.NewState(&StateProps{
	StateName: jsii.String("MyState"),
	OnEnter: []Event{
		&Event{
			EventName: jsii.String("test-event"),
			Condition: iotevents.Expression_CurrentInput(input),
			Actions: []IAction{
				actions.NewSetVariableAction(jsii.String("MyVariable"), iotevents.Expression_InputAttribute(input, jsii.String("payload.temperature"))),
			},
		},
	},
})

Invoke a Lambda function ​

The code snippet below creates an Action that invoke a Lambda function when it is triggered.

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 input: iotevents.IInput;
declare const func: lambda.IFunction;

const state = new iotevents.State({
  stateName: 'MyState',
  onEnter: [{
    eventName: 'test-event',
    condition: iotevents.Expression.currentInput(input),
    actions: [new actions.LambdaInvokeAction(func)],
  }],
});
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_

# input: iotevents.IInput
# func: lambda.IFunction

state = iotevents.State(
    state_name="MyState",
    on_enter=[iotevents.Event(
        event_name="test-event",
        condition=iotevents.Expression.current_input(input),
        actions=[actions.LambdaInvokeAction(func)]
    )]
)
java
import software.amazon.awscdk.services.iotevents.alpha.*;
import software.amazon.awscdk.services.iotevents.actions.alpha.*;
import software.amazon.awscdk.services.lambda.*;

IInput input;
IFunction func;

State state = State.Builder.create()
        .stateName("MyState")
        .onEnter(List.of(Event.builder()
                .eventName("test-event")
                .condition(Expression.currentInput(input))
                .actions(List.of(new LambdaInvokeAction(func)))
                .build()))
        .build();
csharp
using Amazon.CDK.AWS.IoTEvents.Alpha;
using Amazon.CDK.AWS.IoTEvents.Actions.Alpha;
using Amazon.CDK.AWS.Lambda;

IInput input;
IFunction func;

var state = new State(new StateProps {
    StateName = "MyState",
    OnEnter = new [] { new Event {
        EventName = "test-event",
        Condition = Expression.CurrentInput(input),
        Actions = new [] { new LambdaInvokeAction(func) }
    } }
});
go
import "github.com/aws/aws-cdk-go/awscdkioteventsalpha"
import actions "github.com/aws/aws-cdk-go/awscdkioteventsactionsalpha"
import lambda "github.com/aws/aws-cdk-go/awscdk"

var input IInput
var func IFunction

state := iotevents.NewState(&StateProps{
	StateName: jsii.String("MyState"),
	OnEnter: []Event{
		&Event{
			EventName: jsii.String("test-event"),
			Condition: iotevents.Expression_CurrentInput(input),
			Actions: []IAction{
				actions.NewLambdaInvokeAction(func),
			},
		},
	},
})

Types ​

NameKindSummary
ClearTimerActionClassThe action to delete an existing timer.
LambdaInvokeActionClassThe action to write the data to an AWS Lambda function.
ResetTimerActionClassThe action to reset an existing timer.
SetTimerActionClassThe action to create a timer with duration in seconds.
SetVariableActionClassThe action to create a variable with a specified value.
TimerDurationClassThe duration of the timer.