npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@andybalham/state-machine-builder

v1.0.13

Published

A fluent builder interface for defining state machines in CDK

Downloads

32

Readme

State Machine Builder

A fluent builder interface for defining state machines in CDK.

Two versions are supported:

Usage

State machine definitions in CDK are defined using a nested structure. Consider the following example with a sequence of four states:

const stack = new cdk.Stack();

const state1 = new sfn.Pass(stack, 'State1');
const state2 = new sfn.Pass(stack, 'State2');
const state3 = new sfn.Pass(stack, 'State3');
const state4 = new sfn.Pass(stack, 'State4');

new sfn.StateMachine(stack, 'ChainExample', {
  definition: sfn.Chain.start(state1.next(state2.next(state3.next(state4))))
});

Using the fluent builder, the same state machine is defined as follows:

new sfn.StateMachine(stack, 'ChainExample', {
  definition: new StateMachineBuilder()

    .perform(state1)
    .perform(state2)
    .perform(state3)
    .perform(state4)

    .build(stack),
});

IMHO, this approach reads better, is easier to maintain, plays nicer with Prettier, and results in more meaningful differences in pull requests.

Wait and Pass States

Wait and Pass states can added to a state machine either by using the perform method, or by using the pass or wait methods for a little more convenience as the scope parameter is passed in automatically when the build method is called.

new sfn.StateMachine(stack, 'WaitAndPassExample', {
  definition: new StateMachineBuilder()

    .wait('Wait1', {
      time: sfn.WaitTime.duration(cdk.Duration.seconds(1)),
    })

    .pass('Pass1', {
      comment: 'This is pass state 1',
    })

    .build(stack),
});

Choice States

Choice states are defined using references to the id of the target states rather than the state itself. This allows us to avoid nesting states within states when defining the branching. See the example below where a choice has three branches, two with conditions attached, along with a default branch if none of the conditions evaluate to true.

new sfn.StateMachine(stack, 'ChoiceExample', {
  definition: new StateMachineBuilder()

    .choice('Choice1', {
      choices: [
        { when: sfn.Condition.stringEquals('$.var1', 'Foo'), next: state1.id },
        { when: sfn.Condition.stringEquals('$.var1', 'Bar'), next: state2.id },
      ],
      otherwise: state3.id,
    })

    .perform(state1)
    .end()

    .perform(state2)
    .end()

    .perform(state3)

    .build(stack),
});

Next statements

The next statement is used to explicitly define the next state in the flow. Consider the following example, where there is a choice between two states (state1 and state2) followed by a common state (state3). The next statement is used after state1 to prevent the flow from going to state2.

new sfn.StateMachine(stack, 'ChoiceExample', {
  definition: new StateMachineBuilder()

    .choice('Choice1', {
      choices: [{ when: sfn.Condition.booleanEquals('$.var1', true), next: state1.id }],
      otherwise: state2.id,
    })

    .perform(state1)
    .next(state3.id)

    .perform(state2)

    .perform(state3)

    .build(stack),
});

Succeed and Fail States

Succeed and Fail states are added using the succeed and fail methods, passing in an id and optional properties. Succeed and Fail states are always terminal states, so it is not necessary to have an end method call after them.

new sfn.StateMachine(stack, 'SucceedAndFailExample', {
  definition: new StateMachineBuilder()

    .choice('Choice1', {
      choices: [
        { when: sfn.Condition.stringEquals('$.var1', 'Foo'), next: 'Succeed1' },
        { when: sfn.Condition.stringEquals('$.var1', 'Bar'), next: 'Succeed2' },
      ],
      otherwise: 'Fail1',
    })

    .succeed('Succeed1', {
      comment: 'Success 1',
    })

    .succeed('Succeed2', {
      comment: 'Success 2',
    })

    .fail('Fail1', {
      comment: 'Failure 1',
    })

    .build(stack),
});

Map States

Map states are defined by specifying a StateMachineBuilder instance for the iterator property.

new sfn.StateMachine(stack, 'MapExample', {
  definition: new StateMachineBuilder()

    .map('Map1', {
      itemsPath: '$.Items1',
      iterator: new StateMachineBuilder()
        .perform(state1)
        .perform(state2)
        .perform(state3),
    })

    .build(stack);
}

Parallel States

Parallel states are defined by specifying an array of StateMachineBuilder instances for the branches property.

new sfn.StateMachine(stack, 'ParallelExample', {
  definition: new StateMachineBuilder()

    .parallel('Parallel1', {
      branches: [
        new StateMachineBuilder()
          .perform(state1)
          .perform(state2),
        new StateMachineBuilder()
          .perform(state3)
          .perform(state4),
      ],
    })

    .build(stack);
}

Lambda Invoke

The lambdaInvoke method provides a way of adding states that invoke a Lambda function as a task. As well as providing a little syntactic sugar, default props can be passed in to the build method. In the following example, default values for retryOnServiceExceptions and payloadResponseOnly are specified via the build method, but the LambdaInvoke2 overrides the value for retryOnServiceExceptions. The example also shows how the parameters property provides a simplified way of defining an object payload and how the retry properties are specified.

new sfn.StateMachine(stack, 'LambdaInvokeExample', {
  definition: new StateMachineBuilder()

    .lambdaInvoke('LambdaInvoke1', {
      lambdaFunction: function1,
      parameters: {
        constant: 'ConstantValue',
        'dynamic.$': '$.dynamicValue',
      },
      retry: { maxAttempts: 3 },
    })

    .lambdaInvoke('LambdaInvoke2', {
      lambdaFunction: function2,
      retryOnServiceExceptions: true,
    })

    .build(definitionScope, {
      defaultProps: {
        lambdaInvoke: {
          retryOnServiceExceptions: false,
          payloadResponseOnly: true,
        },
      },
    });
}

Error Handling

Error handlers are specified by supplying an array of catch instances as part of the props passed to the tryPerform, lambdaInvoke, map, and parallel methods. Each catch instance specifies the errors that are handled and the id of the handling state.

new sfn.StateMachine(stack, 'ErrorHandlingExample', {
  definition: new StateMachineBuilder()

    .tryPerform(function1, {
      catches: [
        { errors: ['States.Timeout'], handler: catchAll.id },
        { errors: ['States.All'], handler: catchAll.id },
      ],
    })
    .map('Map1', {
      itemsPath: '$.Items1',
      iterator: new StateMachineBuilder()
        .perform(state1)
        .perform(state2),
      catches: [{ handler: catchAll.id }],
    })
    .parallel('Parallel1', {
      branches: [
        new StateMachineBuilder().perform(state3),
        new StateMachineBuilder().perform(state4)],
      catches: [{ handler: catchAll.id }],
    })
    .end()

    .perform(catchAll)

    .build(stack);
}