2

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.

BAD_SEED
  • 4,840
  • 11
  • 53
  • 110

3 Answers3

2

I am an engineer from Step Functions and would like to update this post to let you know that the issue has been fixed.

ResultSelector field will not be applied to caught error output, and the previous behaviour was a bug.

Thanks for bringing this up.

yoodan
  • 65
  • 2
1

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.

BAD_SEED
  • 4,840
  • 11
  • 53
  • 110
0

Fortunately today I encountered the same problem. I believe what you have to do is handle the exception manually in your code there is no way to ignore ResultSelector. You have to return same dict/json from your code if it fails then only the SFN will be executed completely. Here is what I tried: enter image description here

Intentionally I am raising an Exception to check the behaviour of State Machine.

Below is the State Machine's execution when I ran that code:

enter image description here

CK__
  • 1,252
  • 1
  • 11
  • 25