Reputation: 704
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?
Upvotes: 16
Views: 29709
Reputation: 5678
Use the following shell script using AWS CLI to wait for the instance to be running and complete status checks successfully:
timeout=600 # Timeout in seconds
start_time=$(date +%s)
until [ "$(aws ec2 describe-instance-status --instance-id ${INSTANCE_ID} --query 'InstanceStatuses[0].InstanceStatus.Status' --output text)" = "ok" ] && \
[ "$(aws ec2 describe-instance-status --instance-id ${INSTANCE_ID} --query 'InstanceStatuses[0].SystemStatus.Status' --output text)" = "ok" ] && \
[ "$(aws ec2 describe-instances --instance-ids ${INSTANCE_ID} --query 'Reservations[0].Instances[0].State.Name' --output text)" = "running" ]; do
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
if [ "$elapsed_time" -ge "$timeout" ]; then
echo "Timeout reached. Instance didn't reach the desired state within $timeout seconds."
exit 1
fi
sleep 5
echo "Waiting for instance to be running and complete status checks..."
done
echo "Instance is now running fine and status checks have been completed!"
Note: ${INSTANCE_ID}
is the ID of the AWS EC2 instance.
Reference: Wait for an AWS EC2 instance to be running and complete status checks
Upvotes: 0
Reputation: 41190
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 pollsEC2.Client.describe_instances()
every 15 seconds until a successful state is reached. An error is returned after 40 failed checks.
Upvotes: 34
Reputation: 449
You can use boto3 waiters, https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#waiters
for this ex: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Waiter.InstanceRunning
Or in Java https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/ I am sure there are waiters implemented in all the AWS sdks.
Upvotes: 0
Reputation: 86077
Go use Boto3's wait_until_running
method:
http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Instance.wait_until_running
Upvotes: 0
Reputation: 9805
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.
Upvotes: 13
Reputation: 46839
Depending on what you are trying to do (and how many servers you plan on starting), instead of polling for the instance start events, you could install on the AMI a simple program/script that runs once when the instance starts and sends out a notification to that effect, i.e. to an AWS SNS Topic.
The process that needs to know about new servers starting could then subscribe to this SNS topic, and would receive a push notifications each time a server starts.
Solves the same problem from a different angle; your mileage may vary.
Upvotes: 3
Reputation: 8099
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.
Upvotes: 7