Using AWS Step Functions in CFML: Making Choices and Branching in a Step Functions Workflow
Posted 30 April 2019
Thus far in this first example Step Functions workflow that performs image analysis on a randomly selected image, our workflow has progressed in a purely linear fashion. We started at the first step in our workflow, and moved to the second step. We could continue to move linearly from one step to another, but how often do you build applications (or even individual functions) that are purely linear in execution? We often have to make choices based on the state of the application at any given time. One of the great powers of the Step Functions environment is that we can make choices and take different paths of execution based off those choices.
Now that we’ve generated a random number betwen 1 and 100 in the generateRandomNumber task, and put the result of that task into a variable titled $.randomNumber, we pass that $.randomNumber value into our next state, “ChoiceState,” the definition of which is as follows:
“ChoiceState” is, obviously, a state where you make a choice. Execution continues based on the choice that is evaluated in the state definition.
How Choice States Work
Choice states are, effectively, the “if-then-else” flow constructs in a Step Functions workflow. Choice states are made up of an array of choices. Each choice has a comparator that lets the Step Functions execution environment decide which choice should be made. Additionally, each choice state has a default state which will automatically be chosen if there is no match to any of the comparators in the array of choices. There many comparators to chose from, including:
In our case, we have a random number as our input, so our comparators will be numeric comparators. The first choice in the array of chocies says “If $.randomNumber is less than or equal to 50, the next step in this execution will be setDataForImageOne.”
The next (and final) choice in our array of choices says “If $.randomNumber is greater than 50, the next step in this execution will be setDataForImageTwo.”
Finally, we have our default option, which exists outside of the array of choices and will automatically be selected if there is no matching comparator in the array of choices. We should never arrive at the default choice in this workflow because our randomly generated number will always be between 1 and 100. We still have to specify a default state, however. If we were to somehow reach it in this workflow, there’s an error in our execution.
Next up, we’ll look at the setDataForImageOne and setDataForImageTwo states, which demonstrate how you can inject key-value pairs into the current set of data being passed through your Step Function workflow.