title: AWS Step Function - stop State machine execution on a condition
body: <p>I have attached how my state machine looks and also sharing the definition for reference.
  The <code>Service Status Retry Wait</code> state waits for 60 secs before calling the <code>Service Status Retry</code> state which invokes a lambda to check if the service was created in the <code>Service Created?</code> state.</p>
  <pre><code>{
    &quot;Comment&quot;: &quot;Step Function for Deployment&quot;,
    &quot;StartAt&quot;: &quot;Start&quot;,
    &quot;States&quot;: {
      &quot;Start&quot;: {
        &quot;Type&quot;: &quot;Pass&quot;,
        &quot;Next&quot;: &quot;Service Status&quot;
      },
      &quot;Service Status&quot;: {
        &quot;Type&quot;: &quot;Parallel&quot;,
        &quot;Branches&quot;: [
          {
            &quot;StartAt&quot;: &quot;Service Status Check&quot;,
            &quot;States&quot;: {
              &quot;Service Status Check&quot;: {
                &quot;Type&quot;: &quot;Task&quot;,
                &quot;Resource&quot;: &quot;arn:aws:states:::lambda:invoke&quot;,
                &quot;Parameters&quot;: {
                  &quot;FunctionName&quot;: &quot;arn:aws:lambda:us-west-2:&lt;acc-id&gt;:function:&lt;fn-name&gt;&quot;,
                  &quot;Payload&quot;: {
                    &quot;stepFunctionState.$&quot;: &quot;$$.State.Name&quot;,
                    &quot;requestContext&quot;: {
                      &quot;stepFunction&quot;: true,
                      &quot;accountId&quot;: &quot;&lt;acc-id&gt;&quot;
                    },
                    &quot;resource.$&quot;: &quot;States.Format('{}/srvc', $$.Execution.Input.resource)&quot;,
                    &quot;path.$&quot;: &quot;States.Format('{}/srvc', $$.Execution.Input.path)&quot;,
                    &quot;httpMethod&quot;: &quot;POST&quot;,
                    &quot;pathParameters.$&quot;: &quot;$$.Execution.Input.pathParameters&quot;,
                    &quot;body.$&quot;: &quot;$$.Execution.Input.body&quot;,
                    &quot;isBase64Encoded&quot;: false,
                    &quot;headers&quot;: {},
                    &quot;queryStringParameters&quot;: null
                  }
                },
                &quot;ResultPath&quot;: &quot;$.taskresult&quot;,
                &quot;Next&quot;: &quot;Service Created?&quot;
              },
              &quot;Service Created?&quot;: {
                &quot;Type&quot;: &quot;Choice&quot;,
                &quot;Choices&quot;: [
                  {
                    &quot;Variable&quot;: &quot;$.taskresult.Payload.body&quot;,
                    &quot;StringMatches&quot;: &quot;*\&quot;isCreated\&quot;: \&quot;false\&quot;*&quot;,
                    &quot;Next&quot;: &quot;Service Status Retry Wait&quot;
                  },
                  {
                    &quot;Variable&quot;: &quot;$.taskresult.Payload.body&quot;,
                    &quot;StringMatches&quot;: &quot;*\&quot;isCreated\&quot;: \&quot;true\&quot;*&quot;,
                    &quot;Next&quot;: &quot;Service Status Check End&quot;
                  }
                ],
                &quot;Default&quot;: &quot;Service Status Retry Wait&quot;
              },
              &quot;Service Status Retry Wait&quot;: {
                &quot;Type&quot;: &quot;Wait&quot;,
                &quot;Seconds&quot;: 60,
                &quot;Next&quot;: &quot;Service Status Retry&quot;
              },
              &quot;Service Status Retry&quot;: {
                &quot;Type&quot;: &quot;Task&quot;,
                &quot;Resource&quot;: &quot;arn:aws:states:::lambda:invoke&quot;,
                &quot;Parameters&quot;: {
                  &quot;FunctionName&quot;: &quot;arn:aws:lambda:us-west-2:&lt;acc-id&gt;:function:&lt;func-name&gt;&quot;,
                  &quot;Payload&quot;: {
                    &quot;stepFunctionState.$&quot;: &quot;$$.State.Name&quot;,
                    &quot;requestContext&quot;: {
                      &quot;stepFunction&quot;: true,
                      &quot;accountId&quot;: &quot;&lt;acc-id&gt;&quot;
                    },
                    &quot;resource.$&quot;: &quot;States.Format('{}/srvc', $$.Execution.Input.resource)&quot;,
                    &quot;path.$&quot;: &quot;States.Format('{}/srvc', $$.Execution.Input.path)&quot;,
                    &quot;httpMethod&quot;: &quot;POST&quot;,
                    &quot;pathParameters.$&quot;: &quot;$$.Execution.Input.pathParameters&quot;,
                    &quot;body.$&quot;: &quot;$$.Execution.Input.body&quot;,
                    &quot;isBase64Encoded&quot;: false,
                    &quot;headers&quot;: {},
                    &quot;queryStringParameters&quot;: null
                  }
                },
                &quot;ResultPath&quot;: &quot;$.taskresult&quot;,
                &quot;Next&quot;: &quot;Service Created?&quot;
              },
              &quot;Service Status Check End&quot;: {
                &quot;Type&quot;: &quot;Pass&quot;,
                &quot;End&quot;: true
              }
            }
          }
        ],
        &quot;Next&quot;: &quot;End&quot;
      },
      &quot;End&quot;: {
        &quot;Type&quot;: &quot;Pass&quot;,
        &quot;End&quot;: true
      }
    }
  }
  </code></pre>
  <p>Until and Unless the service is created, this state machine keeps going on in loops. I want to know if there is a way to break the loop and exit after 5 retries. It can be after either the <code>Service Status Retry Wait</code> or  <code>Service Status Retry</code> was called 5 times.</p>
  <p>Or if there is a way to know it has been looping for 1 hour now and exit based on that. Any of the above 2 options should work for me.</p>
  <p>Any thoughts/ideas on how this can be achieved?<a href="https://i.stack.imgur.com/D8NJ6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D8NJ6.png" alt="enter image description here" /></a></p>