StateMachineFragment

class aws_cdk.aws_stepfunctions.StateMachineFragment(scope, id)

Bases: Construct

Base class for reusable state machine fragments.

ExampleMetadata:

nofixture infused

Example:

from aws_cdk import Stack
from constructs import Construct
import aws_cdk.aws_stepfunctions as sfn

class MyJob(sfn.StateMachineFragment):

    def __init__(self, parent, id, *, jobFlavor):
        super().__init__(parent, id)

        choice = sfn.Choice(self, "Choice").when(sfn.Condition.string_equals("$.branch", "left"), sfn.Pass(self, "Left Branch")).when(sfn.Condition.string_equals("$.branch", "right"), sfn.Pass(self, "Right Branch"))

        # ...

        self.start_state = choice
        self.end_states = choice.afterwards().end_states

class MyStack(Stack):
    def __init__(self, scope, id):
        super().__init__(scope, id)
        # Do 3 different variants of MyJob in parallel
        parallel = sfn.Parallel(self, "All jobs").branch(MyJob(self, "Quick", job_flavor="quick").prefix_states()).branch(MyJob(self, "Medium", job_flavor="medium").prefix_states()).branch(MyJob(self, "Slow", job_flavor="slow").prefix_states())

        sfn.StateMachine(self, "MyStateMachine",
            definition_body=sfn.DefinitionBody.from_chainable(parallel)
        )

Creates a new construct node.

Parameters:
  • scope (Construct) – The scope in which to define this construct.

  • id (str) – The scoped construct ID. Must be unique amongst siblings. If the ID includes a path separator (/), then it will be replaced by double dash --.

Methods

next(next)

Continue normal execution with the given state.

Parameters:

next (IChainable) –

Return type:

Chain

prefix_states(prefix=None)

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.

Parameters:

prefix (Optional[str]) – The prefix to add. Will use construct ID by default.

Return type:

StateMachineFragment

to_single_state(*, prefix_states=None, state_id=None, comment=None, input_path=None, output_path=None, result_path=None, result_selector=None, state_name=None)

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]’.

Parameters:
  • prefix_states (Optional[str]) – String to prefix all stateIds in the state machine with. Default: stateId

  • state_id (Optional[str]) – ID of newly created containing state. Default: Construct ID of the StateMachineFragment

  • comment (Optional[str]) – An optional description for this state. Default: No comment

  • input_path (Optional[str]) – JSONPath expression to select part of the state to be the input to this state. May also be the special value JsonPath.DISCARD, which will cause the effective input to be the empty object {}. Default: $

  • output_path (Optional[str]) – JSONPath expression to select part of the state to be the output to this state. May also be the special value JsonPath.DISCARD, which will cause the effective output to be the empty object {}. Default: $

  • result_path (Optional[str]) – JSONPath expression to indicate where to inject the state’s output. May also be the special value JsonPath.DISCARD, which will cause the state’s input to become its output. Default: $

  • result_selector (Optional[Mapping[str, Any]]) – The JSON that will replace the state’s raw result and become the effective result before ResultPath is applied. You can use ResultSelector to create a payload with values that are static or selected from the state’s raw result. Default: - None

  • state_name (Optional[str]) – Optional name for this state. Default: - The construct ID will be used as state name

Return type:

Parallel

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

end_states

The states to chain onto if this fragment is used.

id

Descriptive identifier for this chainable.

node

The tree node.

start_state

The start state of this state machine fragment.

Static Methods

classmethod is_construct(x)

Checks if x is a construct.

Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

Parameters:

x (Any) – Any object.

Return type:

bool

Returns:

true if x is an object created from a class which extends Construct.