简体   繁体   English

AWS BOTO - 使用标记查找ec2实例的卷ID

[英]AWS BOTO - find volume ID of an ec2 instance using tag

We have defined tags for our ec2 instances. 我们为ec2实例定义了标签。 We would like to use the python boto module to fetch a list of volumeId's or EBS volumes by using tags. 我们想使用python boto模块通过使用标签来获取volumeId或EBS卷的列表。 Tags have already been created for the instance. 已经为该实例创建了标签。 We would like to use the name tage which in this case is "dev" and list the EBS volumes associated with it. 我们想使用名称tage,在这种情况下是“dev”并列出与之关联的EBS卷。 Kindly help 请帮助

It would be best to try to write some code to try to solve the problem and then post a specific question if that code is not working. 最好尝试编写一些代码来尝试解决问题,然后发布一个特定的问题,如果该代码不起作用。 If you are having problems getting started, here are a few tips. 如果您在开始时遇到问题,请参考以下提示。

The first thing you will need to do is find all of the instances that have a specific tag of the form name=<somevalue> . 您需要做的第一件事是找到具有表单name=<somevalue>的特定标记的所有实例。 In boto, you would use the filter parameter of the get_all_instances method to accomplish this. 在boto中,您将使用get_all_instances方法的filter参数来完成此操作。 Something like this should work: 这样的事情应该有效:

reservations = conn.get_all_instances(filters={'name': ['somevalue']})

This will return a list of Reservation objects which match the query. 这将返回与查询匹配的Reservation对象列表。 Inside the Reservation object is an attribute called instances which is a list of Instance objects. Reservation对象内部是一个名为instances的属性,它是Instance对象的列表。

For each of the Instance objects in that list you will want to find the EBS volumes associated with the instance. 对于该列表中的每个Instance对象,您将需要查找与该实例关联的EBS卷。 Again, you will want to use the filters parameter to accomplish this. 同样,您将需要使用filters参数来完成此操作。 In this case the filter name you want is attachment.instance-id . 在这种情况下,您需要的过滤器名称是attachment.instance-id

volumes = conn.get_all_volumes(filters={'attachment.instance-id': [instance.id]})

Where instance is one of the Instance objects returned from the first call. 其中instance是第一次调用返回的Instance对象之一。 This will return a list of Volume objects for all of the EBS volumes currently attached to instance . 这将返回当前附加到instance所有EBS卷的Volume对象列表。

I hope that helps. 我希望有所帮助。 Try to write the code and then if you run into specific questions, ask them here. 尝试编写代码然后如果遇到具体问题,请在此处询问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM