One of my favorite games growing up was the 24 Game. In this game, a group of friends took a group of numbers from a card and used the order of operations and basic arithmetic to add, subtract, multiply, and divide the numbers on the card to get to a value of 24. The discovery process to arrive at a solution is complex, and beyond the scope of this demonstration, but we can use this basic paradigm to show how to chain Lambda operations together using Amazon States Language (ASL) to arrive at an answer.
Combined with the Lambda Calculator from the AWS C# SDK, the 24 Game is a good way to demonstrate Amazon Step Functions' ability to incrementally use Lamba functions to arrive at an answer. The scope of this blog is not designed to be the most efficient solution, as basic data manipulation is now possible using JSONata (since Step Functions is billed by State Transition). This blog is intended to give an academic exploration of how ASL works. You can extend the concepts in this blog post to include any of AWS Step Function’s task integrations or use your own code elsewhere in the AWS ecosystem.
The Simple Case
The Lambda Calculator takes an input payload in JSON and performs basic math on these inputs, directed by the "action" property in the JSON. We can reuse this same Lambda in a few places in our ASL definition to accomplish the math we need. Here's an initial ASL definition to get us started by doing some basic arithmetic.
The first state assigns the four numbers from our card [2, 4, 6, 8] to variables [“a”,”b”,”c”, and “d”] to start, the second prepares input parameters for the third step, using “a” and “c” to produce a product (we’ll use “b” and “d” later in the solution), and the third step is a lambda function to do the arithmetic.