AWS Step Functions and Their Use in Decision Making Part I

In this first part of the article, we will delve into what AWS Step Functions are and how to use them, the second part will then expand as to their integration with DecisionRules when automating decision-making processes.

What are AWS Step Functions?

AWS Step Functions are a serverless orchestration service that allow you to design and manage state machines for your applications. A state machine, in this context, is a sequence of steps, or states, that guide the execution of AWS services. Similar to Azure Logic Apps or Camunda, AWS Step Functions allow you to create and visualize workflows (i.e. state machines) that coordinate the execution of multiple services, making it easier to manage complex processes.

Step Functions commonly appear as structured sequences of states and transitions, resembling a flowchart. These sequences begin with an initial state (a Start node) and end at a final state (an End node). States in Step Functions can be AWS service executions, usually AWS Lambda functions, or they can be choice states that determine conditional branching within the workflow. Error handlers are integrated at points in the sequence to manage exceptions and retries. The visual representation of a Step Function (example pictured below) provides an overview of your workflow.

This example of a Step Function uses connected Lambda functions to make trades on the stock market. First, a Lambda function checks the price of the stock, then it hands it over to another Lambda function to generate a Buy/Sell recommendation which then waits for approval by a human (using a callback). Depending on the approval and the generated recommendation the Step Function then either buys the stock, sells it, or if the trade wasn’t approved it fails.

Creating a Step Function

Creating state machines in AWS Step Functions can be accomplished using different methods, depending on your requirements. 

The usual and simplest way of creating step functions is using the AWS Step Functions Console. This option provides a user-friendly, visual interface for creating state machines. You can use the Step Functions visual designer to drag and drop states, define transitions, and set error handling.

After logging into the AWS Management Console, navigate to the AWS Step Functions service. Click on "Create state machine" next, use the visual designer to build your workflow by adding and configuring states.

You can add states from either the Actions menu, which provides a list of usable AWS services and their corresponding actions. You can also access the Flow menu, where you can find structural building blocks of your workflows, such as if-else logic blocks and error handling nodes which tell the workflow to transition to specific states depending on the error received.

A basic “Hello World” example using step functions

Other Step Function creation options include the following.

A JSON-based definition

This definition specifies the states, their properties, transitions, and error handling as a JSON Object. You can write this definition manually or generate it using code.

After creating the JSON definition, you can use the AWS Management Console or AWS CLI (Command Line Interface) to create a state machine by providing the JSON definition. This approach is more suitable for developers who want to automate the state machine creation process.

And more advanced AWS SDKs or the AWS CLI option

You can create state machines programmatically with the AWS CLI or one of the AWS SDKs (Software Development Kits). These tools provide commands and APIs to interact with AWS Step Functions. Write code or use CLI commands to define your state machine, set its properties, create it, and run it. This approach is highly customizable and suitable for advanced users interested in automating their Step Function creation, as well as integrating them into their already existing ecosystem.

The “Hello World” state machine as described by AWS CLI

Data Flow in Step Functions

In AWS Step Functions, data is passed from one state to another using the concept of "input" and "output" data, similar to DecisionRules.

When the execution of a state machine starts, you provide an initial input data or payload. This input data is passed to the first state in the workflow. Each state within the state machine can access and process this input data, allowing them to operate on specific portions or properties of the data.

As a state completes its execution, it can produce output data. This output data is automatically passed to the next stage in the workflow as its input.

You can specify what data the state emits as its output in its configuration or through the "ResultPath" attribute. The emitted data can be transformed input data, an entirely new data structure, or any relevant information from the state's execution.

There is much more to Input and Output processing in AWS Step Functions, such as the Context Object or using the “ResultPath” but those topics are outside the scope of this article.

Executing a Step Function

A state machine execution occurs when an AWS Step Functions state machine runs and performs its tasks. Each Step Functions state machine can have multiple simultaneous executions, which you can initiate from the Step Functions console, or by using the AWS SDKs, the Step Functions API actions, or the AWS Command Line Interface. An execution receives JSON input and produces JSON output. 

After and during a Step Functions execution, AWS provides us with helpful information about the current and previous states of the execution including each step's Inputs and Outputs. This information is shown via a graph or a table of steps the state machine took.

This concludes Part I of this article. Now that we understand what Step Functions are, and how we can create them, manage their data and execute them, we can look at some examples of how they could help us with business decision-making when used together with DecisionRules.

Thanks for reading!

DecisionRules team

More reading