2012-09-25 55 views
3

伙计们,我试图检索我的跑步机器的实例id,以及我在aws控制台中添加到别名中的别名。boto aws拉下实例列表

这是正确的方法吗?我没有收到回什么有趣的东西....

import boto 
botoEC2 = boto.connect_ec2('asdf','asdfasdfasdfasdf') 
rsv = botoEC2.get_all_instances() 
tags = botoEC2.get_all_tags() 
print tags 
dir (tags) 
print tags 
print tags.status 
print tags.pop 
print tags.count 
print tags.tagSet 
print tags.requestId 
print tags.index 
print tags. 
print tags.requestId 
print tags.index 
print tags.key_marker 

print tags 

输出: [标签:ec2tag,标签:名称,标签:名称,标签:名称,标签:名称,标签:名称,标签:姓名,标签:名称标签名称标签名称标签名称标签名称标签ec2tag标签名称标签名称标签名称标签名称标签名称

谢谢!

+1

如果您试图找出api,请尝试使用IPython,您可以在对象的方法和属性上标签完整。使这种发现变得简单。 – aychedee

+0

你在上面看到的是ipython复制/粘贴....所以我在打印tags.tab中忘记了什么? – Cmag

+0

好吧,get_all_tags返回一个标签对象列表,所以你看到的只是普通的列表方法。您需要从该列表中拖出一个标签对象'tag = tags [0]',然后尝试对该标签对象进行自动填充。 – aychedee

回答

6

您可以获取所有标签

import boto 
conn = boto.connect_ec2('asdf','asdfasdfasdfasdf') 

tags = conn.get_all_tags() 
for tag in tags: 
    print tag.name, tag.value 

或者你可以只用一个实例

reservation = conn.get_all_instances()[0] 
# Yeah I don't know why they have these stupid reservation objects either... 
instance = reservation.instances[0] 
print instance.tags 
# prints a dictionary of the tags {'Name': 'Given name'} 

UPDATE 2014年4月相关标签:Get all instances is going to change it's behaviour in the near future。有趣的是,它将开始返回一个EC2实例列表。现在应该使用get_all_reservations以避免在下一个主要版本更新期间代码被破坏。

+0

[0]的要点是什么?所有实例只有一个保留,还是有多个保留?甚至不知道我是否正确地问这个问题,但仍然想解释[0]。 :) – BillR

+1

我不质疑AWS API。我只知道'get_all_instances'返回一个保留对象的列表... – aychedee

+0

如果你一次请求多个实例,这将失败。在'conn.get_all_instances()]的res [res.instances for res]中使用'',并循环它们。 – tedder42