AWS Step Functions is a web-based orchestration service that enables you to coordinate the components of an application into logical steps using individual components that each perform a discrete function or task. This allows for easier scalability of processes and decreases the amount of time needed to change applications.
Some of the benefits of step functions are:
- Manages the operations and underlying infrastructure to provide application availability at any scale.
- Tasks in the step functions can run in the AWS Cloud, on on-premises servers or any other system that has access to AWS.
- Uses a graphical interface to build and connect the step functions as well as a JSON-based scripting language called ‘Amazon States Language’.
- Provides a console for displaying a graphical view of your state machine’s structure and logic as well as to monitor executions.
- Step functions integrate with any other AWS service and can call other executions or actions such as a Lambda or a DynamoDB action directly.
Step functions are designed and created using a JSON-based language and execution of a step function is accomplished in one of three ways:
- Issue a ‘StartExecution’ to the Step Functions ‘arn’.
- CloudWatch events.
- API Gateway.
Step functions allow the user to see the workflow in a visual/graphical way during the design and the execution of the Step Functions.
Below is a description of a few of the basic step function language constructs used to create step functions.
Comment (Optional)
A human-readable description of the state machine.
StartAt (Optional)
A string that must exactly match (is case sensitive) the name of one of the state objects.
TimeoutSeconds (Required)
The maximum number of seconds an execution of the state machine can run. If it runs longer than the specified time, the execution fails with a ‘States Timeout’ error.
Version (Optional)
The version of the Amazon States Language used in the state machine (default is “1.0”).
States (Required)
An object containing a comma-delimited set of states.
As an example – the state field contains ‘States’:
{ "State1" : { }, "State2" : { }, ... }
Pass (Optional)
Passes input to output without doing any work.
Wait (Optional)
Causes the state machine to wait before transitioning to the next state.
Succeed (Optional)
Terminates the state machine successfully.
Fail (Optional)
Terminates the state machine and marks it as a failure.
Choice (Optional)
Adds branching logic to the state machine.
Parallel (Optional)
Performs different tasks in parallel.
State Machines
A State Machine is defined by the states that it contains and the relationships between them.
Here is an example:
{ "Comment": "A Hello World example of the Amazon States Language using a Pass state", "StartAt": "HelloWorld", "States": { "HelloWorld": { "Type": "Pass", "Result": "Hello World!", "End": true } } }
This state machine contains a single function that emits a ‘HelloWorld’ as its output. The ”Type” : “Pass” statement in the above step function indicates that input passed to this state machine will be passed on to the next step in the state machine logic flow. The ”Result” : “HelloWorld!” statement indicates that the ‘HelloWorld!’ text will be passed to the next step.
Let us look at a more complicated Step Function. In this example, we will demonstrate how to make decisions in a Step Function and how to branch accordingly based on the decision. The following shows the basic flow of this Step Function:
In this simple example, the Step Function enters a ‘Pass’ state first. A Pass state passes its input to its output without performing any work. This can be useful for debugging Step Functions. Next, the Step Function enters a ‘Choice’ state which allows branching logic to occur. A Choice state can utilize 16 different comparison operators and can be combined with ‘And’, ‘Or’ and ‘Not’ operators.
Following the Choice state ‘Yes’ branch, there is a ‘Wait’ state which allows the Step Function to pause for a designated amount of time. After the ‘Wait’ state, the Step Function enters a ‘Parallel’ state to create parallel branches of execution in your Step Function. At this point, using the Parallel state, two different execution branches are created; ‘Hello’ and ‘World’. This demonstrates one example of the power of Step Functions and the use of the parallel processing capabilities.
Here is the Amazon Step Language code that would be written to achieve the previous Step Function and all of the steps involved.
{ "Comment": "A Hello World example demonstrating various state types of the Amazon States Language", "StartAt": "Pass", "States": { "Pass": { "Comment": "A Pass state passes its input to its output, without performing work. Pass states are useful when constructing and debugging state machines.", "Type": "Pass", "Next": "Hello World example?" }, "Hello World example?": { "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", "Type": "Choice", "Choices": [ { "Variable": "$.IsHelloWorldExample", "BooleanEquals": true, "Next": "Yes" }, { "Variable": "$.IsHelloWorldExample", "BooleanEquals": false, "Next": "No" } ], "Default": "Yes" }, "Yes": { "Type": "Pass", "Next": "Wait 3 sec" }, "No": { "Type": "Fail", "Cause": "Not Hello World" }, "Wait 3 sec": { "Comment": "A Wait state delays the state machine from continuing for a specified time.", "Type": "Wait", "Seconds": 3, "Next": "Parallel State" }, "Parallel State": { "Comment": "A Parallel state can be used to create parallel branches of execution in your state machine.", "Type": "Parallel", "Next": "Hello World", "Branches": [ { "StartAt": "Hello", "States": { "Hello": { "Type": "Pass", "End": true } } }, { "StartAt": "World", "States": { "World": { "Type": "Pass", "End": true } } } ] }, "Hello World": { "Type": "Pass", "End": true } } }
State Machine Execution
AWS provides a console with a user interface that contains all of the tools needed to design, build and debug the Step Functions that are being developed and deployed. During the creation of a Step Function, there are two different regions displayed on the console; a graphical representation of the steps in the State Machine and a region below where the code is written to make up the State Machine. Once the code is complete to your satisfaction, you can create the final State Machine, simulate the execution and examine the logs output by AWS to provide feedback on what occurred within the State Machine.
State Machine/Step Function benefits
- States as services inside a server–less environment
- Easy integration and monitoring of human tasks
- Integrates nicely with other AWS services such as Lambdas, API Gateway, CloudWatch, Metrics and Cloud Trail
- Visual console making development, testing and implementation of complex workflows easy and transparent
- Security integration with AWS IAM since each State Machine is provided a service role.