-
Notifications
You must be signed in to change notification settings - Fork 131
Error Handling in Common Fallback state with JSONata Query Language #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,66 @@ | ||||||
# Common Fallback State - JSONata | ||||||
|
||||||
This example workflow illustrate practical solutions for fetching failed state name in JSONata StateMachine with common fallback state for multiple task states. AWS Step Functions users commonly seek to implement centralized error reporting within their workflows to avoid duplicating error handling logic across multiple states. While implementing a shared error reporting task is straightforward, a significant challenge arises: the error reporting task can access the error message from the failed state, but cannot inherently determine which specific state triggered the error. This limitation becomes particularly problematic in workflows containing numerous Lambda functions, as it complicates identifying the exact point of failure. Using JSONata as our query language, we demonstrate approach to overcome this limitation, enabling comprehensive error tracking while maintaining workflow efficiency. | ||||||
|
||||||
Learn more about this workflow at Step Functions workflows collection: [https://serverlessland.com/workflows/common-fallback-state-jsonata](https://serverlessland.com/workflows/common-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/common-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 programmed to fail. Thus, the workflow execution fails at this task state and transitions into "FallbackState" with input as "FailedStateName": "ThirdLambdaState". | ||||||
The "ThirdLambdaFunction" is intentionally programmed to simulate the failure. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add to paragraph above and then remove |
||||||
## Image | ||||||
 | ||||||
## 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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please include a description of what the user should expect |
||||||
## Cleanup | ||||||
1. Delete the stack | ||||||
```bash | ||||||
aws cloudformation delete-stack --stack-name STACK_NAME | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You deployed using SAM, why not use sam delete? |
||||||
``` | ||||||
1. Confirm the stack has been deleted | ||||||
```bash | ||||||
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus" | ||||||
``` | ||||||
---- | ||||||
Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
SPDX-License-Identifier: MIT-0 |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,69 @@ | ||||||
{ | ||||||
"title": "Common Fallback State JSONata", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use same title as in the README |
||||||
"description": "Fetch failed state name in JSONata StateMachine with common 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 example workflow illustrate practical solutions for fetching failed state name in JSONata StateMachine with common fallback state for multiple task states. AWS Step Functions users commonly seek to implement centralized error reporting within their workflows to avoid duplicating error handling logic across multiple states. While implementing a shared error reporting task is straightforward, a significant challenge arises: the error reporting task can access the error message from the failed state, but cannot inherently determine which specific state triggered the error. This limitation becomes particularly problematic in workflows containing numerous Lambda functions, as it complicates identifying the exact point of failure. Using JSONata as our query language, we demonstrate approach to overcome this limitation, enabling comprehensive error tracking while maintaining workflow efficiency." | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit too verbose, condense and leave out filler words like "practical", "particular", "comprehensive" etc. |
||||||
] | ||||||
}, | ||||||
"testing": { | ||||||
"headline": "Testing", | ||||||
"text": [ | ||||||
"See the GitHub repo for detailed testing instructions." | ||||||
] | ||||||
}, | ||||||
"cleanup": { | ||||||
"headline": "Cleanup", | ||||||
"text": [ | ||||||
"1. Delete the stack: <code>sam delete</code>." | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
] | ||||||
}, | ||||||
"deploy": { | ||||||
"text": [ | ||||||
"sam deploy --guided" | ||||||
] | ||||||
}, | ||||||
"gitHub": { | ||||||
"template": { | ||||||
"repoURL": "https://github.com/aws-samples/step-functions-workflows-collection/tree/main/common-fallback-state-jsonata/", | ||||||
"templateDir":"common-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": "" | ||||||
} | ||||||
] | ||||||
} | ||||||
|
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' | ||
} |
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' | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,116 @@ | ||||||
{ | ||||||
"Comment": "StateMachine using JSONata QueryLanguage with common fallback state for multiple task states", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
"StartAt": "FirstLambdaState", | ||||||
"States": { | ||||||
"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 | ||||||
} | ||||||
}, | ||||||
"QueryLanguage": "JSONata" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move to top, just to be more visible |
||||||
} |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would maybe "Shared Fallback State" be a better description?