Design a state machine in Workflow Studio - AWS Step Functions

Design a state machine in Workflow Studio

In this tutorial, you will learn the basics of working with Workflow Studio for AWS Step Functions. In Design mode of Workflow Studio, you'll create a state machine containing multiple states, including Pass, Choice, Fail, Wait, and Parallel. You'll use the drag and drop feature to search for, select, and configure these states. Then, you'll view the auto-generated Amazon States Language (ASL) definition of your workflow. You'll also use the Code mode of Workflow Studio to edit the workflow definition. Then, you'll exit Workflow Studio, run the state machine, and review the execution details.

In this tutorial, you'll also learn how to update the state machine and view the changes in the execution output. Finally, you'll perform a clean-up step and delete your state machine.

After you complete this tutorial, you'll know how to use Workflow Studio to create and configure a workflow using both the Design and Code modes. You'll also know how to update, run, and delete your state machine.

Step 1: Navigate to Workflow Studio

  1. Open the Step Functions console and choose Create state machine.

  2. In the Choose a template dialog box, select Blank.

  3. Choose Select to open Workflow Studio in Design mode.

Step 2: Create a state machine

In Workflow Studio, a state machine is a graphical representation of your workflow. With Workflow Studio, you can define, configure, and examine the individual steps of your workflow. In the following steps, you use the Design mode of Workflow Studio to create your state machine.

To create a state machine
  1. Make sure you're in the Design mode of Workflow Studio.

  2. From the States browser on the left, choose the Flow tab. Then, drag a Pass state to the empty state labelled Drag first state here.

  3. Drag a Choice state from the Flow tab and drop it below the Pass state.

  4. For State name, replace the default name of Choice. For this tutorial, use the name IsHelloWorldExample.

  5. Drag another Pass state and drop it to one branch of the IsHelloWorldExample state. Then, drag a Fail state and drop it below the other branch of the IsHelloWorldExample state.

  6. Choose the Pass (1) state, and rename it to Yes. Rename the Fail state as No.

  7. Specify the IsHelloWorldExample state's branching logic using the boolean variable IsHelloWorldExample.

    If IsHelloWorldExample is False, the workflow will enter the No state. Otherwise, the workflow will continue its execution flow in the Yes state.

    To define the branching logic, do the following:

    1. Choose the IsHelloWorldExample state on the Canvas and workflow graph, and then under Choice Rules, choose the edit icon in the Rule #1 box to define the first choice rule.

    2. Choose Add conditions.

    3. In the Conditions for rule #1 dialog box, enter $.IsHelloWorldExample under Variable.

    4. Choose is equal to under Operator.

    5. Choose Boolean constant under Value, and then choose true from the dropdown list.

    6. Choose Save conditions.

    7. Make sure the Then next state is: dropdown list has Yes selected.

    8. Choose Add new choice rule, then choose Add conditions.

    9. In the Rule #2 box, define the second choice rule when the IsHelloWorldExample variable's value is false by repeating substeps 7.c through 7.f. For step 7.e, choose false instead of true.

    10. In the Rule #2 box, choose No from the Then next state is: dropdown list.

    11. In the Default rule box, choose the edit icon to define the default choice rule, and then choose Yes from the dropdown list.

  8. Add a Wait state after the Yes state, and name it Wait 3 sec. Then, configure the wait time to be three seconds by doing the following steps:

    1. Under Options, keep the default selection of Wait for a fixed interval.

    2. Under Seconds, make sure Enter seconds is selected, and then enter 3 in the box.

  9. After the Wait 3 sec state, add a Parallel state. Add two Pass states in its two branches. Name the first Pass state Hello. Name the second Pass state World.

    The completed workflow should look like the following diagram:

    Screenshot of IsHelloWorldExample state machine workflow.

Step 3: Review the auto-generated Amazon States Language definition

As you drag and drop states from the Flow tab onto the canvas, Workflow Studio automatically composes the Amazon States Language (ASL) definition of your workflow in real-time. In the Inspector panel panel, choose the Definition toggle button to view this definition or switch to the Code mode to edit this definition as required. For information about editing the workflow definition, see Step 4 of this tutorial.

  • (Optional) Choose Definition on the Inspector panel and view the state machine's workflow.

    The following example code shows the auto-generated Amazon States Language definition for the IsHelloWorldExample state machine. The Choice state that you added in Workflow Studio is used to determine the execution flow based on the branching logic you defined in Step 2.

    { "Comment": "A Hello World example of the Amazon States Language using Pass states", "StartAt": "Pass", "States": { "Pass": { "Type": "Pass", "Next": "IsHelloWorldExample", "Comment": "A Pass state passes its input to its output, without performing work. Pass states are useful when constructing and debugging state machines." }, "IsHelloWorldExample": { "Type": "Choice", "Comment": "A Choice state adds branching logic to a state machine. Choice rules can implement 16 different comparison operators, and can be combined using And, Or, and Not\"", "Choices": [ { "Variable": "$.IsHelloWorldExample", "BooleanEquals": false, "Next": "No" }, { "Variable": "$.IsHelloWorldExample", "BooleanEquals": true, "Next": "Yes" } ], "Default": "Yes" }, "No": { "Type": "Fail", "Cause": "Not Hello World" }, "Yes": { "Type": "Pass", "Next": "Wait 3 sec" }, "Wait 3 sec": { "Type": "Wait", "Seconds": 3, "Next": "Parallel" }, "Parallel": { "Type": "Parallel", "End": true, "Branches": [ { "StartAt": "Hello", "States": { "Hello": { "Type": "Pass", "End": true } } }, { "StartAt": "World", "States": { "World": { "Type": "Pass", "End": true } } } ] } } }

Step 4: Edit the workflow definition in Code mode

