DEV Community

Cover image for CDK ASL Definition Extractor
Benjamen Pyle for AWS Community Builders

Posted on • Edited on • Originally published at binaryheap.com

CDK ASL Definition Extractor

I've been working a good bit lately to push testing down when using Step Functions and building some patterns with AWS State Machines Locally. In that same spirit, I'm wanting to be able to create the State Machine in my local container and that comes from the ASL. However, when using CDK and the builder libraries you don't have an ASL file to work from. So I built this program to extract the ASL Definition from a CDK synth'd CloudFormation file.

I've written a good bit about CDK so if you are looking for some intro this article is a good place to start. But if you've moved passed that and are looking to further build upon some more advanced patterns, I'll have a follow-up article on how to use the Local Step Functions container in conjunction with this program.

CDK ASL Definition Extractor Usage

If you just want to jump straight to the README or the NPM package you can do that here:

This is a simple command line utility that takes the CDK Template output and parses through to extract out the AWS::StepFunctions::StateMachine resources and then outputs them to the STDOUT so that you can do something with that definition.

For usage

Preview Options

โฏ cdk-asl-definition-extractor -h
Usage: cdk-asl-definition-extractor [options]

Extract AWS State Machine definitions from CDK generated ASL

Options:
  -V, --version            output the version number
  -f, --file-name <value>  CloudFormation JSON File
  -h, --help               display help for command
Enter fullscreen mode Exit fullscreen mode

Extract ASL

cdk-asl-definition-extractor  -f </path/to/the/template.json>
Enter fullscreen mode Exit fullscreen mode

The Output

    [
        {
            "identifier": "<StackResourceName>",
            "definition": "<ASL Definition>"
        }
    ]
Enter fullscreen mode Exit fullscreen mode

Wrap Up

Plenty more to come on this and I'll be working to extend/expand its capabilities.

Top comments (3)

Collapse
 
emmamoinat profile image
Emma Moinat

Cool, can I see an example of how your step function looks in CDK? We actually do use the ASL to pass into CDK using @matthewbonig/state-machine and it looks something like this:

readonly someProcessingStateMachine = new StateMachine(this, "SomeProcessingStateMachine", {
    definition: JSON.parse(readFileSync(path.join(__dirname, "processing-state-machine-asl.json"), "utf-8").toString()),
    overrides: {
      "Fetch Some Details": {
        Parameters: {
          FunctionName: this.fetchSomeDetails.lambdaFunction.functionArn
        }
      },
      "Save Some Details": {
        Parameters: {
          FunctionName: this.saveSomeDetails.lambdaFunction.functionArn
        }
      }
    }
  });
Enter fullscreen mode Exit fullscreen mode
Collapse
 
benbpyle profile image
Benjamen Pyle

Here are a few in my repos. They are simple but my production workflows are using the same setup with more complicated machines. I've abandoned using ASL in favor of using the CDK Constructs. I love it

Hope this helps

Collapse
 
emmamoinat profile image
Emma Moinat

Awesome thanks, I will take a look!