Sub topic 2
State machines and Amazon States Language
This chapter assumes you can read JSON and have used the AWS CLI or console. It also assumes you know what a Lambda function is. You will learn how a state machine is structured and how to write a valid definition, and you will avoid the structural errors that most often make a first definition fail.
What a state machine is
A state machine is a set of named steps, called states, and rules for moving between them. At any moment an execution is in exactly one state. When that state finishes, the execution follows a transition to the next state, until it reaches a terminal state.
In Step Functions, you describe a state machine in Amazon States Language (ASL). ASL is a JSON format. Step Functions stores the definition, and each run of it is an execution with its own input, current state, and output.
The top-level structure
A definition is a JSON object with three fields you will use constantly:
| Field | Required | Purpose |
|---|---|---|
| `StartAt` | Yes | Name of the first state to run |
| `States` | Yes | Object mapping each state name to its definition |
| `Comment` | No | Free-text description |
State names are the keys of States. They must be unique within the machine and at most 80 characters long.
The fields every state shares
Each state object has a Type. The types are Task, Choice, Wait, Pass, Succeed, Fail, Parallel, and Map. Later chapters cover each one in depth.
Most states also say what happens next, using one of two fields:
Nextnames the state to run after this one.End: truemarks this state as a terminal state.
A state must not have both. Choice, Succeed, and Fail are special cases. Choice picks its target with rules, while Succeed and Fail end the execution and take neither Next nor End.
A first definition
This machine has a Pass state, which passes data along and can inject a fixed result, followed by a Succeed state.
{
"Comment": "Smallest useful state machine",
"StartAt": "SetGreeting",
"States": {
"SetGreeting": {
"Type": "Pass",
"Result": { "greeting": "Hello" },
"Next": "Done"
},
"Done": {
"Type": "Succeed"
}
}
}Save it as definition.json. You can check its syntax and structure before creating anything:
aws stepfunctions validate-state-machine-definition \
--definition file://definition.jsonThe response contains "result": "OK" when the definition is valid. Otherwise it lists diagnostics with a severity, a code, and a location.
How an execution moves
Every execution starts at the StartAt state with the execution input as that state's input. The state produces an output, and that output becomes the next state's input. This continues until a terminal state runs. The final output becomes the execution's result.
For this example, the Pass state ignores its input and outputs {"greeting": "Hello"}. Done receives that and returns it unchanged.
Common structural errors
- Dangling reference.
StartAtor aNextvalue names a state that does not exist, often through a typo or a rename. - No terminal state. Every path needs to end in
End: true,Succeed, orFail. - Both
NextandEnd. Pick one. - Unreachable state. A state that no transition leads to is rejected, so remove it or connect it.
- Mismatched field names. ASL is case-sensitive, so
startAtis notStartAt.
Exercise
Exercise
A definition has "StartAt": "Validate", but its States object has a state named ValidateOrder and no state named Validate. Another state has both "Next": "Done" and "End": true. Name the two errors.
Show answer
The first is a dangling reference. StartAt names a state that does not exist, so it must be changed to ValidateOrder or the state renamed. The second is a state that sets both Next and End. It must use only one: Next to continue, or End: true to terminate.