Reputation: 323
Following Boto code prints me the undesired output I would to see the status of my EBS volume status not the mount point.
EC2 Reservation Structure:
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)
prints me:
/dev/sdf
/dev/sda1
Following AWS CLI command prints me the right status:
aws ec2 describe-instances --instance myinstance-id --query Reservations[*].Instances[*].BlockDeviceMappings[*].{Ebs:Ebs.{s:Status}}
Upvotes: 1
Views: 420
Reputation: 335
This code snippet will give you instance id , volume id and its current state. With your mount point you also get an boto.ec2.blockdevicemapping object , using which you can access its attributes as in the example below. Hope it helps.
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
some other use cases similar to this https://github.com/dvopsway/aws_scripts/
Upvotes: 1
Reputation: 323
Using CLI
response = json.loads(subprocess.check_output(
['aws', 'ec2', 'describe-volumes', '--volume-ids', Volume_ID, '--query',
'Volumes[*].{AZ:AvailabilityZone, size:Size, Device:Attachments[*].{Device:Device}}']))
Volume_AZ = response[0]['AZ']
Volume_Mount_Point = response[0]['Device'][0]['Device']
Volume_size = response[0]['size']
Upvotes: 0
Reputation: 35139
If you look at the raw
response then it looks similar to that:
<blockDeviceMapping>
<item>
<deviceName>/dev/sda1</deviceName>
<ebs>
<volumeId>vol-xxxxxxxxxx</volumeId>
<status>attached</status>
<attachTime>2014-04-01T02:23:54.000Z</attachTime>
<deleteOnTermination>true</deleteOnTermination>
</ebs>
</item>
<item>
<deviceName>/dev/sdb</deviceName>
<ebs>
<volumeId>vol-xxxxxxx</volumeId>
<status>attached</status>
<attachTime>2014-04-01T02:23:54.000Z</attachTime>
<deleteOnTermination>false</deleteOnTermination>
</ebs>
</item>
</blockDeviceMapping>
So All you have to do is just dig one level deeper then you are.
>>> import boto
>>> ec2 = boto.connect_ec2()
>>> [ebs.status for _, ebs in ec2.get_all_instances(instance_ids=['i-xxxxxx'])[0].instances[0].block_device_mapping.items()]
[u'attached', u'attached']
If your goal is to get EBS Status that would "look" very close to the aws cli, you can do it using boto3
:
>>> import boto3
>>> boto3.client('ec2')
>>> c.describe_instances(InstanceIds=['i-xxxxxx'])['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['Status']
'attached'
Upvotes: 0