列出 Boto 中的 EC2 卷 [英] List EC2 volumes in Boto

查看:28
本文介绍了列出 Boto 中的 EC2 卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出附加到我的 EC2 实例的所有卷.

I want to list all volumes attached to my EC2 instance.

我可以用我的代码列出卷:

I can list volumes with my code:

conn = EC2Connection()
attribute = get_instance_metadata()
region=attribute['local-hostname'].split('.')[1]
inst_id = attribute['instance-id']
aws = boto.ec2.connect_to_region(region)
volume=attribute['local-hostname']
volumes = str(aws.get_all_volumes(filters={'attachment.instance-id': inst_id}))

但这会导致:

[vol-35b0b5fa, Volume:vol-6cbbbea3]

我需要类似的东西:

vol-35b0b5fa
vol-6cbbbea3

推荐答案

如果您使用的是 Boto3 库,那么这里是列出所有附加卷的命令

If you are using Boto3 library then here is the command to list out all attached volumes

import boto3
ec2 = boto3.resource('ec2', region_name='us-west-2')
volumes = ec2.volumes.all() # If you want to list out all volumes
volumes = ec2.volumes.filter(Filters=[{'Name': 'status', 'Values': ['in-use']}]) # if you want to list out only attached volumes
[volume for volume in volumes]

这篇关于列出 Boto 中的 EC2 卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