简体   繁体   English

在AWS中,boto3如何列出所有保留,而不管标签和其他值如何

[英]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_vpcsdescribe_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_instancesdescribe_vpcsdescribe_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.

相关问题 我们如何使用 Boto3 列出 aws 参数存储中的所有参数? boto3 文档中没有 ssm.list_parameters 吗? - How can we list all the parameters in the aws parameter store using Boto3? There is no ssm.list_parameters in boto3 documentation? 如何使用 boto3 获取所有 AWS AMI 的列表? - How to get the list of all AWS AMIs using boto3? AWS Glue Boto3 - 在哪里可以找到服务员名单? - AWS Glue Boto3 - Where can I find the list of the waiters? 使用boto3(AWS)上传到Glacier时,如何确定捕获所有可能的异常 - How can I be sure to catch all possible exceptions when uploading to Glacier with boto3 (AWS) 如何将 json“dict”更改为 aws boto3 tagset list of dicts 的可消耗格式 - how can I change json "dict" into a consumable format for aws boto3 tagset list of dicts 如何通过boto3获取aws卷可用大小 - How can i get the aws volumes available size by boto3 如何使用boto3更改AWS账户电子邮件? - How can I change the AWS Account Email with boto3? 如何使用 boto3 列出附加到 IAM 角色的所有资源? - How can I list the all the resources attached to a IAM role using boto3? Python / Boto3 / 如何获得带有分页的标签列表? - Python / Boto3 / How can i get a tag list with pagination? 如何在 boto3 中为 AWS EC2 实例设置标签 - How to set tags for AWS EC2 instance in boto3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM