<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged stepfunctions</title>
<link>https://coderlegion.com/tag/stepfunctions</link>
<description>Powered by Question2Answer</description>
<item>
<title>AWS Step Functions: Creating a ‘Busy Waiting’ flow to wait for successful lambda executions.</title>
<link>https://coderlegion.com/31106/aws-step-functions-creating-a-busy-waiting-flow-to-wait-for-successful-lambda-executions</link>
<description>&lt;p&gt;Hi everyone, welcome to this article. In the past few days, I had to create a logic in AWS Step functions which involved waiting for a lambda that queries athena before going forward in the flow. &lt;/p&gt;
&lt;p&gt;When I was discussing the logic, it felt like an easy thing to accomplish in minutes but I was surprised upon knowing that Step Functions does not have its native technique/solve to wait for a state to succeed before starting another. This made me search for hours before coming up with a solution.&lt;/p&gt;
&lt;p&gt;Here, I will share with you a technique which will help you in the future.&lt;/p&gt;
&lt;p&gt;First of all, I will create a simple lambda function. All we need to know from the lambda is whether it did its job. So, this simple function will make sure to return its status.&lt;/p&gt;
&lt;p&gt;Lambda function:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import json

def lambda_handler(event, context):
    response = {
        'status': 'success',
        'message': 'Hello, World!'
    }
    
    return response
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can copy both the function and the Step function definition which I will share below to replicate all of this in your own environment. &lt;/p&gt;
&lt;p&gt;Now, lets go ahead and create a state machine in AWS Step Functions.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vqducgkmq06ude209gj.png&quot; alt=&quot;Image description&quot;&gt;&lt;/p&gt;
&lt;p&gt;Click on ‘Create state machine’. &lt;/p&gt;
&lt;p&gt;Choose the option to write your own definition and choose ‘standard’.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3mcoo8lc3omc8jbhnh7h.png&quot; alt=&quot;Image description&quot;&gt;&lt;/p&gt;
&lt;p&gt;Now, in the definition section, copy and paste the following definition.&lt;/p&gt;
&lt;p&gt;Step Functions definition:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{
  &quot;Comment&quot;: &quot;A Hello World example of the Amazon States Language using Pass states&quot;,
  &quot;StartAt&quot;: &quot;Main&quot;,
  &quot;States&quot;: {
    &quot;Main&quot;: {
      &quot;Type&quot;: &quot;Task&quot;,
      &quot;Resource&quot;: &quot;arn:aws:states:::lambda:invoke&quot;,
      &quot;OutputPath&quot;: &quot;$.Payload&quot;,
      &quot;Parameters&quot;: {
        &quot;Payload.$&quot;: &quot;$&quot;,
        &quot;FunctionName&quot;: &quot;arn:aws:lambda:us-east-1:031342435657:function:stepfunctionlambda_test:$LATEST&quot;
      },
      &quot;Retry&quot;: [
        {
          &quot;ErrorEquals&quot;: [
            &quot;Lambda.ServiceException&quot;,
            &quot;Lambda.AWSLambdaException&quot;,
            &quot;Lambda.SdkClientException&quot;,
            &quot;Lambda.TooManyRequestsException&quot;
          ],
          &quot;IntervalSeconds&quot;: 2,
          &quot;MaxAttempts&quot;: 6,
          &quot;BackoffRate&quot;: 2
        }
      ],
      &quot;Next&quot;: &quot;WaitforInvocation&quot;
    },
    &quot;WaitforInvocation&quot;: {
      &quot;Type&quot;: &quot;Wait&quot;,
      &quot;Seconds&quot;: 2,
      &quot;Next&quot;: &quot;CheckInvocationsStatus&quot;
    },
    &quot;CheckInvocationsStatus&quot;: {
      &quot;Type&quot;: &quot;Choice&quot;,
      &quot;Choices&quot;: [
        {
          &quot;Variable&quot;: &quot;$.status&quot;,
          &quot;StringEquals&quot;: &quot;success&quot;,
          &quot;Next&quot;: &quot;Final&quot;
        }
      ],
      &quot;Default&quot;: &quot;WaitforInvocation&quot;
    },
    &quot;Final&quot;: {
      &quot;Type&quot;: &quot;Succeed&quot;
    }
  }
}


