0

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?

ScottS
  • 71,703
  • 13
  • 126
  • 146
  • I'm curious, what information from the Step Functions context object you are looking to use in the fallback state? https://docs.aws.amazon.com/step-functions/latest/dg/input-output-contextobject.html#contextobject-format – Justin Callison Mar 30 '23 at 13:16
  • @JustinCallison Information from the State key of the context object. – ScottS Mar 30 '23 at 14:57

2 Answers2

0

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.

Justin Callison
  • 1,279
  • 2
  • 6
-1

If I'm understanding you're scenario correctly, leveraging the InputPath to the Task should preserve the input as long as ResultPath is used in the Catch.

ref: basic InputPath tricks https://gist.github.com/deric4/733eb129a10b13530cebaa3b70031c77

// ctx object
{
    "Execution": {
        "Id": "String",
        "Input": {},
        "Name": "String",
        "RoleArn": "String",
        "StartTime": "Format: ISO 8601"
    },
    "State": {
        "EnteredTime": "Format: ISO 8601",
        "Name": "String",
        "RetryCount": Number
    },
    "StateMachine": {
        "Id": "String",
        "Name": "String"
    },
    "Task": {
        "Token": "String"
    }
}

Using the InputPath in combination with the Parameters should preserve the input for Fallback

{
  "StartAt": "Step0",
  "States": {
    "Step0": {
      "Type": "Task",
      "InputPath": "$$.Execution.State",
      //   input should be
      //      {
      //        "EnteredTime": "Format: ISO 8601",
      //        "Name": "String",
      //        "RetryCount": Number,
      //      }
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "asdfsdasafas",
        "Payload": {
           // reference the absolute path to the ctx obj again
           "repeat-ctx-ojb.$": "$$.Execution.State",
           "foo": "bar",
        }
      },
      "Catch": [
        {
          "ErrorEquals": [ "States.ALL" ],
          "ResultPath": "$.error-info",
          "Next": "Z"

             //   output should be
             //      {
             //        "EnteredTime": "Format: ISO 8601",
             //        "Name": "String",
             //        "RetryCount": Number,
             //        "error-info": "error from task"
             //      }
        }
      ]
      "End": true
    }
  }
}
deric4
  • 1,095
  • 7
  • 11
  • There are two issues with this answer. (1) The `InputPath` here would overwrite the actual State input to be just the context, whereas I need both the original input and the state context. (2) The way `ResultPath` works if added to the input is that it only gets added to the original State input, not any modifications to that input by `InputPath` or `Parameters`. All this can be seen by using the data flow simulator. – ScottS Apr 18 '23 at 00:56