Supercharge your StepFunctions productivity with local file system sync in Workflow Studio
Published: 2023-01-02 22:10:10

## Note: This article is now outdated, but I'll leave it public anyway. As of re:Invent 2023, Workflow Studio with local file system sync is available natively from within Application Composer. I hope that this article served as inspiration to AWS when deciding to build this.

---

One of my favourite AWS services is Step Functions. It's a serverless visual workflow orchestration service which, once you are up to speed with it, helps you build extremely scalable and resilient applications.

At [Mathem](https://www.mathem.se), we are heavy users of it as an integral part of our serverless architecture. At the time of writing we have over 70 state machines running in production.

With heavy usage comes ideas on how you want the perfect developer experience to be.

The release of [Workflow Studio](https://docs.aws.amazon.com/step-functions/latest/dg/workflow-studio.html) in 2021 was a huge leap in the right direction. It lets developers quickly and intuitively design visual workflows using drag and drop instead of writing ASL in YAML, JSON or CDK.

This post assumes that you have some pre-existing experience of building state machines in Step Functions, preferably using Workflow Studio.

## The problem
Despite how great Workflow Studio is, there is a gap in the full end-to-end developer experience. Once the workflow is designed, the developer has to go through the following manual steps:
1. Export the workflow as YAML or JSON
2. Import the file in their project
3. Replace placeholders in the YAML with [DefinitionSubstitutions](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html#sam-statemachine-definitionsubstitutions). 
In the vast majority of development scenarios, the workflow isn't perfect after the first iteration - it normally needs several iterations. If iterating requires manual steps, then it'll get very costly, not to mention boring, for the developer. For each change, the developer needs to:
4. Convert their ASL to JSON (if it isn't already)
5. Import it back into Workflow Studio
6. Make changes
7. GOTO 1 

Below is a state machine describing the above steps. The steps with a sad smiley are the ones we want to eliminate.
![DX state machine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mpnlnjl9divmbcbw07jt.png)

## The solution
My ideal solution, alike my #1 AWS wishlist item, would be an AWS supported version of Workflow Studio as a VSCode extension with a bidirectional sync between the designer and the ASL definition. Until we have that I will support this workaround.

### Getting started
First, you need to install a Chrome extension from [github.com/ljacobsson/sfn-workflow-studio-sync](https://github.com/ljacobsson/sfn-workflow-studio-sync). The instructions are in the readme.

### Link Workflow Studio with your local ASL file
Next, you'll need to navigate to [Workflow Studio](https://eu-west-1.console.aws.amazon.com/states/home?region=eu-west-1#/visual-editor?flowType=create). You can also open an existing state machine and choose Edit and click through to Workflow Studio.

If the extension was successfully installed, you'll see a new button at the top right corner: 
![Extension buttons 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqlrckkp6j2bzy7a96oq.png)

Clicking that will bring up a file picker dialog. This is using the [File System Access API](https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API) to create a temporary read/write access between the webpage and the selected file for the duration of the page's lifetime.

Keep your ASL file in one window and Workflow Studio in the other and watch the time saving as it happens: 
![Demo 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wa39ndqpn0hie1ovm1sm.gif)

This solves the export/import manual tasks in the developer experience flow from above, but we want to automate the linking of resources in the SAM template and the ASL definition. This is of course only applicable if you are a SAM user.

### Link your SAM template with Workflow Studio

After the ASL file sync has been established, you'll notice two new buttons in the top right corner of the studio:
![Extension buttons](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1wtu8z66pap92pj0vod.png)

The 'Force sync'-button forces the sync to happen. In some cases it's needed, for example after changing Choice conditions.

The other button lets us link a SAM template to Workflow Studio in order to do the definition substitution mapping.

Some resource types, for example Lambda, offer a drop down in the GUI to select a hard-coded reference. Using a hard coded reference is normally a bad idea. In order to create a substitution, select 'Enter' at the top of the dropdown list.

This will bring up a dropdown representing the resource types in your SAM template along with their available attributes:
![Dropdown 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/grnt7pk810sh3vu1s2vp.png)

Selecting an option will update both your SAM and ASL simultaneously:
![Simultaneous file sync](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ln5ja72pqhp5scw55qv.gif)

Most resource types, however, require you to complete JSON to set up your integrations. Here you simply type in the substitution variable you want to use:
![JSON substitution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqx6j7q2kcjsxjd9nh43.png)

You can add more than one substitution variable per JSON configuration:
![Demo 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/re3gs70p9hh04yd4j33y.gif)

## Summary
This extension is heavily dependent on the DOM of the Workflow Studion, which I have no control over. Chances are that AWS will release a breaking change at any time. If that happens, please submit an [issue](https://github.com/ljacobsson/sfn-workflow-studio-sync/issues/new) and I'll do my best to fix it.

AWS prioritises new features based on customer feedback, so if you like this, please submit a feature request to AWS for official support <3

Follow me on Twitter ([@lajacobsson](https://twitter.com/lajacobsson)) for updates on this and other serverless developer experience tools I maintain.