Get instance by instance-id

↘锁芯ラ 提交于 2019-12-23 07:27:48

问题


I need to get the instance by instance-id, is it possible to do it without requesting a list of all instances?

I've tried:

ec2_conn = boto.connect_ec2(aws_access_key_id=key, aws_secret_access_key=access)
c2.get_all_instances([instanceId])

It works, but is there some other way to get the instance?

The reason I'm asking is I received UnauthorizedOperation for get_all_instances request, so I would prefer to change the request, not the security settings.


回答1:


Maybe boto has evolved since the time the OP asked the question, but this deserves the up to date answer to be added here:

reservations = ec2conn.get_all_instances(instance_ids=['i-12345678'])
instance = reservations[0].instances[0]



回答2:


You can try with

reservations = ec2_conn.get_all_instances(filters={'instance-id' : 'i-xxxxxxxx'})
new_instance = reservations[0].instances[0]

it will definitely work.




回答3:


instances = get_only_instances(instance_ids=['i-12345678'])

Regarding the above answer using

get_all_instances()

, from the BOTO API --

get_all_instances() is deprecated in favor of get_all_reservations(). 

A future major release will change get_all_instances() to return a list of 
boto.ec2.instance.Instance objects as its name suggests. 
To obtain that behavior today, use get_only_instances().


来源:https://stackoverflow.com/questions/12096241/get-instance-by-instance-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!