Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EC2: Waiting until a new instance is in running state

Tags:

amazon-ec2

I would like to create a new instance based on my stored AMI.

I achieve this by the following code:

RunInstancesRequest rir = new RunInstancesRequest(imageId,1, 1);
// Code for configuring the settings of the new instance
...
RunInstancesResult runResult = ec2.runInstances(rir);

However, I cannot find a wait to "block"/wait until the instance is up and running apart from Thread.currentThread().sleep(xxxx) command.

On the other hand, StartInstancesResult and TerminateInstancesResult gives you a way to have access on the state of the instances and be able to monitor any changes. But, what about the state of a completely new instance?

like image 310
Dio Avatar asked Sep 27 '13 15:09

Dio


People also ask

Why is my EC2 instance stuck in the stopping state?

Instances might appear "stuck" in the stopping state when there is a problem with the underlying hardware hosting the instance. This can also occur when hibernating a hibernation-enabled instance. Note: To check the most recent state of your instance, choose the refresh icon in the Amazon EC2 console.

How long does it take to launch EC2 instance?

You do not need to wait for the Instance Status Check to complete before using an Amazon EC2 instance. Linux instances are frequently ready 60-90 seconds after launch.

How long do EC2 instances stay in terminated state?

Terminated instances remain visible after termination (for approximately one hour). By default, Amazon EC2 deletes all EBS volumes that were attached when the instance launched. Volumes attached after instance launch continue running. You can stop, start, and terminate EBS-backed instances.

What are the three main states of EC2 instances?

One of the ways to reduce costs with Amazon EC2 is to choose the right pricing option for the way your applications run. There are three main purchasing options for EC2 instances: on-demand, reserved, and spot instances.


3 Answers

boto3 has:

instance.wait_until_running()

From the boto3 docs:

Waits until this Instance is running. This method calls EC2.Waiter.instance_running.wait() which polls EC2.Client.describe_instances() every 15 seconds until a successful state is reached. An error is returned after 40 failed checks.

like image 159
Zags Avatar answered Nov 02 '22 23:11

Zags


From the AWS CLI changelog for v1.6.0:

Add a wait subcommand that allows for a command to block until an AWS resource reaches a given state (issue 992, issue 985)

I don't see this mentioned in the documentation, but the following worked for me:

aws ec2 start-instances --instance-ids "i-XXXXXXXX"
aws ec2 wait instance-running --instance-ids "i-XXXXXXXX"

The wait instance-running line did not finish until the EC2 instance was running.

I don't use Python/boto/botocore but assume it has something similar. Check out waiter.py on Github.

like image 39
dnlbrky Avatar answered Nov 02 '22 23:11

dnlbrky


Waiting for the EC2 instance to get ready is a common pattern. In the Python library boto you also solve this with sleep calls:

   reservation = conn.run_instances([Instance configuration here])
   instance = reservation.instances[0]

   while instance.state != 'running':
       print '...instance is %s' % instance.state
       time.sleep(10)
       instance.update()

With this mechanism you will be able to poll when your new instance will come up.

like image 45
j0nes Avatar answered Nov 03 '22 01:11

j0nes