typescriptaws-lambdaaws-cdkaws-step-functions

AWS CDK: Catch All Errors from Step Function


I am working on a Step Function in CDK that is mostly just a chain of Lambdas;

lambda_1
      .next(lambda_2)
      .next(lambda_3)
      .next(lambda_4)
      .next(lambda_5)

Whenever any of the Lambdas throws an error (handled or unhandled) I need to be able to log the error, which I intend to do in another Lambda as I need to perform some cleaning. Now, I can do this by triggering the Lambda from the Step-Function-Failed event, but for visibility I'd rather keep the logging as a step in the Step Function. I've reviewed the documentation, can't find a generic place that the addCatch method is available.

Is there any way to add a catch to a Step Function in CDK to catch errors thrown by all steps?


Solution

  • You can't set a "global" catch for all states directly, but you can achieve the same result by pointing all task states' catch handlers to the same fallback state.

    This is easy in the CDK. Your LambdaInvoke tasks have an addCatch method, which they inherit from TaskStateBase:

    myTasks.forEach((task: tasks.TaskStateBase) => {
        task.addCatch(myFallbackState);
    });