如何在AWS Step Functions状态机中将一个动作的输出作为另一个动作的输入?以及如何实现多Lambda函数间数据传递与错误信息汇总至Error Handling Lambda?
Hey there! Let's break down your two AWS Step Functions questions one by one—these are super common scenarios, so I’ve got you covered.
The core here is using JSON Path syntax, which Step Functions uses to reference data from a state's input or output. Here's how to do it in a few common cases:
Basic Pass-Through
If you want to send the entire output of one action directly as input to the next, use InputPath to target the output. For Lambda tasks, remember that Lambda's output is nested under the Payload key in the state's output.
Example snippet for two chained Lambdas:
{ "States": { "FirstLambda": { "Type": "Task", "Resource": "arn:aws:lambda:us-east-1:123456789012:function:FirstFunction", "Next": "SecondLambda" }, "SecondLambda": { "Type": "Task", "Resource": "arn:aws:lambda:us-east-1:123456789012:function:SecondFunction", "InputPath": "$.Payload", // Takes FirstLambda's full output as input "End": true } } }
Pass Specific Fields
If you only need a subset of the previous action's output, use the Parameters field to shape the input for the next action. This lets you map specific fields to the structure the next action expects:
"SecondLambda": { "Type": "Task", "Resource": "arn:aws:lambda:us-east-1:123456789012:function:SecondFunction", "Parameters": { "customerId": "$.Payload.customer.id", "orderDetails": "$.Payload.order" }, "End": true }
This works for any Step Functions state type (not just Lambda)—Pass, Map, etc.—since JSON Path is universal across the service.
For this scenario, we need to chain the Lambdas (passing outputs between them), catch errors from each one without stopping the workflow, then send all collected errors to your error-handling Lambda. Here's a step-by-step implementation:
Step 1: Chain the Lambdas with Input Pass-Through
Use the same JSON Path logic from the first question to pass each Lambda's output to the next. For example, Lambda 2 takes Lambda 1's output as input, Lambda 3 takes Lambda 2's output as input.
Step 2: Add Error Catching to Each Lambda
For each Lambda task, add a Catch block that captures errors, stores them in the workflow's context, and continues to the next Lambda. Use ResultPath to inject error data without overwriting existing workflow data.
Step 3: Aggregate Errors and Trigger the Error Handler
After all three Lambdas run, use a Choice state to check if any errors were collected. If yes, send all errors to your Error Handling Lambda; if no, end the workflow successfully.
Full Example State Machine
{ "Comment": "Chain 3 Lambdas + aggregate errors to error handler", "StartAt": "Lambda1", "States": { "Lambda1": { "Type": "Task", "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Lambda1", "Next": "Lambda2", "Catch": [ { "ErrorEquals": ["States.ALL"], "ResultPath": "$.errors.lambda1", "Next": "Lambda2" } ], "ResultPath": "$.lambda1Output" // Optional: save successful output if needed }, "Lambda2": { "Type": "Task", "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Lambda2", "InputPath": "$.lambda1Output.Payload", // Pass Lambda1's output as input "Next": "Lambda3", "Catch": [ { "ErrorEquals": ["States.ALL"], "ResultPath": "$.errors.lambda2", "Next": "Lambda3" } ], "ResultPath": "$.lambda2Output" }, "Lambda3": { "Type": "Task", "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Lambda3", "InputPath": "$.lambda2Output.Payload", // Pass Lambda2's output as input "Next": "CheckForErrors", "Catch": [ { "ErrorEquals": ["States.ALL"], "ResultPath": "$.errors.lambda3", "Next": "CheckForErrors" } ], "ResultPath": "$.lambda3Output" }, "CheckForErrors": { "Type": "Choice", "Choices": [ { "Variable": "$.errors", "IsPresent": true, "Next": "ErrorHandlingLambda" } ], "Default": "SuccessState" }, "ErrorHandlingLambda": { "Type": "Task", "Resource": "arn:aws:lambda:us-east-1:123456789012:function:ErrorHandler", "InputPath": "$.errors", // Send all collected errors as input "End": true }, "SuccessState": { "Type": "Succeed" } } }
Key Notes:
ResultPathin Catch Blocks: This ensures error data is added to the workflow's input (under$.errors) without replacing existing data—so we can keep track of errors from multiple Lambdas.States.ALL: Catches every type of error (Lambda exceptions, timeouts, etc.). You can narrow this down to specific error types (e.g.,Lambda.ServiceException) if needed.- Continue on Error: Each Catch block directs the workflow to the next Lambda, so even if one fails, the rest still run before we handle errors.
- Optional Success Output: The
ResultPathin successful Lambda tasks saves their output if you need to use it elsewhere (e.g., in a post-success cleanup step).
内容的提问来源于stack exchange,提问作者KanikaM

