Search code examples
typescriptamazon-web-servicesaws-cdkaws-step-functions

CDK Step Functions - How to create a loop


I'm trying to migrate a Step function that I created with the AWS interface and I encountered an issue with reproducing the following behavior:

Based on a condition, I want my task 2 to either execute task 3 and come back to the task 1 or to end the step function. My issue is the red path on the image enter image description here Here is the code I have for now:

sfn.Chain.start(OtherTaskWeDoNotCare)
  .next(task1)
  .next(
    new sfn.Choice(this, "task2").when(
      sfn.Condition.booleanEquals("$.isFinished", false),
      task3.next(task1) // This is not working
    )
  );

Hope someone can help me! Thanks in advance! 🙂


Solution

  • I finally found how to do! 😅 Here is the code:

    sfn.Chain.start(OtherTaskWeDoNotCareHere)
      .next(task1)
      .next(
        new sfn.Choice(this, "task2")
        .when(
          sfn.Condition.booleanEquals("$.isFinished", false),
          task3.next(task1)
        )
        .otherwise(new sfn.Succeed(this, "Done"))
      );