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

AWS Step Functions - How to ignore "ResultSelector" when error is catched?


I have a AWS Step Functions state machine that as first state starts a Lambda function. This function do something then returns a JSON like { temp_a: "temporary a" }. This output should be sent to the second state of this state machine but, I don't want to send temp_a as key, rather I'd like to rename it a, so the result of the first state should be { a: "temporary a" }.

This is trivial and can be done using ResultSelector. For this, the Step Functions will look like this:

{
  "StartAt": "State1",
  "States": {
    "State1": {
      "Next": "State2",
      "Resource": "arn:aws:lambda:eu-west-1:XXX:function:sfexample-LambdaFunction",
      "ResultSelector": {
        "a.$": "$.temp_a"
      },
      "Type": "Task"
    },
    "State2": {
      "End": true,
      "Type": "Pass"
    }
  }
}

and Lambda will be something as easiest as possible since it contains just a single instruction return { temp_a: "temporary a" };.

Once the state machine has been started, everything works like a charm since the temp_a is successfully renamed into a (thanks to the ResultSelector) and then it is sent to the State2. Great!

Occasionally, that Lambda can throw a CustomError exception that I would catch in the state machine. When the error is caught the flow have to be diverted into the state CustomErrorState.

To make things possible I've added a Catch statement into the State1, and added another state called CustomErrorState of type Fail.

{
  "StartAt": "State1",
  "States": {
    "CustomErrorState": {
      "Cause": "Error happens",
      "Type": "Fail"
    },
    "State1": {
      "Catch": [
        {
          "ErrorEquals": [
            "CustomError"
          ],
          "Next": "CustomErrorState"
        }
      ],
      "Next": "State2",
      "Resource": "arn:aws:lambda:eu-west-1:XXX:function:sfexample-LambdaFunction",
      "ResultSelector": {
        "a.$": "$.temp_a"
      },
      "Type": "Task"
    },
    "State2": {
      "End": true,
      "Type": "Pass"
    }
  }
}

This seems reasonable but when Lambda throw the CustomError I get a runtime error because the State1 cannot perform what I've specified in the ResultSelector property.

What's the meaning? If an error is caught how could I handle the result? It is possible to ignore ResultSelector's instructions when I catch en error in a state?

Further detail: Here you can grab all necessary file to test it into your account.


Solution

  • The issue has been fixed. See post below.


    I have posted the same question on AWS discussion forum, signalling that the behaviour looks like a bug. They confirm the problem, in fact:

    The "ResultSelector" field should only be applied to the successful result of a Task, Map or Parallel State. In the case of a caught error, "ResultSelector" should NOT be applied.