python - AWS 提取卷 AttachmentID Boto3
问题描述
我需要在我的帐户中生成所有 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)
解决方案
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
推荐阅读
- laravel - Laravel取消链接不会删除文件
- javascript - 我无法在 excel web add in (javascript) 中保存对代码的更改
- r - 如何计算 Quanteda 中多词表达的频率?
- linux - 源子中的父脚本名称
- wordpress - 在 Woocommerce 产品属性中保存 URL
- flutter - 将 DraggableScrollableSheet 添加到 Sliver 页面的底部
- excel - Excel VBA多次重复3行相同的值
- c++ - 在 C++ 中检查枚举变量的无效值
- lumen - 找不到路由时的 Lumen API 自定义错误消息
- python - django python - 如何列出列表的渐进数值