&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Save the definition and click next.&lt;/p&gt;
&lt;p&gt;Specify the name of your state machine and then leave everything else as default. A new execution role will be created if you don’t specify any.&lt;/p&gt;
&lt;p&gt;Save it and then you will have created a state machine which looks like this:&lt;/p&gt;
&lt;p&gt;Visual flow:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h567w7r69dda3w38t36g.png&quot; alt=&quot;Image description&quot;&gt;&lt;/p&gt;
&lt;p&gt;To explain the definition above, I have pointed out how this state machine works:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The state machine starts at the &quot;Main&quot; state.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &quot;Main&quot; state is a Task state that invokes a Lambda function specified by the &quot;FunctionName&quot; parameter. The input to the Lambda function is the entire input passed to the state machine.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &quot;OutputPath&quot; parameter is  set to &quot;$.Payload&quot; to capture the output of the Lambda function.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the Lambda function invocation fails with certain exceptions specified in the &quot;Retry&quot; section (Lambda.ServiceException, Lambda.AWSLambdaException, Lambda.SdkClientException,  Lambda.TooManyRequestsException), the state machine will retry the invocation up to 6 times with a backoff interval of 2 seconds.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After invoking the Lambda function, the state machine proceeds to the &quot;WaitforInvocation&quot; state.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &quot;WaitforInvocation&quot; state is a Wait state that pauses the execution for 2 seconds.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After the wait period,  the state machine goes  to the &quot;CheckInvocationsStatus&quot; state.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &quot;CheckInvocationsStatus&quot; state is a Choice state that checks  the value of the &quot;$.status&quot; field in the output of the Lambda function. If the status is &quot;success&quot;, the state machine proceeds to  the &quot;Final&quot; state. Otherwise, it goes back to the &quot;WaitforInvocation&quot; state.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &quot;Final&quot; state is a Succeed state that indicates the successful completion of the state machine.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code&gt;Now, lets go ahead and execute it!
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amyu3l4skfdr3os7owe5.png&quot; alt=&quot;Image description&quot;&gt;&lt;/p&gt;
&lt;p&gt;Click on ‘Start execution’ and  leave the input field empty.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7hib8yqc4i674d0132r8.png&quot; alt=&quot;Image description&quot;&gt;&lt;/p&gt;
&lt;p&gt;Incase of a lambda that requires input, you can insert your input here as a Json&lt;/p&gt;
&lt;p&gt;After you start your execution, each state will turn into different colours which indicates the following.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Green: Indicates successful completion of a state or step.&lt;/li&gt;
&lt;li&gt;Red: Indicates a failure or error occurred during the execution of a state or step.&lt;/li&gt;
&lt;li&gt;Yellow: Indicates a state is in a waiting or paused state.&lt;/li&gt;
&lt;li&gt;Blue: Represents a decision or choice point where the state machine evaluates conditions to determine the next state.&lt;/li&gt;
&lt;li&gt;Gray: Represents a passive state that simply passes input to output without performing significant work.&lt;/li&gt;
&lt;li&gt;White: Represents transitions or connections between states, indicating the flow of execution from one state to another.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Finally this is how the execution will look like:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fp8g4hthee521cc883w4.png&quot; alt=&quot;Image description&quot;&gt;&lt;/p&gt;
&lt;p&gt;Thank you. Hope you can now go ahead and apply the  “Busy Waiting” logic in your step functions.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/31106/aws-step-functions-creating-a-busy-waiting-flow-to-wait-for-successful-lambda-executions</guid>
<pubDate>Wed, 14 Oct 2026 18:01:55 +0000</pubDate>
</item>
</channel>
</rss>