Reputation: 61
I am relatively new to Python and am trying to take the output of the command
aws ec2 describe-images --filters Name=name,Values=Nessus*BYOL*
and get the value of ImageId
. But when I try, I get the following error:
TypeError: 'int' object is not subscriptable
I've tried to convert it to a string, but I don't know if that's the correct way of doing this. I've also tried to regex it, but that didn't seem to work out neither.
At the end of the day, I need to assign the value of ImageId to a variable for later use.
Here's my code:
def queries():
describe_images = subprocess.call("aws ec2 describe-images --filters Name=name,Values=Nessus*BYOL*")
str(print(describe_images["ImageId"]))
Here's the output that you get when you run that command:
{
"Images": [
{
"Architecture": "x86_64",
"CreationDate": "2019-06-04T11:50:36.000Z",
"ImageId": "ami-0d700172aa0395099",
"ImageLocation": "aws-marketplace/Nessus 8.4.0 (master-193 1558031440.58) BYOL-8e783acf-0dfb-44dc-b080-415aad141bb2-ami-03eadadcd69ef2dbc.4",
"ImageType": "machine",
"Public": true,
"ProductCodes": [
{
"ProductCodeId": "8fn69npzmbzcs4blc4583jd0y",
"ProductCodeType": "marketplace"
}
],
"State": "available",
"BlockDeviceMappings": [
{
"DeviceName": "/dev/xvda",
"Ebs": {
"DeleteOnTermination": false,
"SnapshotId": "snap-07020d6ea4da33df4",
"VolumeSize": 8,
"VolumeType": "gp2",
"Encrypted": false
}
}
],
"EnaSupport": false,
"Hypervisor": "xen",
"ImageOwnerAlias": "aws-marketplace",
"Name": "Nessus 8.4.0 (master-193 1558031440.58) BYOL-8e783acf-0dfb-44dc-b080-415aad141bb2-ami-03eadadcd69ef2dbc.4",
"RootDeviceName": "/dev/xvda",
"RootDeviceType": "ebs",
"VirtualizationType": "hvm"
}
]
}
Upvotes: 1
Views: 42
Reputation: 59444
The subprocess.call
method will return the returncode
, not the command output. From the documentation:
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None)
Run the command described by args. Wait for command to complete, then return the
returncode
attribute.
Perhaps you wanted to use .check_output()
:
Run command with arguments and return its output.
Also note that the output will be a string. You should use json.loads()
to convert it to a Python dictionary first.
Upvotes: 1