gpt4 book ai didi

python - CLI 命令的 boto 解释

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:13 25 4
gpt4 key购买 nike

以下 Boto 代码会打印出不需要的输出,我想查看的是 EBS 卷的状态而不是挂载点的状态。

EC2 预留结构:

object {1}
Reservations [1]
Instances[1]
BlockDeviceMappings[2]
DiviceName
Ebs{4}
Status
DeleteOnTermination
VolumeId
AttachTime

reservations = ec2Conn.get_all_instances(instance_ids=[my_id])
for reservation in reservations:
for instance in reservation.instances:
for BlockDeviceMappings in instance.block_device_mapping:
print(BlockDeviceMappings)

打印我:

/dev/sdf
/dev/sda1

以下 AWS CLI 命令会打印正确的状态:

aws ec2 describe-instances --instance myinstance-id --query Reservations[*].Instances[*].BlockDeviceMappings[*].{Ebs:Ebs.{s:Status}}

最佳答案

此代码片段将为您提供实例 ID、卷 ID 及其当前状态。通过挂载点,您还可以获得 boto.ec2.blockdevicemapping 对象,您可以使用它访问其属性,如下例所示。希望对您有所帮助。

from boto.regioninfo import *
from boto.ec2.connection import EC2Connection

# AWS connect info
aws_access_key_id='########## AWS Access Key ############'
aws_secret_access_key='########### AWS Secret Key ############'
region_name='ap-southeast-1'
region_ec2_endpoint='ec2.ap-southeast-1.amazonaws.com'

# Connect EC2
aws_region = RegionInfo(name=region_name, endpoint=region_endpoint)
conn = EC2Connection(aws_access_key_id,aws_secret_access_key,region=aws_region)

reservations = conn.get_all_instances(instance_ids=[my_ids])
for each in reservations:
for vol in each.instances[0].block_device_mapping.items():
print str(each.instances[0].id) + " " + vol[1].volume_id + " "+ vol[1].status

一些与此类似的其他用例 https://github.com/dvopsway/aws_scripts/

关于python - CLI 命令的 boto 解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32659749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com