I have written code to get data from an AWS API. I was able to parse the JSON data from the API. I'm filtering out only the running instances through the API and extract its data. The output is a JSON response.
Im trying to :
- retrieve
corecountandThreadsPerCorevalues of the running instances, - later i will have to multiply
corecountxThreadsPerCore=vcpusof the respective instances. - and summing up all
vcpusof the running instances.
How can I write code that does this ?
I read some tutorials, so I understand that I need to use [] to access elements of the nested lists and dictionaries; and I tried retrieving values using it for a single set of data (only for INSTANCE-1) but I can't figure out exactly how it works in my case where I may have 'n' of set of datas ( INSTANCE 1, 2, 3, .....) with specific values needs to be obtained. Hence here I have implemented for loop and again the work gets complex when i had to do the arithmetic operations.
here is the JSON output structure which contains 3 sets of data (for 3 running instances) in this example but might differ based on scenarios:
{
"Reservations": [
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-123",
"InstanceId": "INSTANCE-1",
"InstanceType": "t2.micro",
"KeyName": "MyKeyPair",
"State": {
"Code": 16,
"Name": "running"
},
"StateTransitionReason": "",
"CpuOptions": {
"CoreCount": 4,
"ThreadsPerCore": 2
},
],
"OwnerId": "123456789012"
"ReservationId": "r-02a3f596d91211712",
},
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-123",
"InstanceId": "INSATNCE-2",
"InstanceType": "t2.micro",
"KeyName": "MyKeyPair",
"State": {
"Code": 16,
"Name": "running"
},
"StateTransitionReason": "",
"CpuOptions": {
"CoreCount": 8,
"ThreadsPerCore": 2
},
],
"OwnerId": "123456789012"
"ReservationId": "r-02a3f596d91211712",
},
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-123",
"InstanceId": "INSTANCE-3",
"InstanceType": "t2.micro",
"KeyName": "MyKeyPair",
"State": {
"Code": 16,
"Name": "running"
},
"StateTransitionReason": "",
"CpuOptions": {
"CoreCount": 4,
"ThreadsPerCore": 2
},
],
"OwnerId": "123456789012"
"ReservationId": "r-02a3f596d91211712",
}
]
}
My code to retrieve CpuOptions : CoreCount and ThreadsPerCore values is :
from shell import run_command_str
import json
cmd = "aws ec2 describe-instances --output json --filters Name=instance-state-name,Values=running"
output = run_command_str(command=cmd, shell_command=True)
status = json.loads(output)
corecount = {}
for reservation in status1["Reservations"]:
for instance in reservation["Instances"]:
corecount[instance["InstanceId"]] = instance["CpuOptions"]["CoreCount"]
print(corecount)
threadspercore = {}
for reservation in status1["Reservations"]:
for instance in reservation["Instances"]:
threadspercore[instance["InstanceId"]] = instance["CpuOptions"]["ThreadsPerCore"]
print(threadspercore)
The output for this code:
{'INSTANCE-1': 4, 'INSTANCE-2': 8, 'INSTANCE-3': 4} ###corecount values
{'INSTANCE-1': 2, 'INSTANCE-2': 2, 'INSTANCE-3': 2} ###Threadpercore values