aws-cdk-lib.aws_stepfunctions.StateMachineFragment

class StateMachineFragment

LanguageType name
.NETAmazon.CDK.AWS.StepFunctions.StateMachineFragment
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awsstepfunctions#StateMachineFragment
Javasoftware.amazon.awscdk.services.stepfunctions.StateMachineFragment
Pythonaws_cdk.aws_stepfunctions.StateMachineFragment
TypeScript (source)aws-cdk-lib » aws_stepfunctions » StateMachineFragment

Implements IConstruct, IDependable, IChainable

Extends Construct

Base class for reusable state machine fragments.

Example

import { Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';

interface MyJobProps {
  jobFlavor: string;
}

class MyJob extends sfn.StateMachineFragment {
  public readonly startState: sfn.State;
  public readonly endStates: sfn.INextable[];

  constructor(parent: Construct, id: string, props: MyJobProps) {
    super(parent, id);

    const choice = new sfn.Choice(this, 'Choice')
      .when(sfn.Condition.stringEquals('$.branch', 'left'), new sfn.Pass(this, 'Left Branch'))
      .when(sfn.Condition.stringEquals('$.branch', 'right'), new sfn.Pass(this, 'Right Branch'));

    // ...

    this.startState = choice;
    this.endStates = choice.afterwards().endStates;
  }
}

class MyStack extends Stack {
  constructor(scope: Construct, id: string) {
    super(scope, id);
    // Do 3 different variants of MyJob in parallel
    const parallel = new sfn.Parallel(this, 'All jobs')
      .branch(new MyJob(this, 'Quick', { jobFlavor: 'quick' }).prefixStates())
      .branch(new MyJob(this, 'Medium', { jobFlavor: 'medium' }).prefixStates())
      .branch(new MyJob(this, 'Slow', { jobFlavor: 'slow' }).prefixStates());

    new sfn.StateMachine(this, 'MyStateMachine', {
      definition: parallel,
    });
  }
}

Initializer

new StateMachineFragment(scope: Construct, id: string)

Parameters

  • scope Construct — The scope in which to define this construct.
  • id string — The scoped construct ID.

Creates a new construct node.

Properties

NameTypeDescription
endStatesINextable[]The states to chain onto if this fragment is used.
idstringDescriptive identifier for this chainable.
nodeNodeThe tree node.
startStateStateThe start state of this state machine fragment.

endStates

Type: INextable[]

The states to chain onto if this fragment is used.


id

Type: string

Descriptive identifier for this chainable.


node

Type: Node

The tree node.


startState

Type: State

The start state of this state machine fragment.

Methods

NameDescription
next(next)Continue normal execution with the given state.
prefixStates(prefix?)Prefix the IDs of all states in this state machine fragment.
toSingleState(options?)Wrap all states in this state machine fragment up into a single state.
toString()Returns a string representation of this construct.

next(next)

public next(next: IChainable): Chain

Parameters

  • next IChainable

Returns

  • Chain

Continue normal execution with the given state.


prefixStates(prefix?)

public prefixStates(prefix?: string): StateMachineFragment

Parameters

  • prefix string — The prefix to add.

Returns

  • StateMachineFragment

Prefix the IDs of all states in this state machine fragment.

Use this to avoid multiple copies of the state machine all having the same state IDs.


toSingleState(options?)

public toSingleState(options?: SingleStateOptions): Parallel

Parameters

  • options SingleStateOptions

Returns

  • Parallel

Wrap all states in this state machine fragment up into a single state.

This can be used to add retry or error handling onto this state machine fragment.

Be aware that this changes the result of the inner state machine to be an array with the result of the state machine in it. Adjust your paths accordingly. For example, change 'outputPath' to '$[0]'.


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.