The Code mode of Workflow Studio provides an integrated code editor to view and edit the ASL definition of your workflows.

  1. Choose Code to switch to the Code mode.

  2. After the Parallel state's definition, place the cursor and press Enter.

  3. Press Ctrl+space to see the list of states that you can add after the Parallel state.

  4. Choose Pass State from the list of options. The code editor adds boilerplate code for the Pass State.

  5. The addition of this state results in errors in your workflow definition. In the Parallel state's definition, replace "End": true with "Next": "PassState".

  6. In the Pass State definition you added, make the following changes:

    1. Remove the Result node.

    2. Remove "ResultPath": "$.result", and "Next": "NextState".

    3. After "Type": "Pass",, enter "End": true.

    4. Add a , after the Pass State definition.

Your workflow definition should now look similar to the following definition.

{ "Comment": "A description of my state machine", "StartAt": "Pass", "States": { "Pass": { "Type": "Pass", "Next": "IsHelloWorldExample" }, "IsHelloWorldExample": { "Type": "Choice", "Choices": [ { "Variable": "$.IsHelloWorldExample", "BooleanEquals": true, "Next": "Yes" }, { "Variable": "$.IsHelloWorldExample", "BooleanEquals": false, "Next": "No" } ], "Default": "Yes" }, "Yes": { "Type": "Pass", "Next": "Wait 3 seconds" }, "Wait 3 seconds": { "Type": "Wait", "Seconds": 3, "Next": "Parallel" }, "Parallel": { "Type": "Parallel", "Branches": [ { "StartAt": "Hello", "States": { "Hello": { "Type": "Pass", "End": true } } }, { "StartAt": "World", "States": { "World": { "Type": "Pass", "End": true } } } ], "Next": "PassState" }, "PassState": { "Type": "Pass", "End": true }, "No": { "Type": "Fail" } } }

Step 5: Save the state machine

  1. Choose the Config more or choose the edit icon next to the default state machine name of MyStateMachine. In State machine configuration, specify a name. For example, enter HelloWorld.

  2. (Optional) Specify other workflow settings, such as state machine type and its execution role. For this tutorial, keep all the default selections in State machine configuration.

  3. Choose Create.

  4. In the Confirm role creation dialog box, choose Confirm to continue.

    You can also choose View role configuration to go back to the Config mode.

For more information about the Config mode, see Config mode of Workflow Studio.

Step 6: Run the state machine

State machine executions are instances where you run your workflow to perform tasks.

  1. On the State machines page, choose the HelloWorld state machine.

  2. On the HelloWorld page, choose Start execution.

  3. (Optional) Enter a custom execution name to override the generated default.

    Non-ASCII names and logging

    Step Functions accepts names for state machines, executions, activities, and labels that contain non-ASCII characters. Because such characters will not work with Amazon CloudWatch, we recommend using only ASCII characters so you can track metrics in CloudWatch.

  4. In the Input box, enter input values for your execution in JSON format. Based on your input, the IsHelloWorldExample variable determines which state machine flow will be executed. For now, use the following input value:

    { "IsHelloWorldExample": true }
    Note

    While specifying an execution input is optional, in this tutorial, it is mandatory to specify an execution input similar to the above example input. This input value is referenced in the Choice state when you run the state machine.

  5. Choose Start execution.

  6. The Step Functions console directs you to a page that's titled with your execution ID. This page is known as the Execution Details page. On this page, you can review the execution results as the execution progresses or after it's complete.

    To review the execution results, choose individual states on the Graph view, and then choose the individual tabs on the Step details pane to view each state's details including input, output, and definition respectively. For details about the execution information you can view on the Execution Details page, see Execution details overview.

    For this tutorial, if you entered an input value of "IsHelloWorldExample": true, you should see the following output:

    { "IsHelloWorldExample": true }, { "IsHelloWorldExample": true }

Step 7: Update your state machine

When you update a state machine, your updates are eventually consistent. After a short amount of time, all newly started executions will reflect your state machine's updated definition. All currently running executions will run to completion under the previous definition.

In this step, you'll update your state machine in the Design mode mode of Workflow Studio. You'll add a Result field in the Pass state named World.

  1. On the page titled with your execution ID, choose Edit state machine.

  2. Make sure you're in the Design mode.

  3. Choose the Pass state named World on the canvas, and then choose Output.

  4. In the Result box, enter "World has been updated!".

  5. Choose Save.

  6. (Optional) In the Definition area, view the updated Amazon States Language definition of your workflow.

    { "Type": "Parallel", "End": true, "Branches": [ { "StartAt": "Hello", "States": { "Hello": { "Type": "Pass", "End": true } } }, { "StartAt": "World", "States": { "World": { "Type": "Pass", "Result": "World has been updated!", "End": true } } } ], "Next": "PassState" }
  7. Choose Execute.

  8. In the Start execution dialog box that opens in a new tab, provide the following execution input.

    { "IsHelloWorldExample": true }
  9. Choose Start Execution.

  10. (Optional) In the Graph view, choose the World step, and then choose Output. The output is World has been updated!

Step 8: Clean up

To delete your state machine
  1. From the navigation menu, choose State machines.

  2. On the State machines page, select HelloWorld, and then choose Delete.

  3. In the Delete state machine dialog box, type delete to confirm deletion.

  4. Choose Delete.

    If deletion is successful, a green status bar appears at the top of your screen. The green status bar tells you that your state machine is marked for deletion. Your state machine will be deleted when all of its in-progress executions stop running.

To delete your execution role
  1. Open the Roles page for IAM.

  2. Choose the IAM role that Step Functions created for you. For example, StepFunctions-HelloWorld-role-EXAMPLE.

  3. Choose Delete role.

  4. Choose Yes, delete.