To handle errors within a Step Function, you can use a Catch block that hands off execution to an Error State. This State can be a Task type, which can (among other things) trigger a Lambda that does the actual error handling.
Example#
## Step function failure handler - in the future this will be more complex than writing to DB
failure_handler = lambda_python.PythonFunction(
self,
f"failure-handler",
entry="lambda/",
index="failure_handler.py",
runtime=_lambda.Runtime.PYTHON_3_9,
)
failure_task = sfn_tasks.LambdaInvoke(
self,
f"failure-lambda-task",
lambda_function=training_pipeline_failure_handler,
output_path="$",
)
## State machine definition
task_one.add_catch(failure_task, result_path="$.error")
task_two.add_catch(failure_task, result_path="$.error")
state_machine_def = task_one.next(task_two)
state_machine = sfn.StateMachine(
self, f"failing-state-machine", definition=state_machine_def
)