Skip to content

Step Functions: Returning a meaningful error indication from a nested state machine (sync) invocation

0

When using a Task state with Resource: arn:aws:states:::states:startExecution.sync:2 to invoke a state machine (SM), some failures inside the nested SM justify a Retry, but some don't, such as failure due to invalid arguments or missing permissions, which will not fix themselves if retried.

It is possible to set a retry policy using ErrorEquals: ["States.TaskFailed"], but that will apply to failure due to any cause.

It is possible, of course, to write something like the following, but as far as I could test, there is no way for the nested SM to set the Error value seen by the Retry clause here:

 # YAML, in the invoking SM
  Invoke nested SM:
    Type: Task
    Resource: arn:aws:states:::states:startExecution.sync:2
    Arguments: ...
    Retry:
      - ErrorEquals: ["NotExpectedToBeFixedAnyTimeSoon.Error"]
        MaxAttempts: 0  # no use retrying this
      - ErrorEquals: ["States.TaskFailed"]
        MaxAttempts: 2

I tried ending the nested SM with a Fail state and setting the Error clause on it (passed as input from the preceding state that detected the failure):

 # YAML, in the nested SM
  Failure:
      Type: Fail
      Cause: '{% $states.input.Cause %}'
      Error: '{% $states.input.Error %}'

This does work in the sense that the returned Error value is visible to the invoking SM inside the cause key of the task output, but the error value is still always States.TaskFailed.

It would help if there was a way to define a retry policy that depends on cause in addition to error, but AFAIK there isn't one.

Is there a way to accomplish this without an elaborate manual implementation of a retry loop using a Choice state and variables for counting and timing? Some of my SMs have multiple invocations of nested "routine" SMs. If each invocation expands to multiple states and variables, those SMs can become unwieldy pretty quickly...

asked 2 years ago409 views

1 Answer
0

You're correct that there isn't a direct way for a nested state machine to set the Error value seen by the Retry clause in the parent state machine when using the startExecution.sync:2 resource. The error returned is always States.TaskFailed for any failure in the nested state machine.

However, there are a few approaches you can consider to handle this situation:

  1. Use a Lambda function as a wrapper: Instead of directly invoking the nested state machine, you can use a Lambda function as an intermediary. This Lambda function can start the execution of the nested state machine, wait for its completion, and then interpret the results. Based on the output or error from the nested state machine, the Lambda function can throw different types of errors that your parent state machine can then handle with more granular retry logic.

  2. Use the output of the nested state machine: While you can't directly change the error type, you can use the output of the nested state machine to determine whether to retry. In your parent state machine, after the Task state that invokes the nested state machine, you can add a Choice state that examines the output and decides whether to retry or move to a different state based on the content of the cause or other output fields.

  3. Implement a custom retry mechanism: As you mentioned, you could implement a custom retry mechanism using Choice states and variables for counting and timing. While this might make the state machine more complex, it gives you full control over the retry logic.

  4. Use Step Functions' new restart feature: AWS Step Functions recently introduced a feature that allows restarting workflows from their point of failure. This could be used as an alternative to complex retry logic in some scenarios, although it may not be suitable for all use cases.

While these solutions aren't as straightforward as directly setting the error type from the nested state machine, they provide ways to achieve more granular control over error handling and retries when working with nested state machines. The best approach will depend on your specific use case and the complexity you're willing to accept in your state machine definitions.
Sources
Handling errors in Step Functions workflows - AWS Step Functions
Handling error conditions using a Step Functions state machine - AWS Step Functions
AWS Step Functions now supports restarting workflows from failure

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.