Search code examples
amazon-web-servicesaws-lambdaerror-handlingworkflowaws-step-functions

AWS: Pass Lambda and Step Function Context to Catch Result


Working in Node.js with AWS lambdas, I know:

  1. Lambda Context is passed to the handler as a second parameter
  2. Step Function Context can be obtained at various points in the data flow, the most useful being the Parameters step, where it can be added to the input of the lambda
  3. ResultSelector would work to add data (like Step Function Context) to the Result as well, except that transformation is ignored and not an allowed within a Catch statement.
  4. A Pass state can sit between states to also add data.
  5. That ResultPath can be used to append to the input the error within a Catch statement

My Desired Goal

Besides the error information, I want information from both the Lambda Context and the Step Function Context for the Lambda and Step State that is erroring (so specifically from the State key, as the Execution, StateMachine, and Task are all the same for any step) to be added to the final output result of a Catch to pass on to the Next step after.

The Challenge Faced in Achieving This Goal

However, it turns out that if Step Function Context is added using Parameters to the lambda input, that changed input it is still not accessible using ResultPath, as ResultPath only brings in the original input to the Step State, not the input as transformed by Parameters! You can test this with the data flow simulator (you need to be logged into an AWS account to utilize that). And of course a Pass state between the Catch and its Next call would no longer have access to the previous State Context that errored.

With my current codebase, I believe I have a workaround way of getting the Lambda Context fairly easily without a significant change as that is already being passed and used. However, accessing the Step Function Context through the lambda (via a Parameter passing to its input) would require hundreds of lines of code change in handlers and associated code to utilize it.

Ultimate Question

Is there some way I'm missing to directly add the Step Function Context from the State that is erroring to the Catch statement result, while also maintaining input and error info as is common in Catch statement use of ResultPath?


Solution

  • Unfortunately, I don't know of a way to make the Step Functions context object from the failing state available in the fallback state. The best workaround I can suggest, admittedly not great, would be to have a dedicated Pass state as Fallback (i.e., intermediary between your failing state and the state you want to handle the error condition) where you could augment context such as the name of the failing state.

    I'll pass this scenario on to the team as well to see how we can improve in the future.