Design a state machine in Workflow Studio
In this tutorial, you will learn the basics of working with Workflow Studio for
Amazon 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
-
Open the Step Functions console
and choose Create state machine. -
In the Choose a template dialog box, select Blank.
-
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
-
Make sure you're in the Design mode of Workflow Studio.
-
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.
-
Drag a Choice state from the Flow tab and drop it below the Pass state.
-
For State name, replace the default name of Choice. For this tutorial, use the name
IsHelloWorldExample
. -
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.
-
Choose the Pass (1) state, and rename it to
Yes
. Rename the Fail state asNo
. -
Specify the IsHelloWorldExample state's branching logic using the boolean variable
IsHelloWorldExample
.If
IsHelloWorldExample
isFalse
, 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:
-
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.
-
Choose Add conditions.
-
In the Conditions for rule #1 dialog box, enter
$.IsHelloWorldExample
under Variable. -
Choose is equal to under Operator.
-
Choose Boolean constant under Value, and then choose true from the dropdown list.
-
Choose Save conditions.
-
Make sure the Then next state is: dropdown list has Yes selected.
-
Choose Add new choice rule, then choose Add conditions.
-
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. -
In the Rule #2 box, choose No from the Then next state is: dropdown list.
-
In the Default rule box, choose the edit icon to define the default choice rule, and then choose Yes from the dropdown list.
-
-
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:-
Under Options, keep the default selection of Wait for a fixed interval.
-
Under Seconds, make sure Enter seconds is selected, and then enter
3
in the box.
-
-
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 stateWorld
.The completed workflow should look like the following diagram:
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. TheChoice
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.
-
Choose Code to switch to the Code mode.
-
After the Parallel state's definition, place the cursor and press
Enter
. -
Press
Ctrl+space
to see the list of states that you can add after the Parallel state. -
Choose Pass State from the list of options. The code editor adds boilerplate code for the Pass State.
-
The addition of this state results in errors in your workflow definition. In the Parallel state's definition, replace
"End": true
with
."Next": "PassState"
-
In the Pass State definition you added, make the following changes:
-
Remove the Result node.
-
Remove
"ResultPath": "$.result",
and"Next": "NextState"
. -
After
"Type": "Pass",
, enter
."End": true
-
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
-
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
. -
(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.
-
Choose Create.
-
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.
-
On the State machines page, choose the HelloWorld state machine.
-
On the HelloWorld page, choose Start execution.
-
(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.
-
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. -
Choose Start execution.
-
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.
-
On the page titled with your execution ID, choose Edit state machine.
-
Make sure you're in the Design mode.
-
Choose the Pass state named World on the canvas, and then choose Output.
-
In the Result box, enter
"World has been updated!"
. -
Choose Save.
-
(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" }
-
Choose Execute.
-
In the Start execution dialog box that opens in a new tab, provide the following execution input.
{ "IsHelloWorldExample": true }
-
Choose Start Execution.
-
(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
-
From the navigation menu, choose State machines.
-
On the State machines page, select HelloWorld, and then choose Delete.
-
In the Delete state machine dialog box, type
delete
to confirm deletion. -
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
-
Open the Roles page
for IAM. -
Choose the IAM role that Step Functions created for you. For example, StepFunctions-HelloWorld-role-EXAMPLE.
-
Choose Delete role.
-
Choose Yes, delete.