Skip to main content

A Step Function state machine construct focused on working well with the Workflow Studio

Project description

Workflow Studio compatible State Machine

View on Construct Hub

This is a Workflow Studio compatible AWS Step Function state machine construct.

The goal of this construct is to make it easy to build and maintain your state machines using the Workflow Studio but still leverage the AWS CDK as the source of truth for the state machine.

Read more about it here.

How to Use This Construct

Start by designing your initial state machine using the Workflow Studio. When done with your first draft, copy and paste the ASL definition to a local file.

Create a new instance of this construct, handing it a fully parsed version of the ASL. Then add overridden values. The fields in the overrides field should match the States field of the ASL.

Projen component

There is a projen component included in this library which will help you in using the construct. It works similar to the auto-discovery feature. To use it, first add the component to your projen project:

// ...
const { StepFunctionsAutoDiscover } = require('@matthewbonig/state-machine');

const project = new awscdk.AwsCdkTypeScriptApp({
  // ...,
  deps: [
    // ...,
    '@matthewbonig/state-machine',
  ]
});

new StepFunctionsAutoDiscover(project, {
  srcdir: project.srcdir,
});

Now projen will look for any files with a suffix .workflow.json and generate beside it:

  • A typed overrides interface which is based on your workflow.
  • A construct based on StateMachine that uses this override.

Instead of using the StateMachine construct directly you can now use the generated one:

.
├── MyFancyThing.workflow.json
└── MyFancyThing-statemachine.ts
export class SomeStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);
    const handler = new NodejsFunction(this, 'MyHandler');
    new SomeFancyThingStateMachine(this, 'MyFancyWorkflow', {
      overrides: {
        'My First State': {
          Parameters: {
            FunctionName: handler.functionName
          }
        }
      }
    })
  }
}

:warning: The interfaces and constructs generated here are NOT jsii compliant (they use Partials and Omits) and cannot be compiled by jsii into other languages. If you plan to distribute any libraries you cannot use this.

Examples

const secret = new Secret(stack, 'Secret', {});
new StateMachine(stack, 'Test', {
  stateMachineName: 'A nice state machine',
  definition: JSON.parse(fs.readFileSync(path.join(__dirname, 'sample.json'), 'utf8').toString()),
  overrides: {
    'Read database credentials secret': {
      Parameters: {
        SecretId: secret.secretArn,
      },
    },
  },
});

You can also override nested states in arrays, for example:

new StateMachine(stack, 'Test', {
    stateMachineName: 'A-nice-state-machine',
    overrides: {
      Branches: [{
        // pass an empty object too offset overrides
      }, {
        StartAt: 'StartInstances',
        States: {
          StartInstances: {
            Parameters: {
              InstanceIds: ['INSTANCE_ID'],
            },
          },
        },
      }],
    },
    stateMachineType: StateMachineType.STANDARD,
    definition: {
      States: {
        Branches: [
          {
            StartAt: 'ResumeCluster',
            States: {
              'Redshift Pass': {
                Type: 'Pass',
                End: true,
              },
            },
          },
          {
            StartAt: 'StartInstances',
            States: {
              'StartInstances': {
                Type: 'Task',
                Parameters: {
                  InstanceIds: [
                    'MyData',
                  ],
                },
                Resource: 'arn:aws:states:::aws-sdk:ec2:startInstances',
                Next: 'DescribeInstanceStatus',
              },
              'DescribeInstanceStatus': {
                Type: 'Task',
                Next: 'EC2 Pass',
                Parameters: {
                  InstanceIds: [
                    'MyData',
                  ],
                },
                Resource: 'arn:aws:states:::aws-sdk:ec2:describeInstanceStatus',
              },
              'EC2 Pass': {
                Type: 'Pass',
                End: true,
              },
            },
          },
        ],
      },
    },
  });

For Python, be sure to use a context manager when opening your JSON file.

  • You do not need to str() the dictionary object you supply as your definition prop.
  • Elements of your override path do need to be strings.
secret = Secret(stack, 'Secret')

with open('sample.json', 'r+', encoding='utf-8') as sample:
    sample_dict = json.load(sample)

state_machine = StateMachine(
    self,
    'Test',
    definition = sample_dict,
    overrides = {
    "Read database credentials secret": {
      "Parameters": {
        "SecretId": secret.secret_arn,
      },
    },
  })

In this example, the ASL has a state called 'Read database credentials secret' and the SecretId parameter is overridden with a CDK generated value. Future changes can be done by editing, debugging, and testing the state machine directly in the Workflow Studio. Once everything is working properly, copy and paste the ASL back to your local file.

Issues

Please open any issues you have on Github.

Contributing

Please submit PRs from forked repositories if you'd like to contribute.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mbonig.state-machine-0.0.18.tar.gz (73.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mbonig.state_machine-0.0.18-py3-none-any.whl (71.6 kB view details)

Uploaded Python 3

File details

Details for the file mbonig.state-machine-0.0.18.tar.gz.

File metadata

  • Download URL: mbonig.state-machine-0.0.18.tar.gz
  • Upload date:
  • Size: 73.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for mbonig.state-machine-0.0.18.tar.gz
Algorithm Hash digest
SHA256 62d04df4fe80de76813648f0ee8b8dacb6582e890a93df27c20e208392cf39e3
MD5 7b65f8c38cb812e7de4834df2155654d
BLAKE2b-256 8470ca71e613a16520cbce6b4d9612aa599c6b61aefe293975cea08e10355655

See more details on using hashes here.

File details

Details for the file mbonig.state_machine-0.0.18-py3-none-any.whl.

File metadata

File hashes

Hashes for mbonig.state_machine-0.0.18-py3-none-any.whl
Algorithm Hash digest
SHA256 660e83484d95063de9efc89f66daf411dddb53e7eb4ffff56802211390f30b3c
MD5 f6b031f7df4d0b57d94c59dc76e9626b
BLAKE2b-256 200e2e26debb7d9dadc51f945a7a7ccbd434092e86d8d8b028df3521eab4c633

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page