Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto3 get EC2 instance's volume

I am trying to get volume-id list of aws instance using boto 3, I am getting sort of collection manager but I don't know how to get the data inside.

import boto3
ec2 = boto3.resource('ec2', region_name='us-west-2')
instance = ec2.Instance('i-xxxxxx')
volumes = instance.volumes.all()
print volumes

The answer I got is:

ec2.Instance.volumesCollection(ec2.Instance(id='i-xxxxxx'), ec2.Volume)

How I am using the "ec2.Volume" to get the volume id

Thanks, Cfir.

like image 590
cfircoo Avatar asked Dec 07 '22 22:12

cfircoo


1 Answers

It's just an iterable of objects so

for v in volumes:
    print(v.id)

if you want to get a list of id :

l = [v.id for v in volumes]
like image 162
polku Avatar answered Dec 11 '22 11:12

polku