Amazon States Language Part-2

First published on: Medium

In our first tutorial which is An Introduction to the AWS Step Function, we tried to explore what is an AWS Step Function. In our second tutorial which is Amazon States Language: part-1, we tried to learn how to define a Step Function with a json schema based language called Amazon States Language. In this part, we will continue to explore the Amazon States Language.

YOLO. I hope you’re doing well. Today I’ll be discussing a bit more on Amazon States Language. In part 1, I discussed some of the Amazon States Language’s keyword. Can you remember them? Well if you don’t let’s recap.

We discussed Comment, StartAt, States and Type keywords. And I also mentioned that Type keywords take the following values:

  1. Task
  2. Choice
  3. Pass
  4. Wait
  5. Succeed
  6. Fail
  7. Parallel
  8. Map

I discussed the Task in the first part. In this module, I’ll discuss the Choice and Passstate in this tutorial.

Choice

Let’s look at the previous code again.

{   
    "Comment": "An example of the Amazon States Language using a choice state.",   
    "StartAt": "FirstState",   
    "States": {     
        "FirstState": {       
            "Type": "Task",       
            "Resource": "arn:aws:lambda:us-east-1:123456789012:function:FUNCTION_NAME",       
            "Next": "ChoiceState"     
        },     
        "ChoiceState": {       
            "Type" : "Choice",       
            "Choices": [         
                {           
                  "Variable": "$.foo",           
                  "NumericEquals": 1,           
                  "Next": "FirstMatchState"         
                },         
                {           
                  "Variable": "$.foo",           
                  "NumericEquals": 2,           
                  "Next": "SecondMatchState"         
                }       
            ],       
            "Default": "DefaultState"     
        },      
        "FirstMatchState": {       
          "Type" : "Task",       
          "Resource": "arn:aws:lambda:us-east-1:123456789012:function:OnFirstMatch",       
          "Next": "NextState"     
        },      
        "SecondMatchState": {       
            "Type" : "Task",       
            "Resource": "arn:aws:lambda:us-east-1:123456789012:function:OnSecondMatch",       
            "Next": "NextState"     
        },      
        "DefaultState": {       
            "Type": "Fail",       
            "Error": "DefaultStateError",       
            "Cause": "No Matches!"     
        },
        "NextState": {       
            "Type": "Pass",       
            "Next": "PassState"    
        }      
        "PassState": {       
            "Type": "Task",       
            "Resource": "arn:aws:lambda:us-east-1:123456789012:function:FUNCTION_NAME",       
            "End": true     
        }   
    } 
}

So, what is Choice? The Choice is a state where you can choose which state to go based on a certain variable. Let’s look at it closely.

"ChoiceState": {       
  "Type" : "Choice",       
    "Choices": [         
      {           
        "Variable": "$.foo",           
        "NumericEquals": 1,           
        "Next": "FirstMatchState"         
      },         
      {           
        "Variable": "$.foo",           
        "NumericEquals": 2,           
        "Next": "SecondMatchState"         
      }       
    ],       
    "Default": "DefaultState"     
}

"Type": "Choice" is saying that this state is a choice type state. And this state takes a list of choices like this:

"Choices": [
  {
    # Your choice here
  }
  {
    # Your another choice here
  }
]

As we can see from our code above,

"Choices": [         
      {           
        "Variable": "$.foo",           
        "NumericEquals": 1,           
        "Next": "FirstMatchState"         
      },         
      {           
        "Variable": "$.foo",           
        "NumericEquals": 2,           
        "Next": "SecondMatchState"         
      }       
]

The Choice state takes 2 choices. The first choice is made based on a variable foo. [N.B. If you’re worried about the $. sign, please don’t. I’ll discuss it later.]

So it says,

Check the variable foo and if it’s value equals to 1 then go to the FirstMatchState.

In the second choice, we can see that, if the value of the variable foo is Numeric and equals to 2 the next state is SecondMatchState.

NB: This is kind of like a if-else block.

if the value of the variable is 1: 
    go to FirstMatchState 
else if the value of the variable is 2:
    go to SecondMatchState

If you’re thinking where this value is coming from, don’t sweat it. Just assume there’s a variable named foo . We’ll discuss it later.

So the summary of the Choice state is, based on the value of a variable, you can decide which state you should choose next.

The Default keyword means if the value of the variable doesn’t match with the first two choices you’ve written then the next state will be the state you’ve written in the Default field, which is the DefaultState in our snippet.

One thing to note, if you do not give a Default choice and the value of the variable doesn’t match any of the choices you’ve written in the Choice state, the step function’s execution will fail.

Let’s explore the Fail type.

Fail

Let’s look at this code snippet.

"DefaultState": {       
  "Type": "Fail",       
  "Error": "DefaultStateError",       
  "Cause": "No Matches!"     
},

It’s the Default state we mention in the ChoiceState . So if the value of our variable foo is neither 1 nor 2 then we will go to this state. The definition of the state is given the above snippet.

"Type": "Fail" means you want the state to fail. Therefore the execution of the step function will fail. You can also set the custom error message in a field name Error . You can also set the cause in the field Cause which will tell you why this state fails.

So, you wanted your variable foo to have a Numeric value and you want if the value is, 1 then go to a state where you want to do something based on this value. If the value is, 2 then go to another state and do something based on this value. But if the value is neither 1 nor 2 then it means something went wrong and you don’t have any state based on this value. So a safe option is to fail the execution of the step function as your system doesn’t know what to do with an unknown value. And if you fail a state you can define in the state why it failed and your error message.

Let’s discuss the Pass type.

Pass

"Type": "Pass" means you don’t want to process anything in this state and just pass the execution to the next state. Let’s look at our snippet.

"NextState": {       
  "Type": "Pass",       
  "Next": "PassState"    
}

The type of this state is Pass , so we don’t want to do any processing in this step. We simply pass the execution to the next state which is mention in the field "Next": "PassState".

So that’s it for today. We will discuss the rest of the type in another module.


Our next tutorial is Creating a basic AWS Step Function, where we will create a Step Function from the scratch!

You can get the whole tutorial series from here: A Journey to the AWS Step Function.

See you in the next tutorial!