Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions shared-fallback-state-jsonata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Shared Fallback State - JSONata

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.

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)

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.

## Requirements

* [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.
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed

## Deployment Instructions

1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
```
git clone https://github.com/aws-samples/step-functions-workflows-collection
```
1. Change directory to the pattern directory:
```
cd step-functions-workflows-collection/shared-fallback-state-jsonata
```
1. From the command line, use AWS SAM to deploy the AWS resources for the workflow as specified in the template.yaml file:
```
sam deploy --guided
```
1. During the prompts:
* Enter a stack name
* Enter the desired AWS Region
* Allow SAM CLI to create IAM roles with the required permissions.

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.

1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.

## How it works

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".

## Image

![image](./resources/statemachine.png)

## Testing

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".

## Cleanup

1. Delete the stack
```
sam delete
```

----
Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0
69 changes: 69 additions & 0 deletions shared-fallback-state-jsonata/example-workflow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"title": "Shared Fallback State - JSONata",
"description": "Fetch failed state name in JSONata StateMachine with shared fallback state for multiple task states",
"language": "Python",
"simplicity": "2 - Pattern",
"usecase": "Error Handling",
"type": "Standard",
"diagram":"/resources/statemachine.png",
"videoId": "",
"level": "200",
"framework": "SAM",
"services": ["stepfunctions","lambda"],
"introBox": {
"headline": "How it works",
"text": [
"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."
]
},
"testing": {
"headline": "Testing",
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"headline": "Cleanup",
"text": [
"Delete the stack: <code>sam delete</code>."
]
},
"deploy": {
"text": [
"sam deploy --guided"
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/step-functions-workflows-collection/tree/main/shared-fallback-state-jsonata/",
"templateDir":"shared-fallback-state-jsonata",
"templateFile": "template.yaml",
"ASL": "statemachine/statemachine.asl.json"
},
"payloads": [
{
"headline": "",
"payloadURL": ""
}
]
},
"resources": {
"headline": "Additional resources",
"bullets": [
{
"text": "The AWS Step Functions Workshop",
"link": "https://catalog.workshops.aws/stepfunctions/en-US"
}
]
},
"authors": [
{
"name": "Sahithi Ginjupalli",
"image": "https://drive.google.com/file/d/1YcKYuGz3LfzSxiwb2lWJfpyi49SbvOSr/view?usp=sharing",
"bio": "Cloud Engineer at AWS with a passion for diving deep into cloud and AI services to build innovative serverless applications.",
"linkedin": "ginjupalli-sahithi-37460a18b",
"twitter": ""
}
]
}

6 changes: 6 additions & 0 deletions shared-fallback-state-jsonata/functions/first-function/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def lambda_handler(event, context):
print("Executing First Lambda Function")
return {
'statusCode': 200,
'body': 'First Lambda Function executed successfully'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def lambda_handler(event, context):
print("Executing Second Lambda Function")
return {
'statusCode': 200,
'body': 'Second Lambda Function executed successfully'
}
7 changes: 7 additions & 0 deletions shared-fallback-state-jsonata/functions/third-function/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def lambda_handler(event, context):
print("Executing Third Lambda Function")
print(raising_error)
return {
'statusCode': 200,
'body': 'Third Lambda Function executed successfully'
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
116 changes: 116 additions & 0 deletions shared-fallback-state-jsonata/statemachine/statemachine.asl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"Comment": "State machine using JSONata query language with common fallback state for multiple task states",
"StartAt": "FirstLambdaState",
"States": {
"QueryLanguage": "JSONata",
"FirstLambdaState": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Output": "{% $states.result.Payload %}",
"Arguments": {
"FunctionName": "${FirstLambdaArn}",
"Payload": "{% $states.input %}"
},
"Retry": [
{
"ErrorEquals": [
"Lambda.ServiceException",
"Lambda.AWSLambdaException",
"Lambda.SdkClientException",
"Lambda.TooManyRequestsException"
],
"IntervalSeconds": 1,
"MaxAttempts": 3,
"BackoffRate": 2,
"JitterStrategy": "FULL"
}
],
"Next": "SecondLambdaState",
"Catch": [
{
"ErrorEquals": [
"States.ALL"
],
"Next": "FallbackState",
"Output": {
"FailedStateName": "{% $states.context.State.Name %}"
}
}
]
},
"SecondLambdaState": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Output": "{% $states.result.Payload %}",
"Arguments": {
"FunctionName": "${SecondLambdaArn}",
"Payload": "{% $states.input %}"
},
"Retry": [
{
"ErrorEquals": [
"Lambda.ServiceException",
"Lambda.AWSLambdaException",
"Lambda.SdkClientException",
"Lambda.TooManyRequestsException"
],
"IntervalSeconds": 1,
"MaxAttempts": 3,
"BackoffRate": 2,
"JitterStrategy": "FULL"
}
],
"Catch": [
{
"ErrorEquals": [
"States.ALL"
],
"Next": "FallbackState",
"Output": {
"FailedStateName": "{% $states.context.State.Name %}"
}
}
],
"Next": "ThirdLambdaState"
},
"ThirdLambdaState": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Output": "{% $states.result.Payload %}",
"Arguments": {
"FunctionName": "${ThirdLambdaArn}",
"Payload": "{% $states.input %}"
},
"Retry": [
{
"ErrorEquals": [
"Lambda.ServiceException",
"Lambda.AWSLambdaException",
"Lambda.SdkClientException",
"Lambda.TooManyRequestsException"
],
"IntervalSeconds": 1,
"MaxAttempts": 3,
"BackoffRate": 2,
"JitterStrategy": "FULL"
}
],
"End": true,
"Catch": [
{
"ErrorEquals": [
"States.ALL"
],
"Next": "FallbackState",
"Output": {
"FailedStateName": "{% $states.context.State.Name %}"
}
}
]
},
"FallbackState": {
"Type": "Pass",
"End": true
}
}
}
95 changes: 95 additions & 0 deletions shared-fallback-state-jsonata/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
# Common Lambda Role
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

# First Lambda Function
FirstLambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/first-function
Handler: app.lambda_handler
Runtime: python3.13
Architectures:
- x86_64
MemorySize: 128
Timeout: 30
Role: !GetAtt LambdaExecutionRole.Arn

# Second Lambda Function
SecondLambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/second-function/
Handler: app.lambda_handler
Runtime: python3.13
Architectures:
- x86_64
MemorySize: 128
Timeout: 30
Role: !GetAtt LambdaExecutionRole.Arn

# Third Lambda Function
ThirdLambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/third-function/
Handler: app.lambda_handler
Runtime: python3.13
Architectures:
- x86_64
MemorySize: 128
Timeout: 30
Role: !GetAtt LambdaExecutionRole.Arn

# State Machine Role
StateMachineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: states.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: LambdaInvokePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: lambda:InvokeFunction
Resource:
- !GetAtt FirstLambdaFunction.Arn
- !GetAtt SecondLambdaFunction.Arn
- !GetAtt ThirdLambdaFunction.Arn

# State Machine
StateMachine:
Type: AWS::Serverless::StateMachine
Properties:
DefinitionSubstitutions:
FirstLambdaArn: !GetAtt FirstLambdaFunction.Arn
SecondLambdaArn: !GetAtt SecondLambdaFunction.Arn
ThirdLambdaArn: !GetAtt ThirdLambdaFunction.Arn
DefinitionUri: statemachine/statemachine.asl.json
Role: !GetAtt StateMachineRole.Arn

Outputs:
StateMachineArn:
Description: "State Machine ARN"
Value: !Ref StateMachine