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.
A human-readable description of the state machine.
A string that must exactly match (is case sensitive) the name of one of the state objects.
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.
The version of the Amazon States Language used in the state machine (default is “1.0”).
An object containing a comma-delimited set of states.
As an example – the state field contains ‘States’:
Passes input to output without doing any work.
Causes the state machine to wait before transitioning to the next state.
Terminates the state machine successfully.
Terminates the state machine and marks it as a failure.
Adds branching logic to the state machine.
Performs different tasks in parallel.
A State Machine is defined by the states that it contains and the relationships between them.
Here is an example:
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.
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.