【问题标题】:AWS Extract Volume AttachmentID Boto3AWS 提取卷 AttachmentID Boto3
【发布时间】:2020-02-14 04:04:59
【问题描述】:

我需要生成我帐户中所有 ebs 卷和实例附件的列表。我知道如何做到这一点,但我想使用 Python。如何提取帐户中所有正在使用的卷的附件 ID?

    # Get all in-use volumes in all regions  
    result = ec2.describe_volumes( Filters=[{'Name': 'status', 'Values': ['in-use']}])
    for volume in result['Volumes']:
        attachment = ec2.describe_volumes().get('AttachmentsID,[]')
        print(attachment)

【问题讨论】:

    标签: python amazon-web-services amazon-ec2 boto3


    【解决方案1】:

    describe_volumes() 已经在'Attachments' 中返回了您需要的信息。要打印所有卷,您需要查询所有区域并处理分页。

    import boto3
    import botocore.exceptions
    
    session = boto3.Session()
    for region in session.get_available_regions('ec2'):
        print(region)
        try:
            ec2 = session.client('ec2', region_name=region)
            for res in ec2.get_paginator('describe_volumes').paginate(Filters=[{'Name': 'status', 'Values': ['in-use']}]):
                for volume in res['Volumes']:
                    for attachment in volume['Attachments']:
                        print(attachment['InstanceId'], attachment['VolumeId'])
        except botocore.exceptions.ClientError as e:
            if e.response['Error']['Code'] == 'AuthFailure':
                print('No access to', region)
            else:
                raise
    

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      • 2017-06-01
      • 2019-09-16
      • 1970-01-01
      相关资源
      最近更新 更多