Skip to content

Commit 1feff72

Browse files
authored
Merge pull request #400 from ginjups/ginjups-feature-errorhandling
Error Handling in Common Fallback state with JSONata Query Language
2 parents 6cae457 + 8b7d85e commit 1feff72

File tree

8 files changed

+360
-0
lines changed

8 files changed

+360
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Shared Fallback State - JSONata
2+
3+
This workflow shows how to fetch failed state names in JSONata StateMachine using a shared fallback state for multiple tasks. AWS Step Functions users want to implement centralized error reporting to avoid duplicating error handling logic. While setting up shared error reporting is simple, a challenge exists: the error reporting task can access error messages but cannot determine which state triggered the error. This creates issues in workflows with many Lambda functions by making it hard to identify failure points. Using JSONata, we show how to overcome this limitation and enable error tracking.
4+
5+
Learn more about this workflow at Step Functions workflows collection: [https://serverlessland.com/workflows/shared-fallback-state-jsonata](https://serverlessland.com/workflows/shared-fallback-state-jsonata)
6+
7+
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
8+
9+
## Requirements
10+
11+
* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
12+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
13+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
14+
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
15+
16+
## Deployment Instructions
17+
18+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
19+
```
20+
git clone https://github.com/aws-samples/step-functions-workflows-collection
21+
```
22+
1. Change directory to the pattern directory:
23+
```
24+
cd step-functions-workflows-collection/shared-fallback-state-jsonata
25+
```
26+
1. From the command line, use AWS SAM to deploy the AWS resources for the workflow as specified in the template.yaml file:
27+
```
28+
sam deploy --guided
29+
```
30+
1. During the prompts:
31+
* Enter a stack name
32+
* Enter the desired AWS Region
33+
* Allow SAM CLI to create IAM roles with the required permissions.
34+
35+
Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults.
36+
37+
1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
38+
39+
## How it works
40+
41+
The workflow doesn't require any input. By default, the third lambda function in "ThirdLambdaState" task state is intentinally programmed simulate the failure. Thus, the workflow execution fails at this task state and transitions into "FallbackState" with input as "FailedStateName": "ThirdLambdaState".
42+
43+
## Image
44+
45+
![image](./resources/statemachine.png)
46+
47+
## Testing
48+
49+
To test the workflow, simply start an execution of the state machine. This workflow doesn't require any input payload. Observe the execution once it has completed. In the execution, you would see that "ThirdLambdaState" has failed and the execution transitioned into "FallbackState" with input having "FailedStateName" as "ThirdLambdaState".
50+
51+
## Cleanup
52+
53+
1. Delete the stack
54+
```
55+
sam delete
56+
```
57+
58+
----
59+
Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
60+
61+
SPDX-License-Identifier: MIT-0
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"title": "Shared Fallback State - JSONata",
3+
"description": "Fetch failed state name in JSONata StateMachine with shared fallback state for multiple task states",
4+
"language": "Python",
5+
"simplicity": "2 - Pattern",
6+
"usecase": "Error Handling",
7+
"type": "Standard",
8+
"diagram":"/resources/statemachine.png",
9+
"videoId": "",
10+
"level": "200",
11+
"framework": "SAM",
12+
"services": ["stepfunctions","lambda"],
13+
"introBox": {
14+
"headline": "How it works",
15+
"text": [
16+
"This workflow shows how to fetch failed state names in JSONata StateMachine using a shared fallback state for multiple tasks. Step Functions users want to implement centralized error reporting to avoid duplicating error handling logic. While setting up shared error reporting is simple, a challenge exists: the error reporting task can access error messages but cannot determine which state triggered the error. This creates issues in workflows with many Lambda functions by making it hard to identify failure points. Using JSONata, we show how to overcome this limitation and enable error tracking."
17+
]
18+
},
19+
"testing": {
20+
"headline": "Testing",
21+
"text": [
22+
"See the GitHub repo for detailed testing instructions."
23+
]
24+
},
25+
"cleanup": {
26+
"headline": "Cleanup",
27+
"text": [
28+
"Delete the stack: <code>sam delete</code>."
29+
]
30+
},
31+
"deploy": {
32+
"text": [
33+
"sam deploy --guided"
34+
]
35+
},
36+
"gitHub": {
37+
"template": {
38+
"repoURL": "https://github.com/aws-samples/step-functions-workflows-collection/tree/main/shared-fallback-state-jsonata/",
39+
"templateDir":"shared-fallback-state-jsonata",
40+
"templateFile": "template.yaml",
41+
"ASL": "statemachine/statemachine.asl.json"
42+
},
43+
"payloads": [
44+
{
45+
"headline": "",
46+
"payloadURL": ""
47+
}
48+
]
49+
},
50+
"resources": {
51+
"headline": "Additional resources",
52+
"bullets": [
53+
{
54+
"text": "The AWS Step Functions Workshop",
55+
"link": "https://catalog.workshops.aws/stepfunctions/en-US"
56+
}
57+
]
58+
},
59+
"authors": [
60+
{
61+
"name": "Sahithi Ginjupalli",
62+
"image": "https://drive.google.com/file/d/1YcKYuGz3LfzSxiwb2lWJfpyi49SbvOSr/view?usp=sharing",
63+
"bio": "Cloud Engineer at AWS with a passion for diving deep into cloud and AI services to build innovative serverless applications.",
64+
"linkedin": "ginjupalli-sahithi-37460a18b",
65+
"twitter": ""
66+
}
67+
]
68+
}
69+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def lambda_handler(event, context):
2+
print("Executing First Lambda Function")
3+
return {
4+
'statusCode': 200,
5+
'body': 'First Lambda Function executed successfully'
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def lambda_handler(event, context):
2+
print("Executing Second Lambda Function")
3+
return {
4+
'statusCode': 200,
5+
'body': 'Second Lambda Function executed successfully'
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def lambda_handler(event, context):
2+
print("Executing Third Lambda Function")
3+
print(raising_error)
4+
return {
5+
'statusCode': 200,
6+
'body': 'Third Lambda Function executed successfully'
7+
}
205 KB
Loading
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{
2+
"Comment": "State machine using JSONata query language with common fallback state for multiple task states",
3+
"StartAt": "FirstLambdaState",
4+
"States": {
5+
"QueryLanguage": "JSONata",
6+
"FirstLambdaState": {
7+
"Type": "Task",
8+
"Resource": "arn:aws:states:::lambda:invoke",
9+
"Output": "{% $states.result.Payload %}",
10+
"Arguments": {
11+
"FunctionName": "${FirstLambdaArn}",
12+
"Payload": "{% $states.input %}"
13+
},
14+
"Retry": [
15+
{
16+
"ErrorEquals": [
17+
"Lambda.ServiceException",
18+
"Lambda.AWSLambdaException",
19+
"Lambda.SdkClientException",
20+
"Lambda.TooManyRequestsException"
21+
],
22+
"IntervalSeconds": 1,
23+
"MaxAttempts": 3,
24+
"BackoffRate": 2,
25+
"JitterStrategy": "FULL"
26+
}
27+
],
28+
"Next": "SecondLambdaState",
29+
"Catch": [
30+
{
31+
"ErrorEquals": [
32+
"States.ALL"
33+
],
34+
"Next": "FallbackState",
35+
"Output": {
36+
"FailedStateName": "{% $states.context.State.Name %}"
37+
}
38+
}
39+
]
40+
},
41+
"SecondLambdaState": {
42+
"Type": "Task",
43+
"Resource": "arn:aws:states:::lambda:invoke",
44+
"Output": "{% $states.result.Payload %}",
45+
"Arguments": {
46+
"FunctionName": "${SecondLambdaArn}",
47+
"Payload": "{% $states.input %}"
48+
},
49+
"Retry": [
50+
{
51+
"ErrorEquals": [
52+
"Lambda.ServiceException",
53+
"Lambda.AWSLambdaException",
54+
"Lambda.SdkClientException",
55+
"Lambda.TooManyRequestsException"
56+
],
57+
"IntervalSeconds": 1,
58+
"MaxAttempts": 3,
59+
"BackoffRate": 2,
60+
"JitterStrategy": "FULL"
61+
}
62+
],
63+
"Catch": [
64+
{
65+
"ErrorEquals": [
66+
"States.ALL"
67+
],
68+
"Next": "FallbackState",
69+
"Output": {
70+
"FailedStateName": "{% $states.context.State.Name %}"
71+
}
72+
}
73+
],
74+
"Next": "ThirdLambdaState"
75+
},
76+
"ThirdLambdaState": {
77+
"Type": "Task",
78+
"Resource": "arn:aws:states:::lambda:invoke",
79+
"Output": "{% $states.result.Payload %}",
80+
"Arguments": {
81+
"FunctionName": "${ThirdLambdaArn}",
82+
"Payload": "{% $states.input %}"
83+
},
84+
"Retry": [
85+
{
86+
"ErrorEquals": [
87+
"Lambda.ServiceException",
88+
"Lambda.AWSLambdaException",
89+
"Lambda.SdkClientException",
90+
"Lambda.TooManyRequestsException"
91+
],
92+
"IntervalSeconds": 1,
93+
"MaxAttempts": 3,
94+
"BackoffRate": 2,
95+
"JitterStrategy": "FULL"
96+
}
97+
],
98+
"End": true,
99+
"Catch": [
100+
{
101+
"ErrorEquals": [
102+
"States.ALL"
103+
],
104+
"Next": "FallbackState",
105+
"Output": {
106+
"FailedStateName": "{% $states.context.State.Name %}"
107+
}
108+
}
109+
]
110+
},
111+
"FallbackState": {
112+
"Type": "Pass",
113+
"End": true
114+
}
115+
}
116+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
4+
Resources:
5+
# Common Lambda Role
6+
LambdaExecutionRole:
7+
Type: AWS::IAM::Role
8+
Properties:
9+
AssumeRolePolicyDocument:
10+
Version: '2012-10-17'
11+
Statement:
12+
- Effect: Allow
13+
Principal:
14+
Service: lambda.amazonaws.com
15+
Action: sts:AssumeRole
16+
ManagedPolicyArns:
17+
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
18+
19+
# First Lambda Function
20+
FirstLambdaFunction:
21+
Type: AWS::Serverless::Function
22+
Properties:
23+
CodeUri: functions/first-function
24+
Handler: app.lambda_handler
25+
Runtime: python3.13
26+
Architectures:
27+
- x86_64
28+
MemorySize: 128
29+
Timeout: 30
30+
Role: !GetAtt LambdaExecutionRole.Arn
31+
32+
# Second Lambda Function
33+
SecondLambdaFunction:
34+
Type: AWS::Serverless::Function
35+
Properties:
36+
CodeUri: functions/second-function/
37+
Handler: app.lambda_handler
38+
Runtime: python3.13
39+
Architectures:
40+
- x86_64
41+
MemorySize: 128
42+
Timeout: 30
43+
Role: !GetAtt LambdaExecutionRole.Arn
44+
45+
# Third Lambda Function
46+
ThirdLambdaFunction:
47+
Type: AWS::Serverless::Function
48+
Properties:
49+
CodeUri: functions/third-function/
50+
Handler: app.lambda_handler
51+
Runtime: python3.13
52+
Architectures:
53+
- x86_64
54+
MemorySize: 128
55+
Timeout: 30
56+
Role: !GetAtt LambdaExecutionRole.Arn
57+
58+
# State Machine Role
59+
StateMachineRole:
60+
Type: AWS::IAM::Role
61+
Properties:
62+
AssumeRolePolicyDocument:
63+
Version: '2012-10-17'
64+
Statement:
65+
- Effect: Allow
66+
Principal:
67+
Service: states.amazonaws.com
68+
Action: sts:AssumeRole
69+
Policies:
70+
- PolicyName: LambdaInvokePolicy
71+
PolicyDocument:
72+
Version: '2012-10-17'
73+
Statement:
74+
- Effect: Allow
75+
Action: lambda:InvokeFunction
76+
Resource:
77+
- !GetAtt FirstLambdaFunction.Arn
78+
- !GetAtt SecondLambdaFunction.Arn
79+
- !GetAtt ThirdLambdaFunction.Arn
80+
81+
# State Machine
82+
StateMachine:
83+
Type: AWS::Serverless::StateMachine
84+
Properties:
85+
DefinitionSubstitutions:
86+
FirstLambdaArn: !GetAtt FirstLambdaFunction.Arn
87+
SecondLambdaArn: !GetAtt SecondLambdaFunction.Arn
88+
ThirdLambdaArn: !GetAtt ThirdLambdaFunction.Arn
89+
DefinitionUri: statemachine/statemachine.asl.json
90+
Role: !GetAtt StateMachineRole.Arn
91+
92+
Outputs:
93+
StateMachineArn:
94+
Description: "State Machine ARN"
95+
Value: !Ref StateMachine

0 commit comments

Comments
 (0)