Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With ec2 python API boto, how to get spot instance_id from SpotInstanceRequest?

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.

like image 479
Andrew_1510 Avatar asked Oct 09 '12 14:10

Andrew_1510


People also ask

What is Boto3 EC2?

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.


1 Answers

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)
like image 159
user116293 Avatar answered Sep 29 '22 06:09

user116293