Skip to content
v2.275.0

SfnStateMachine Class ​

Use a StepFunctions state machine as a target for Amazon EventBridge rules.

Example ​

ts
import * as iam from 'aws-cdk-lib/aws-iam';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';

const rule = new events.Rule(this, 'Rule', {
  schedule: events.Schedule.rate(Duration.minutes(1)),
});

const dlq = new sqs.Queue(this, 'DeadLetterQueue');

const role = new iam.Role(this, 'Role', {
  assumedBy: new iam.ServicePrincipal('events.amazonaws.com'),
});
const stateMachine = new sfn.StateMachine(this, 'SM', {
  definition: new sfn.Wait(this, 'Hello', { time: sfn.WaitTime.duration(Duration.seconds(10)) })
});

rule.addTarget(new targets.SfnStateMachine(stateMachine, {
  input: events.RuleTargetInput.fromObject({ SomeParam: 'SomeValue' }),
  deadLetterQueue: dlq,
  role: role
}));
python
import aws_cdk.aws_iam as iam
import aws_cdk.aws_stepfunctions as sfn


rule = events.Rule(self, "Rule",
    schedule=events.Schedule.rate(Duration.minutes(1))
)

dlq = sqs.Queue(self, "DeadLetterQueue")

role = iam.Role(self, "Role",
    assumed_by=iam.ServicePrincipal("events.amazonaws.com")
)
state_machine = sfn.StateMachine(self, "SM",
    definition=sfn.Wait(self, "Hello", time=sfn.WaitTime.duration(Duration.seconds(10)))
)

rule.add_target(targets.SfnStateMachine(state_machine,
    input=events.RuleTargetInput.from_object({"SomeParam": "SomeValue"}),
    dead_letter_queue=dlq,
    role=role
))
java
import software.amazon.awscdk.services.iam.*;
import software.amazon.awscdk.services.stepfunctions.*;


Rule rule = Rule.Builder.create(this, "Rule")
        .schedule(Schedule.rate(Duration.minutes(1)))
        .build();

Queue dlq = new Queue(this, "DeadLetterQueue");

Role role = Role.Builder.create(this, "Role")
        .assumedBy(new ServicePrincipal("events.amazonaws.com"))
        .build();
StateMachine stateMachine = StateMachine.Builder.create(this, "SM")
        .definition(Wait.Builder.create(this, "Hello").time(WaitTime.duration(Duration.seconds(10))).build())
        .build();

rule.addTarget(SfnStateMachine.Builder.create(stateMachine)
        .input(RuleTargetInput.fromObject(Map.of("SomeParam", "SomeValue")))
        .deadLetterQueue(dlq)
        .role(role)
        .build());
csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.IAM;
using Amazon.CDK.AWS.StepFunctions;


var rule = new Rule(this, "Rule", new RuleProps {
    Schedule = Schedule.Rate(Duration.Minutes(1))
});

var dlq = new Queue(this, "DeadLetterQueue");

var role = new Role(this, "Role", new RoleProps {
    AssumedBy = new ServicePrincipal("events.amazonaws.com")
});
var stateMachine = new StateMachine(this, "SM", new StateMachineProps {
    Definition = new Wait(this, "Hello", new WaitProps { Time = WaitTime.Duration(Duration.Seconds(10)) })
});

rule.AddTarget(new SfnStateMachine(stateMachine, new SfnStateMachineProps {
    Input = RuleTargetInput.FromObject(new Dictionary<string, string> { { "SomeParam", "SomeValue" } }),
    DeadLetterQueue = dlq,
    Role = role
}));
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"


rule := events.NewRule(this, jsii.String("Rule"), &RuleProps{
	Schedule: events.Schedule_Rate(awscdk.Duration_Minutes(jsii.Number(1))),
})

dlq := sqs.NewQueue(this, jsii.String("DeadLetterQueue"))

role := iam.NewRole(this, jsii.String("Role"), &RoleProps{
	AssumedBy: iam.NewServicePrincipal(jsii.String("events.amazonaws.com")),
})
stateMachine := sfn.NewStateMachine(this, jsii.String("SM"), &StateMachineProps{
	Definition: sfn.NewWait(this, jsii.String("Hello"), &WaitProps{
		Time: sfn.WaitTime_Duration(awscdk.Duration_Seconds(jsii.Number(10))),
	}),
})

rule.AddTarget(targets.NewSfnStateMachine(stateMachine, &SfnStateMachineProps{
	Input: events.RuleTargetInput_FromObject(map[string]*string{
		"SomeParam": jsii.String("SomeValue"),
	}),
	DeadLetterQueue: dlq,
	Role: role,
}))

Initializer ​

ts
new SfnStateMachine(machine: IStateMachine, props?: SfnStateMachineProps)
python
SfnStateMachine(machine: IStateMachine, props: typing.Optional[SfnStateMachineProps] = None)
java
new SfnStateMachine(IStateMachine machine, SfnStateMachineProps props)
csharp
new SfnStateMachine(IStateMachine machine, SfnStateMachineProps? props = null)
go
NewSfnStateMachine(Machine IStateMachine, Props SfnStateMachineProps)

Properties ​

Methods ​

bind(rule: IRuleRef, _id?: string): RuleTargetConfigdef bind(self, rule: IRuleRef, _id: typing.Optional[str] = None) -> RuleTargetConfigpublic RuleTargetConfig bind(IRuleRef rule, String _id)public RuleTargetConfig Bind(IRuleRef rule, string? _id = null)func Bind(Rule IRuleRef, _id *string) RuleTargetConfig#
Returns a properties that are used in an Rule to trigger this State Machine.