A Choice based Step Function
First published on: Medium
YOLO. How you doin’?
In our first tutorial which is An Introduction to the AWS Step Function, we tried to explore what is an AWS Step Function. Our second and third tutorial are Amazon States Language: part-1 and Amazon States Language: part-2 where we explored how to define a Step Function with a json schema based language called Amazon States Language. In our fourth tutorial, we saw how to Create a Step Function from the scratch. In this tutorial, we will try to create a step function that uses the Choice feature we described in this tutorial.
Prerequisite
In order to get through this tutorial, you need to have some knowledge about the Amazon States language. Please, follow this tutorial: Amazon State Language: Part - 2
So, let’s go to the console and create a Step function. You can follow this tutorial I wrote on how to create a Step Function from the console.
Now let’s define the Step Function.
Below is the definition of our function:
{
"StartAt": "ChoiceState",
"States": {
"ChoiceState": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.foo",
"NumericEquals": 1,
"Next": "FirstMatchState"
},
{
"Variable": "$.foo",
"NumericEquals": 2,
"Next": "SecondMatchState"
}
],
"Default": "DefaultState"
},
"FirstMatchState": {
"Type": "Pass",
"Result": "Got 1",
"End": true
},
"SecondMatchState": {
"Type": "Pass",
"Result": "Got 2",
"End": true
},
"DefaultState": {
"Type": "Pass",
"Result": "This is the DEFAULT State",
"End": true
}
}
}
So our input will be a parameter named foo
and we will check it’s value. If the value is 1
then our next state will be FirstMatchState
. If the value is 2
then our next state will be SecondMatchState
. If we get any other value, then we will go to the DefaultState
.
The flow looks like this:
Let’s execute the Step Function by clicking the button, Start Execution.
As our input is 1
, it matches the first choice. So our next state will be FirstMatchState
.
Here is the flow:
Similarly, if the input is 2, the next state from ChoiceState
will be SecondMatchState
.
For any other input, the next state from ChoiceState
will be DefaultState
.
So this is how you use the ChoiceState to branch your control flow.
Usage
So, why should we use this ChoiceState
and in which use cases?
If you’re developing your services with an event-driven
architecture, this Choice
state may help.
How?
Say you want to send push notification and sms to your users. For this, you have two different SNS topics. So, you can trigger this Step function when your expected event occurs and with the Choice
state, you can check if the event is for notification or for sms.
Then you can invoker the corresponding SNS
topic.
This is just a small example. There’re so many cool things you can do with Choice
state.
So, that’s all for today.
In the next tutorial, we’ll see how to integrate AWS Lambda with the Step function.