boto3 wait_until_running doesn't work as desired
Question:
I’m trying to write a script using boto3 to start an instance and wait until it is started. As per the documentation of wait_until_running, it should wait until the instance is fully started (I”m assuming checks should be OK) but unfortunately it only works for wait_until_stopped and incase of wait_until_running it just starts the instance and doesn’t wait until it is completely started. Not sure if I’m doing something wrong here or this is a bug of boto3.
Here is the code:
import boto3
ec2 = boto3.resource('ec2',region_name="ap-southeast-2")
ec2_id = 'i-xxxxxxxx'
instance = ec2.Instance(id=ec2_id)
print("starting instance " + ec2_id)
instance.start()
instance.wait_until_running()
print("instance started")
Answers:
I’ve tried instance.wait_until_running(). It took time to update the instance to running state. As per amazon docs link, it says that the instances take a minimum of 60seconds to spin up. Here’s a sample code that worked for me. Hope it helps!
;to create 5 instances
ec2.create_instances(ImageId='<ami-image-id>', MinCount=1, MaxCount=5)
time.sleep(60)
;print your instances
Thanks to @Mark B @Madhurya Gandi here is the solution that worked in my case:
import boto3,socket
retries = 10
retry_delay=10
retry_count = 0
ec2 = boto3.resource('ec2',region_name="ap-southeast-2")
ec2_id = 'i-xxxxxxxx'
instance = ec2.Instance(id=ec2_id)
print("starting instance " + ec2_id)
instance.start()
instance.wait_until_running()
while retry_count <= retries:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((instance.public_ip_address,22))
if result == 0:
Print "Instance is UP & accessible on port 22, the IP address is: ",instance.public_ip_address
break
else:
print "instance is still down retrying . . . "
time.sleep(retry_delay)
except ClientError as e:
print('Error', e)
I believe the right way to do it is as follows:
instance.wait_until_running(
Filters=[
{
'Name': 'instance-state-name',
'Values': [
'running',
]
},
]
)
tested and worked for me
This worked for me:
waiter.wait(
InstanceIds = [instanceID],
Filters = [
{
"Name": "instance-status.reachability" ,
"Values": [
"passed"
]
}
]
)
I’m trying to write a script using boto3 to start an instance and wait until it is started. As per the documentation of wait_until_running, it should wait until the instance is fully started (I”m assuming checks should be OK) but unfortunately it only works for wait_until_stopped and incase of wait_until_running it just starts the instance and doesn’t wait until it is completely started. Not sure if I’m doing something wrong here or this is a bug of boto3.
Here is the code:
import boto3
ec2 = boto3.resource('ec2',region_name="ap-southeast-2")
ec2_id = 'i-xxxxxxxx'
instance = ec2.Instance(id=ec2_id)
print("starting instance " + ec2_id)
instance.start()
instance.wait_until_running()
print("instance started")
I’ve tried instance.wait_until_running(). It took time to update the instance to running state. As per amazon docs link, it says that the instances take a minimum of 60seconds to spin up. Here’s a sample code that worked for me. Hope it helps!
;to create 5 instances
ec2.create_instances(ImageId='<ami-image-id>', MinCount=1, MaxCount=5)
time.sleep(60)
;print your instances
Thanks to @Mark B @Madhurya Gandi here is the solution that worked in my case:
import boto3,socket
retries = 10
retry_delay=10
retry_count = 0
ec2 = boto3.resource('ec2',region_name="ap-southeast-2")
ec2_id = 'i-xxxxxxxx'
instance = ec2.Instance(id=ec2_id)
print("starting instance " + ec2_id)
instance.start()
instance.wait_until_running()
while retry_count <= retries:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((instance.public_ip_address,22))
if result == 0:
Print "Instance is UP & accessible on port 22, the IP address is: ",instance.public_ip_address
break
else:
print "instance is still down retrying . . . "
time.sleep(retry_delay)
except ClientError as e:
print('Error', e)
I believe the right way to do it is as follows:
instance.wait_until_running(
Filters=[
{
'Name': 'instance-state-name',
'Values': [
'running',
]
},
]
)
tested and worked for me
This worked for me:
waiter.wait(
InstanceIds = [instanceID],
Filters = [
{
"Name": "instance-status.reachability" ,
"Values": [
"passed"
]
}
]
)