title: Wait for EC2 instance to start
body: <p>I have a custom AMI which runs my service. Using the AWS Java SDK, I create an EC2 instance using <code>RunInstancesRequest</code> from the AMI. Now before I begin to use my service, I must ensure that the newly created instance is up and running. I poll the instance using:</p>
  
  <pre><code>var transitionCompleted = false
  while (!transitionCompleted) {
    val currentState = instance.getState.getName
    if (currentState == desiredState) {
      transitionCompleted = true
    }
    if(!transitionCompleted) {
      try {
        Thread.sleep(TRANSITION_INTERVAL)
      } catch {
        case e: InterruptedException =&gt; e.printStackTrace()
      }
    }
  }
  </code></pre>
  
  <p>So when the <code>currentState</code> of the instance turns into <code>desiredState</code>(which is <code>running</code>), I get the status that the instance is ready. However any newly created instance, despite being in <code>running</code> state, is not available for immediate use as it is still initializing.</p>
  
  <p>How do I ensure that I return only when I'm able to access the instance and its services? Are there any specific status checks to make?</p>
  
  <p>PS: I use Scala</p>