Conditional Workflow State Machine (Part 2)
Step 4
Now if the even flag is false, we are going to call a parallel state. You can setup whatever state you want but for the purposes of this demonstration, I'll be demoing a parallel state.
In a parallel state, we can add two different processes that will run in parallel at the same time. This state will involve the same lambda function, but it will pass two different parameters to our lambda function.
We want to convert a number to binary and roman numerals, but we want to do it in parallel.
We will be passing a flag 'is_binary = true' for binary and to convert the number to roman we will be passing a different flag 'is_binary = false'.
We need to add 'pass state'. This state will pass a parameter but a different parameter to our lambda function.
So, lets add the 'pass state' from the state browser and give it a suitable name.
This state will use the same input as the parallel state, so we'll leave that config as it is.
Since this state will be passing the parameter to the next lambda function, we will change some settings in the output tab.
In the output tab, select the check box 'Add original input to output using resultpath'.
Insert the flag, '$.is_binary' and we are going to set it to true. (see below)
Next, we will add the pass state for the roman numeral flag.
Give it a suitable name and in the output, we'll add the not binary flag settings as shown below.
Step 5
Now we add the invoke lambda function for both pass states. and choose your second lambda function we created earlier that converts the numbers.
Once done, go ahead and create. This particular step function is going to add execution roles. Policies for the S3 bucket will be created manually so we will be adding that in the next steps.
In the IAM Console, we will be adding permissions for the S3 bucket. Find your newly created IAM role and grant full S3 access to the role. The role should currently have two policies attached one for Lambda and the other for the Xray access.
Added S3 permissions access.
Step 6
Now our state machine is ready. We can start execution.
In the JSON code, we'll pass the numbers we did earlier to our lambda code in step 1.
10 + 5 is 15 which should make the results odd.
Below you can see our parallel state ran. There is the option to view the function in event and state view. You can also check the input and output of all your states for more detailed view.
State view.
Below you can see that the choice state has the output from the lambda function (also the input). Based on the output of the choice state, our parallel state was triggered as the result is "false" from the "is_even" flag.
If you click the input tab, you will see that the parallel state input has the output of the earlier lambda invoke function.
Below you can see the parallel state has the two combined outputs of both branches below it. follow the tree map for more understanding.
Step 7
Let's execute our state with even numbers to trigger the copy object state (to signify the function is in a failed state).
Start a new execution and change the 5 to 2 so we can get an even number.
When we run this new execution, we will see that it gives a fail error.
This is because we actually added a fil state as part of the process but if you drill into the step process, it should have copied the data you're attempting to move over.
Both the input and output will give the same reason message.
You can see the copyobject is green which means the copyobject state succeeded.
output message.
If we go to our s3 bucket, you should see the action completed.
Below we have the output folder and inside the file that has been copied.
This is how you can execute and orchestrate multiple tasks using state machines and state machines will help a lot as you can see the output from every state, and you can pass parameters and inputs including outputs.
Subscribe to my newsletter
Read articles from teju fam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by