I have the below JSON data which is a shortened version of AWS "cli describe ec2 data" output.
I need to use Python so that I can query an InstanceId value and get all of of its VolumeId values (there may be more than 1 entry) and its PrivateIpAddress value.
The output would look something like this:
i-01,10.1.1.1,vol-123,vol-456
i-02,10.2.2.2,vol-789
I am new to Python and cant seem to find the answer on how to do this. Can anyone help?
{
"Reservations": [
{
"Instances": [
{
"InstanceId": "i-01",
"PrivateIpAddress": "10.1.1.1",
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sda",
"Ebs": {
"Status": "attached",
"VolumeId": "vol-123",
}
},
{
"DeviceName": "/dev/sdb",
"Ebs": {
"Status": "attached",
"VolumeId": "vol-456",
}
}
],
"Tags": [
{
"Value": "Server01",
"Key": "Name"
}
],
"AmiLaunchIndex": 0
}
],
"OwnerId": "123456789"
},
{
"Instances": [
{
"InstanceId": "i-02",
"PrivateIpAddress": "10.2.2.2",
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sda",
"Ebs": {
"Status": "attached",
"VolumeId": "vol-789",
}
}
],
"Tags": [
{
"Value": "Server02",
"Key": "Name"
}
],
"AmiLaunchIndex": 0
}
],
"OwnerId": "123456789"
}
]
}
CodePudding user response:
result = []
for reservation in d["Reservations"]:
res = {}
instances = reservation["Instances"][0]
res['InstanceID'] = instances['InstanceId']
res['PrivateIpAddress'] = instances['PrivateIpAddress']
res['BlockDeviceMappings'] = []
for blockdevice in instances['BlockDeviceMappings']:
res['BlockDeviceMappings'].append(blockdevice['Ebs']['VolumeId'])
result.append(res)
You can then do:
final_df = pd.DataFrame(result)
to have the results displayed in a nice way.