5

使用boto时,Amazon aws python API。

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

如何从 中获取 instance_ids SpotInstanceRequest

更新:我是这样做的,经过大量的游戏和谷歌搜索,希望这会有所帮助:

ec2_connection.get_all_spot_instance_requests(request_ids=[my_spot_request_id, ])

这将返回updated SpotInstanceRequest,当实例准备好时,我们可以从中获取 *instance_id* 。

4

2 回答 2

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)
于 2013-11-08T22:02:22.673 回答
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))
于 2015-03-26T20:23:29.850 回答