[英]In AWS, boto3 how can I list all reservations regardless of the tags and other values
I want to list tags for all ec2 resources(customer-gateway | dhcp-options | image | instance | internet-gateway | network-acl | network-interface | reserved-instances | route-table | security-group | snapshot | spot-instances-request | subnet | volume | vpc | vpn-connection and vpn-gateway). 我想列出所有ec2资源的标签(客户网关| dhcp选项|图像|实例|互联网网关|网络acl |网络接口|预留实例|路由表|安全组|快照|点-实例请求|子网|卷| vpc | vpn连接和vpn网关)。
The following code lists all the resources for my ec2 client which have tags: 以下代码列出了带有标签的ec2客户端的所有资源:
session = boto3.Session(profile_name='default')
ec2Client = session.client('ec2', region_name='eu-west-2')
allTags = ec2Client.describe_tags()['Tags']
for tag in allTags:
print tag
PROBLEM 问题
The problem here is that only resources like in case of 'instances', the instances which do not have tags are not included. 这里的问题是仅不包括像“实例”的情况那样的资源,不包含没有标签的实例。 If there are 5 instances in ec2, 3 with tags and 2 without tags, the above code will list only those 3 instances with tags.
如果ec2中有5个实例,带有标签的3个实例和没有标签的2个实例,则上面的代码将仅列出这3个带有标签的实例。
DESIRED OUTPUT 期望的输出
I want all the resources (instances, VPCs, subnet, security groups etc.), to be listed whether the tags are defined or not. 我希望列出所有资源(实例,VPC,子网,安全组等),无论是否定义了标签。 If there are tags it shows tags, if not I still want it to be included in the result without tags.
如果有标签,它将显示标签,如果没有,我仍然希望它包含在没有标签的结果中。
One way is to use describe_xxx method for each resource to get the reservations and look for tags, but I would have to call it for every resource (like describe_instances(), describe_snapshots, describe_security_groups() etc.), which, in my opinion is not so cleaned and generic way solution. 一种方法是对每个资源使用describe_xxx方法来获取预留并查找标签,但是我必须为每个资源调用它(例如describe_instances(),describe_snapshots,describe_security_groups()等),我认为这是没有那么干净和通用的方法解决方案。
QUESTION 题
Is there any way using boto3 library to list all the resources, if the resources have tags show the tags too if not then show the resources only? 有什么方法可以使用boto3库列出所有资源,如果资源中有标签也显示标签(如果没有,则仅显示资源)?
As far as I know, boto3 does not offer a describe_all
method. 据我所知,boto3不提供
describe_all
方法。 To achieve your desired result, if I understand it correctly, you will have to separately describe all object types, ie describe_vpcs
, describe_instances
, etc, and then look for the tags in the resulting data structures. 为了获得理想的结果,如果我理解正确,则必须分别描述所有对象类型,即
describe_vpcs
, describe_instances
等,然后在生成的数据结构中查找标记。
For example, One could construct a single data structure from all the resulting method calls ( describe_instances
, describe_vpcs
, describe_subnets
, etc.) which will look like: 例如,可以从所有结果方法调用(
describe_instances
, describe_vpcs
, describe_subnets
等)构造一个数据结构,如下所示:
{
Vpcs: [{... Tags: [{...}]}]
Instances: [{... Tags: [{...}]}]
Subnets: [{... Tags: [{...}]}]
...
}
I believe this is the closest match to the object you are referring to in your question. 我相信这是与您在问题中所指对象最接近的匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.