title: How to wait untill EC2 (created in Lambda) 2/2 status check is passed in Python using Boto3 and Calling Lambda function from Shell script
body: <p>The Use case that I am executing is:</p>
  
  <ol>
  <li>Create EC2 instance </li>
  <li>In that wait till that EC2 instance is up and running </li>
  <li>Deploy build by running a shell script in EC2 instance </li>
  <li>Get the success response from shell script </li>
  <li>Terminate the instance </li>
  <li>After the instance is terminated and the response of running shell script is with success code I need to run different pipeline in Jenkins which will in turn call creating EC2 instance and so on.</li>
  </ol>
  
  <p>The main purpose of waiting is: </p>
  
  <ol>
  <li>If my lambda function exit without executing the entire code my Build will not be deployed and next jenkins pipeline (step 6) will run which I don't want until I complete the first deployment successfully. </li>
  <li>If there is any error in build deploy (Step 3), it should return that error and should not run my next pipeline this I am not able to handle as my response code is returned well before my EC2 instances is running.</li>
  </ol>
  
  <p>I am invoking the Lambda function from shell script (shellScript.sh), and the command to invoke is in below code which is from AWS documentation.</p>
  
  <p>In the Lambda function I have following python code to create an EC2 instance first and then detect whether an EC2 is running along with 2/2 status check, but it exits and return 200 success code to shell script when "instance state" shows running (Lambda still continues with other commands to run after that which are not mentioned here and EC2 status is in Initializing state).</p>
  
  <p>Which API function should I use to block until EC2 "status check" show "2/2 checks passed"? (I don't want my Lambda function to exit until the status shows 2/2 checks passed.)</p>
  
  <p>Note: Print response gives the status of 'ok'</p>
  
  <p>I referred to Stack overflow link below and tried those options (as seen in my code below) but it didn't work for me.</p>
  
  <p><a href="https://stackoverflow.com/questions/48794485/how-to-block-until-ec2-status-check-is-passed-using-python-boto3">How to block until EC2 status check is passed using Python Boto3?</a></p>
  
  <p>ShellScript.sh:</p>
  
  <pre><code>aws lambda invoke --invocation-type RequestResponse --function-name &lt;my-function-name&gt; --payload '{"Key": "'$value'"}' /dev/stdout
  </code></pre>
  
  <p>Lambda_function.py:</p>
  
  <pre class="lang-py prettyprint-override"><code>    instance = EC2.run_instances(
          ImageId=&lt;image-id&gt;,
          InstanceType=&lt;instance-type&gt;,
          MinCount=1, 
          MaxCount=1,
          .......
      )
      instance_id = instance['Instances'][0]['InstanceId']
      ec2instance = ec2.Instance(id=instance_id)
      ec2instance.wait_until_running()
      waiter = EC2.get_waiter('instance_status_ok')
      waiter.wait(
          InstanceIds=[instance_id],
          WaiterConfig={
              'Delay': 15,
              'MaxAttempts': 40
          },
          IncludeAllInstances = True
      )
      response = EC2.describe_instance_status(
          InstanceIds=[instance_id],
          IncludeAllInstances=True
      )
      print(response)
  </code></pre>