Working with AWS Step Functions - AWS Toolkit for VS Code

Working with AWS Step Functions

You can use the AWS Toolkit for Visual Studio Code (VS Code) to perform various operations with state machines.

Prerequisites

  • Be sure your system meets the prerequisites specified in Installing the Toolkit for VS Code, then install the toolkit.

  • Ensure that you have configured your credentials before opening the AWS Explorer.

Work with state machines in VS Code

You can use VS Code to interact with remote state machines, and develop state machines locally in JSON or YAML format. You can create or update state machines, list existing state machines, run them, and download them. VS Code also lets you create new state machines from templates, see a visualization of your state machine, and provides code snippets, code completion, and code validation.

List existing state machines

If you've already created state machines, you can view a list of them:

  1. Open the AWS Explorer.

  2. Select Step Functions

  3. Verify that it lists all the state machines in your account.

    AWS Explorer menu showing regions and services, with LambdaStateMachine highlighted under Step Functions.

Download a state machine

To download a state machine:

  1. In the AWS Explorer, right click the state machine that you want to download.

  2. Select Download, then select the location where you want to download the state machine.

  3. Verify that it downloaded correctly.

    AWS Explorer menu showing LambdaStateMachine with Download Definition option highlighted.

Create a state machine

You can create a new state machine yourself, or you can use a template. For more information on creating a state machine from a template, see the State Machine Templates section. To create a new state machine:

  1. Create a new Amazon States Language (ASL) file with your state machine definition. Use the menu at the bottom right to set it as Amazon States Language.

  2. Select Publish to Step Functions.

    JSON code snippet showing state machine configuration with FirstState and ChoiceState.
  3. Select Quick Create, choose a role, and name your state machine.

    Quick Create option highlighted for creating a state machine with default settings.
    Dropdown menu showing execution role options, with ECSRole highlighted for selection.
    Dialog box prompting user to name their state machine, with input field and instructions.

Update a state machine

To update a state machine:

  1. Edit the ASL file with your state machine definition.

  2. Select Publish to Step Functions.

    JSON code snippet showing state machine configuration with FirstState and ChoiceState.
  3. Select Quick Update, then select the state machine you want to update.

    Quick Update option highlighted in the Publish to AWS Step Functions menu.
    Dropdown menu showing state machine options for updating in us-east-1 region.

Run a state machine

To run a state machine:

  1. In the AWS Explorer, right click the state machine that you want to run.

  2. Provide input for the state machine. You can try both input from a file, and input in a text box.

  3. Start the state machine and verify that it runs successfully.

    AWS Explorer menu showing LambdaStateMachine with Start Execution option highlighted.
    Start Execution interface for LambdaStateMachine with JSON input option and example.

State machine templates

When you create a state machine, you have the option to create it from a template. The template contains a sample state machine definition with several commonly used states, and provides you with a starting point. To use state machine templates:

  1. Open the Command Palette in VS Code.

  2. Select AWS Toolkit Create a new Step Functions state machine.

  3. Choose the template you want to use.

    List of starter templates including Hello world, Retry failure, Wait state, and others.
  4. Choose whether you want to use the JSON or the YAML template format.

    Dropdown menu showing JSON and YAML options for selecting a template format.

State machine graph visualization

Graph visualizations let you see what your state machine looks like in graphical format. When you create a graph visualization, another tab will open and display a visualizion of the state machine JSON or YAML. You can then compare the state machine definition you are writing concurrently with its visualization. As you change your state machine definition, the visualization will be updated.

Note

To create a visualizion of a state machine definition, the definition must be open in the active editor. If you close or rename the definition file, the visualization will close.

To create a state machine graph visualization:

  1. Define your state machine.

  2. Open the Command Palette in VS Code.

  3. To create a visualization, use the visualization button in the upper right corner, or choose AWS Render graph.

    JSON code and graph visualization of Amazon States Language with choice state example.

Code snippets

Code snippets let you insert short sections of code. To use code snippets:

  1. Open a file and save it with the extension .asl.json for JSON format, or .asl.yaml for YAML format.

  2. Create a new state machine with the States property.

  3. Place the cursor within States.

  4. Use the key combination Control + Space, and select your preferred code snippet.

  5. Use Tab to traverse the variable and parameters in the code snippet.

  6. Test Retry and Catch snippets by placing the cursor within the related state.

    Code snippet for Lambda Task State with dropdown menu showing various state types.

Code completion and validation

To see how code completion works:

  1. Create several states.

  2. Place the cursor after a Next, StartAt, or Default property.

  3. Use the key combination Control + Space to list available completions. Additional properties can be accessed using Control + Space again, and will be based on the Type of the State.

  4. As you work, code validation will happen for:

    • Missing properties

    • Incorrect values

    • No terminal state

    • Nonexistent states that are pointed to

    JSON code snippet showing state machine structure with task, choice, and default states.
    Code snippet showing AWS Lambda function properties with a tooltip describing the Catch property.