0

I'm using Lambda to run through my AWS account, returning a list of all instances. I need to be able to print out all of the 'VolumeId' values, but I can't work out how to access them as they are nested. I am able to print out the first VolumeId for each instance, however, some of the instances have several volumes, and some only have one. I think I know why I get these results, but I can't work out what to do to get all of them back.

Here's a snippet of what the JSON for one instance looks like:

{  
'Groups':[],
'Instances':[  
  {  
     'AmiLaunchIndex':0,
     'ImageId':'ami-0',
     'InstanceId':'i-0123',
     'InstanceType':'big',
     'KeyName':'nonprod',
     'LaunchTime':'date',
     'Monitoring':{  
        'State':'disabled'
     },
     'Placement':{  
        'AvailabilityZone':'world',
        'GroupName':'',
        'Tenancy':'default'
     },
     'PrivateDnsName':'secret',
     'PrivateIpAddress':'1.2.3.4',
     'ProductCodes':[  

     ],
     'PublicDnsName':'',
     'State':{  
        'Code':80,
        'Name':'stopped'
     },
     'StateTransitionReason':'User initiated',
     'SubnetId':'subnet-1',
     'VpcId':'vpc-1',
     'Architecture':'yes',            
     'BlockDeviceMappings':[  
        {  
           'DeviceName':'/sda',
           'Ebs':{  
              'AttachTime':'date',
              'DeleteOnTermination':True,
              'Status':'attached',
              'VolumeId':'vol-1'
           }
        },
        {  
           'DeviceName':'/sdb',
           'Ebs':{    
              'AttachTime':'date'),
              'DeleteOnTermination':False,
              'Status':'attached',
              'VolumeId':'vol-2'
           }
        }
     ],

This is what I'm doing to get the first VolumeId:

ec2client = boto3.client('ec2')
ec2 = ec2client.describe_instances()
for reservation in ec2["Reservations"]:
    for instance in reservation["Instances"]:
        instanceid = instance["InstanceId"]

        volumes = instance["BlockDeviceMappings"][0]["Ebs"]["VolumeId"]
        print("The associated volume IDs for this instance are: ",(volumes))

I think the reason that I'm getting just the first ID is because I'm referencing the first element within "BlockDeviceMappings", but I can't work out how to get the other ones. If I try it without specifying the [0], I get the list indices must be integers or slices, not str error. I tried to use a dictionary instead of a list too, but felt like I was barking up the wrong tree with that one. Any suggestions/help would be appreciated!

1 Answer 1

1

One possible answer, not particularly pythonic

...

id_list = []
volumes_data = instance["BlockDeviceMappings"]
for element in volumes_data:
    id_list.append(element["Ebs"]["VolumeId"])

Or else use json.loads and then iterate though json using .get syntax like the final answer in this

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, that's all I needed. I did try doing it with json.load, json.loads, & json.dumps but struggled to do what I needed. Kept getting that same list indices error. Thanks for the solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.