Amazon States Language Part-1
First published on: Medium
In our previous tutorial, which is an Introduction to the AWS Step Function, we tried to learn about what is Step Function. In this module, we will learn about Amazon States Language which is actually the definition of a step function.
When we develop software, we do this by writing some commands. These commands get executed by the machine and the output is our software. As we don’t feel comfortable with machine code, we use different programming languages. Similarly, to develop a step function, we need a language. And the name of this language is Amazon States Language.
Amazon States Language
Amazon States Language is like a json
type of language. It looks like a key-value type language, where the key is your instruction name, and value is what you want it to do.
So, with this json
based on Amazon States Language you define your states, you tell your states where to go next when to stop, what to do if it fails, what to do if it succeeds.
From Amazon doc, the following is how Amazon States Language looks like:
{
"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": "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:FUNCTION_NAME",
"End": true
}
}
}
Aaahhh!!! Looking Gibberish!!! Just take a deep breath. I’m going to explain every one of them. So here we go..…
As I told earlier, the Amazon State Language is a json
based language i.e. key-value type. So you will describe each of your states with a key and what this state should do will be stored as the value of this key.
So let’s explain what we saw up there.
"Comment": "An example of the Amazon States Language using a choice state.",
As you can see, this is a key-value pair where the name of the key is Comment
. Just like the other programming language, when the step function will start execution, it’ll omit the comment. But Comment
is a nice way to document what your state will do!
"StartAt": "FirstState"
Now, each of the step function has to have this key, StartAt
. This tells the step function which state is the start state of the step function. Because step function needs a state from where it can start execution.
The above line saying, start the execution of this step function from the state FirstState
and execute what has been described in the FirstState
.
"States": {}
This is the skeleton of your step function. The key States
contains all the states of your step function. As all your states can do any simpler or complex task, they should be treated as objects.
As you can see from the above example, FirstState, ChoiceState, FirstMatchState, SecondMatchState, DefaultState, NextState
we have these 6 states in our step function. All of these steps do different tasks. That’s why we listed all of them inside States:
key and told our step function that if you want all the states of our step function, you can find these inside States
key.
FirstState
This first state of our step function is FirstState
. Inside the FirstState key, we defined how this state will work.
The definition is like the following:
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:FUNCTION_NAME",
"Next": "ChoiceState"
The Type
keyword defines what is the role of this state. The value Task
means this state will do a task. In order to do the task what resources we should use is defined in the Resource
key. Here you can see, the resource key points to a Lambda function
.
So what are we doing in this state is basically like this:
As described in this image, the task of FirstState
is to invoke a lambda function. The output of this state is what the lambda function returns back. With this output, where this state should go? It’s defined in the Next
key. As the Next
key defines, ChoiceState
, so the execution will go to the ChoiceState
and it’ll start executing from there.
This module is getting bigger. So I’ll discuss the rest of the states, ChoiceState, FirstMatchState, SecondMatchState, DefaultState, NextState
in the next module.
But before finishing this module one more thing to say. The keyword Type is required field in state and can have only a fixed number of values. They are:
- Task
- Choice
- Pass
- Wait
- Succeed
- Fail
- Parallel
- Map
I’ve discussed the
Task
type in this module. In the next module, Amazon States Language: part-2, I’ll discuss theChoice, Fail
step.
The first of this tutorial series is An Introduction to the AWS Step Function.
You can get the whole tutorial series from here: A Journey to the AWS Step Function.