When using boto, Amazon aws python API.
ec2_connection.request_spot_instances(...)
# This will return an ResultSet of SpotInstanceRequest
How can I get instance_ids from the SpotInstanceRequest
?
UPDATE: I did it this way, after a lot playing and googleing, hope this help:
ec2_connection.get_all_spot_instance_requests(request_ids=[my_spot_request_id, ])
This will return the updated SpotInstanceRequest
, when the instance is ready, we can get *instance_id* from it.
Dec 16, 2020 • ec2. AWS Boto3 is the Python SDK for AWS. Boto3 can be used to directly interact with AWS resources from Python scripts. In this tutorial, we will look at how we can use the Boto3 library to perform various operations on AWS EC2. Table of contents.
I did something similar: check periodically to see if the spot instance request id returned by
ec2_connection.request_spot_instances(...)
is matched to an instance in the results of
conn.get_all_spot_instance_requests(...)
:
conn = boto.ec2.connect_to_region(region_name=region_name, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
req = conn.request_spot_instances(price=MAX_SPOT_BID,instance_type=instance_type,image_id=AMI_ID,availability_zone_group=region_name,key_name=KEY_PAIR_PEM[:-4],security_groups=security_groups)
job_instance_id = None
while job_instance_id == None:
print "checking job instance id for this spot request"
job_sir_id = req[0].id # spot instance request = sir, job_ is the relevant aws item for this job
reqs = conn.get_all_spot_instance_requests()
for sir in reqs:
if sir.id == job_sir_id:
job_instance_id = sir.instance_id
print "job instance id: " + str(job_instance_id)
break
time.sleep(SPINUP_WAIT_TIME)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With