5

I have a simple AWS state machine with two task states that execute C# lambda functions, and one pass state error handler to handle "States.ALL":

{
  "Comment": "StateMachine1",
  "StartAt": "step1",
  "States": {
    "step1": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-2:0000:function:step1",
      "Catch": [ {
            "ErrorEquals": ["States.ALL"],
            "Next": "CatchAllFallback"
         } ],
      "Next": "step2"
    },
      "step2": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-2:0000:function:step2",
        "Catch": [ {
            "ErrorEquals": ["States.ALL"],
            "Next": "CatchAllFallback"
         } ],
      "End": true
    },
     "CatchAllFallback": {
         "Type": "Pass",
         "Result": "This is a fallback from any error code",
         "End": true
      }
  }
}

When one of the steps fails, I get the following as the input to my "CatchAllFallback":

"Error": "Lambda.Unknown",
"Cause": "The cause could not be determined because Lambda did not return an error type."

When I review the Cloudwatch logs, I see that the step timed out. My question is: how do I get that information in my error handler instead of just "Lambda.Unknown"? I know it must be possible, but after spending hours searching the web, I can NOT figure out how to do it. Any advice would be appreciated.

2 Answers 2

3

I don't think so that this is supported by default (But maybe someone can proof me wrong?). It's because of the root of the issue. AWS Step Functions expect a valid error type to pass this to the next state but your Lambda timed out and therefore it's impossible to pass a valid error type.

But! You could build a workaround inside your Lambda(s).

Just check the RemainingTime inside the Context Object and as soon as it is close to get timed out you throw an exception which then can be mapped to a valid error type.

Information about the remaining time of function execution can be used to specify function behavior when nearing the timeout

Sign up to request clarification or add additional context in comments.

4 Comments

I wish I could prove you wrong, but I can't yet! I still haven't found a way to catch these AWS exceptions inside Lambda code. But, thanks for the hints about the context object.
I just had this same issue and, also was due to a Lambda timing out
@gvasquez did you tackle it with the race condition approach mentioned above?
@MaiKaYI had to tweak my Lambda config as it had a too low timeout for what it actually had to do
0

Your lambda needs to throw the relevant error in order for the step function to give a better output, the documentation has an explanation for this scenario using c#

Example

namespace Example {            
   public class AccountAlreadyExistsException : Exception {
      public AccountAlreadyExistsException(String message) :
         base(message) {
      }
   }
} 

namespace Example {
   public class Handler {
     public static void CreateAccount() {
       throw new AccountAlreadyExistsException("Account is in use!");
     }
   }
}

1 Comment

That's true for exceptions that occur within the C# code inside the Lambda. I'm talking about AWS-level infrastructure exceptions, for example lambda timeouts. What you outlined will not catch or handle those exceptions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.