AWS EBS Volume – Python – Find all fields info as shown in AWS EC2 EBS Volume Console
我正在尝试创建一个 Python 脚本来针对所有可用的 AWS EBS 卷生成一个 csv 格式文件,并显示我在 AWS EC2 EBS 卷控制台中看到的所有字段值。
1
2 3 4 5 6 7 8 9 10 11 |
[arun@Andrews-MBP-2 ~/aks/always-latest-ws-sunny/anisble] $ cat ~/aws-vol-info.py
import boto3 #define the connection volumes = ec2.volumes.all() for vol in volumes: |
这个脚本给了我以下错误信息。原因:如果我不使用 vol.type 字段,则上述脚本有效。它不起作用,因为体积变量在运行 ec2.volumes.all().
时从未将其纳入其值
1
2 3 4 5 6 |
[arun@Andrews-MBP-2 ~/aks/always-latest-ws-sunny/anisble] $ python ~/aws-vol-info.py
Traceback (most recent call last): File“/Users/arun/aws-vol-info.py”, line 9, in <module> print“Created(“ + str(vol.create_time) +“),VolumeState(“ + str(vol.state) +“),VolumeID(“ + str(vol.id) +“),VolumeSize(“ + str(vol.size) +“)” + vol.type AttributeError: ‘ec2.Volume’ object has no attribute ‘type’ [arun@Andrews-MBP-2 ~/aks/always-latest-ws-sunny/anisble] $ |
我应该在上面的脚本中使用/更改什么库/函数,使用它可以显示 EBS 卷的所有字段或更有意义的字段(我在 AWS EC2 EBS 卷控制台中看到的),请参见下图AWS 控制台中的可用字段。
我在 Github 上在线找到了另一个脚本 (#2),它似乎可以打印更多字段,但它给出了下面列出的另一个错误。我成功地运行了 python -m pip install –user aws 或 python -m pip install aws 或 pip install aws,或者运行了脚本(仅在 #Import classes from aws package 行之后包含行,在他的存储库的 aws 文件夹内(克隆后),但仍然出现错误。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import boto.ec2
class Volumes: def __init__(self): ”’ Volumes Constructor ”’ def list_volumes(conn): # if volumes found #print attachment set object #Import classes from aws package connInst = Connection() #instantiate Volumes and list volumes |
脚本#2 错误是:
1
2 3 4 |
Traceback (most recent call last):
File“/Users/arun/aws-vol-info2.py”, line 30, in <module> from aws import Connection ImportError: cannot import name Connection |
如果我在脚本# 2 中注释 from aws … .. 的行并且只使用/取消注释 import aws,那么我会得到:
1
2 3 4 |
Traceback (most recent call last):
File“/Users/arun/aws-vol-info2.py”, line 35, in <module> connInst = Connection() NameError: name ‘Connection’ is not defined |
我认为您正在寻找 vol.volume_type。您可以在此处查看 ec2.Volume 中的完整属性列表:
http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#volume
- 似乎很接近,我试过这个 import boto3 ec2 = boto3.resource(‘ec2’, region_name=”us-west-2″) volumes = ec2.volumes.all() for vol in volumes: for inst_vol in ec2.Volume(vol.id): print”–” + str(inst_vol.volume_type) 但它仍然出错。戳进去。
- 这行得通: volume = ec2.Volume(‘vol-b4e1fabc’) print volume.volume_type 但是,我想知道为什么当我在上面使用 2 for 循环时它不起作用。第一个 for 循环会将有效的卷 ID vol-xxxxx 传递给 ec2.Volume(<here>),但它因以下错误而哭泣:for inst_vol in ec2.Volume(vol.id): TypeError: ‘ec2.Volume’ object is not iterable。 `
- 因为 ec2.Volume 不是 list 而是一个类。你不能遍历它。您上面的原始打印语句现在可以使用:print”Created(” + str(vol.create_time) +”),VolumeState(” + str(vol.state) +”),VolumeID(” + str(vol.id) +”),VolumeSize(” + str(vol.size) +”)” + vol.volume_type
- 没关系,谢谢它现在起作用了。我不必使用第二个 for 循环。仍在检查是否会根据 AWS 控制台为我提供所有字段。
可以增强这个脚本以显示更有意义的信息,但是使用 jpavs 的提示,我想出了这个脚本 ~/aws-vol-info.py:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import boto3
# Define the connection # Find all volumes # Loop through all volumes and pass it to ec2.Volume(‘xxx’) # The following next 2 print statements variables apply only in my case. |
Ran: python ~/aws-vol-info.py 它为我提供了 Python 脚本中提到的所有字段的 CSV 格式值。由于库不提供这些,因此缺少 (2-3) 个 AWS 控制台字段,但无论我能从上面得到什么,或者如果我深入研究 iv.attachments[0][‘<somekey>’] 或 iv.tags[0][‘<somekey>’],此时对我来说就足够了。
PS:iv.attachments 和 iv.tags 都是 iv 对象中的列表/字典类型变量,因此您可以通过显示您想要从中获取的确切内容来增强脚本。因此,如果你想要 InstanceID 那么你可以使用这个: str(iv.attachments[0][‘InstanceId’]) 来打印它。
要获得更好的版本:在此处检查 .python 脚本:telegraf – exec 插件 – aws ec2 ebs volumen info – 度量解析错误,原因:[缺少字段] 或遇到的错误:[无效数字]
在这里也找到了这个有用的脚本:http://www.n2ws.com/blog/ebs-report.html
来源:https://www.codenong.com/42596583/