Sub topic 8
Handling failures with Retry and Catch
This chapter assumes you can read a basic Amazon States Language (ASL) definition and know what a Task state does. It also assumes you know why retries need backoff. Here you will add Retry and Catch fields to a Task state and learn how Step Functions evaluates them.
How errors are named
When a state fails, Step Functions raises an error name. A Lambda function that throws a custom exception produces an error named after the exception class. Service failures use names such as Lambda.ServiceException. Step Functions also defines built-in names:
| Error name | Meaning |
|---|---|
| `States.ALL` | Matches any error name |
| `States.Timeout` | The state ran past `TimeoutSeconds`, or no heartbeat arrived within `HeartbeatSeconds` |
| `States.TaskFailed` | Any failure from a Task state except `States.Timeout` |
| `States.Permissions` | The execution role lacks permission for the call |
| `States.Runtime` | An internal problem, such as a bad path. It can't be retried or caught |
Retry and Catch both match on these names through ErrorEquals.
Retry: try the same state again
Retry is an array of retriers. Step Functions checks them in order and uses the first one whose ErrorEquals matches. Each retrier keeps its own attempt counter.
IntervalSecondsis the wait before the first retry. The default is 1.MaxAttemptsis the number of retries. The default is 3, and 0 means never retry.BackoffRatemultiplies the wait after each attempt. The default is 2.0.MaxDelaySecondscaps the wait.JitterStrategyset toFULLrandomizes the wait, which keeps many failing executions from retrying at the same moment.
Catch: move on after retries run out
Catch runs only after Retry has no retrier left to apply, or when no retrier matches. It sends the execution to the state named in Next. Set ResultPath in a catcher. Without it, the default is $, so the error object replaces the state's whole input. With "ResultPath": "$.error", the error is stored next to the original input, and the fallback state still sees both.
{
"StartAt": "ChargeCard",
"States": {
"ChargeCard": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "arn:aws:lambda:us-east-1:123456789012:function:ChargeCard:$LATEST",
"Payload.$": "$"
},
"ResultPath": "$.charge",
"TimeoutSeconds": 30,
"Retry": [
{
"ErrorEquals": ["Lambda.ServiceException", "Lambda.TooManyRequestsException", "States.Timeout"],
"IntervalSeconds": 2,
"MaxAttempts": 4,
"BackoffRate": 2.0,
"MaxDelaySeconds": 20,
"JitterStrategy": "FULL"
}
],
"Catch": [
{
"ErrorEquals": ["CardDeclined"],
"ResultPath": "$.error",
"Next": "NotifyCustomer"
},
{
"ErrorEquals": ["States.ALL"],
"ResultPath": "$.error",
"Next": "ChargeFailed"
}
],
"End": true
},
"NotifyCustomer": {
"Type": "Pass",
"End": true
},
"ChargeFailed": {
"Type": "Fail",
"Error": "ChargeFailed",
"Cause": "Payment could not be completed"
}
}
}Here the transient service errors and timeouts are retried up to four times, with waits of about 2, 4, 8, and 16 seconds before jitter. A CardDeclined error is a permanent business failure. It skips retries and goes straight to NotifyCustomer. Anything else lands in ChargeFailed.
Common mistakes
- Putting
States.ALLfirst. It matches everything, so it must be last in the array and the only entry in itsErrorEquals. The same rule applies to retriers. - Retrying non-idempotent work. A retry may follow a call that partly succeeded. Make the task safe to run twice, for example with an idempotency key.
- Omitting
TimeoutSeconds. A hung task waits up to the default maximum, which is a year. Without a timeout there is noStates.TimeoutforRetryto react to. - Forgetting
ResultPathinCatch. The error replaces the input, and later states lose the data they need. - Expecting
States.ALLto catch everything. It does not matchStates.DataLimitExceededorStates.Runtime.
Exercise
A Task state has a Catch with "ErrorEquals": ["States.ALL"] and no ResultPath, and the next state reads $.orderId from the original input. Why does the next state fail, and how do you fix it?
Show answer
A catcher without ResultPath defaults to "$", so the error object ({"Error": ..., "Cause": ...}) replaces the whole input and $.orderId no longer exists. Add "ResultPath": "$.error" to the catcher so the error is stored under $.error and the original input is kept.