Helper method for waiting EC2 instances until they get ready

Automating EC2 instance creation is a necessity when it comes to executing a series of try-outs. The method given below is a nice helper one that waits until your EC2 instances are ready before doing any heavy-load work.

private static void sleepUntilInstancesIsRunning(AmazonEC2 ec2, 
Set<String> instanceIdList) throws InterruptedException {
while(true) {
DescribeInstancesResult instanceStatusResult =
ec2.describeInstances(new DescribeInstancesRequest().
withInstanceIds(instanceIdList));

if(Iterables.all(instanceStatusResult.getReservations(),
new Predicate<Reservation>() {
@Override
public boolean apply(Reservation input) {
return Iterables.all(input.getInstances(),
new Predicate<Instance>() {
@Override
public boolean apply(Instance input) {
return input.getState().getName().
equalsIgnoreCase(InstanceStateName.Running.toString());
}
});
}
})){
break;
} else {
System.out.println("waiting...");
Thread.sleep(1000);
}
}
}

You can find the gist of the code in here.