我有以下JSON数据,这是AWS的缩短版本"cli描述ec2数据"输出。
我需要使用Python,以便我可以查询InstanceId值并获得其所有VolumeId值(可能有多个条目)及其PrivateIpAddress值。
输出看起来像这样:
i-01,10.1.1.1,vol-123,vol-456
i-02,10.2.2.2,vol-789
我是Python的新手,似乎找不到如何做到这一点的答案。有人能帮忙吗?
{
"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"
}
]
}
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)
你可以这样做:
final_df = pd.DataFrame(result)
让结果以一种好的方式显示。