2012-10-09 57 views
5

當使用boto時,Amazon aws python API。使用ec2 python API boto,如何從SpotInstanceRequest獲取spot instance_id?

ec2_connection.request_spot_instances(...) 
# This will return an ResultSet of SpotInstanceRequest 

如何從SpotInstanceRequest獲得instance_ids?

更新:我就是這麼做的,很多遊戲和googleing後,希望這幫助:

ec2_connection.get_all_spot_instance_requests(request_ids=[my_spot_request_id, ]) 

這將返回更新SpotInstanceRequest,當實例準備好了,我們可以從中獲取* instance_id *。

回答

6

我做了類似的事情:定期檢查,看是否現貨實例請求ID通過 ec2_connection.request_spot_instances(...)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) 
+1

而不是做的' ''while job_instance_id == None:'''你可以用更pythonic的方式來做 - '''而不是job_instance_id:''' – iamkhush

0
spot_instance_requests = aws.ec2_get_connection().request_spot_instances(...) 
MAX_MINUTES = 180 
spot_instance_request_ids = [sir.id for sir in spot_instance_requests] 
for _ in range(MAX_MINUTES): 
    log.info('waiting for spot instances to start', request_ids=spot_instance_request_ids, seconds=60) 
    time.sleep(60) 
    spot_instance_requests = aws.ec2_get_connection().get_all_spot_instance_requests(
     request_ids=spot_instance_request_ids) 
    if any(sir.instance_id for sir in spot_instance_requests): 
     log.info('spot instance started. waiting...', seconds=60*5) 
     time.sleep(60*5) 
     break 
else: 
    raise Exception("Spot instances didn't start in {0} minutes!".format(MAX_MINUTES)) 
相關問題