I am attempting to write a Perl script that will run on a Centos 6 host. This script will run a shell command that queries our AWS interface like this:
my $json = `aws ec2 describe-instances`;
This query will return a JSON object but I belive my issue is that it is actually being stored as a string in $output and not actually in a JSON object.
I am trying the following code:
# Get each instance
my $json = `aws ec2 describe-instances`;
# Decode AWS json result
my $decoded = decode_json $json;
# Find reference type
print "Reference type: " . ref $decoded,"\n";
# Loop through each EC2 instance
print $decoded -> {Reservations} . "\n";
When I execute this code I get the following message:
Reference type: HASH
ARRAY(0xa774b0)
Can anyone help me with this? I have been googling and messing with this for over a whole day now :(
Below is an example of the string that is stored in $output although in the real output there would be multiple instances:
{
"Reservations": [
{
"OwnerId": "82XXXXXXX043",
"ReservationId": "r-0XXXXXXXXXXX",
"Groups": [],
"Instances": [
{
"Monitoring": {
"State": "XXXXX"
},
"PublicDnsName": "XXX",
"RootDeviceType": "XXXXXXX",
"State": {
"Code": XXX,
"Name": "stopped"
},
"EbsOptimized": XXXX,
"LaunchTime": "XXXXXXXXXX",
"PrivateIpAddress": "XXXXXXXXXX",
"ProductCodes": [],
"VpcId": "XXXXXXXXXX",
"StateTransitionReason": "UXXXXXXXXXX",
"InstanceId": "XXXXXXXXXX",
"ImageId": "XXXXXXXXXX",
"PrivateDnsName": "XXXXXXXXXX",
"KeyName": "XXXXXXXXXX",
"SecurityGroups": [
{
"GroupName": "XXXXXXXXXX",
"GroupId": "XXXXXXXXXX"
},
{
"GroupName": "XXXXXXXXXX",
"GroupId": "XXXXXXXXXX"
},
{
"GroupName": "XXXXXXXXXX",
"GroupId": "XXXXXXXXXX"
}
],
"ClientToken": "XXXXXXXXXX",
"SubnetId": "XXXXXXXXXX",
"InstanceType": "XXXXXXXXXX",
"NetworkInterfaces": [
{
"Status": "XXXXXXXXXX",
"MacAddress": "XXXXXXXXXX",
"SourceDestCheck": XXXXXXXXXX,
"VpcId": "XXXXXXXXXX",
"Description": "XXXXXXXXXX",
"NetworkInterfaceId": "XXXXXXXXXX",
"PrivateIpAddresses": [
{
"Primary": XXXXXXXXXX,
"PrivateIpAddress": "XXXXXXXXXX"
}
],
"Ipv6Addresses": [],
"Attachment": {
"Status": "XXXXXXXXXX",
"DeviceIndex": 01234,
"DeleteOnTermination": XXXXXXXXXX,
"AttachmentId": "XXXXXXXXXX",
"AttachTime": "XXXXXXXXXX"
},
"Groups": [
{
"GroupName": "XXXXXXXXXX",
"GroupId": "XXXXXXXXXX"
},
{
"GroupName": "XXXXXXXXXX",
"GroupId": "XXXXXXXXXX"
},
{
"GroupName": "XXXXXXXXXX",
"GroupId": "XXXXXXXXXX"
}
],
"SubnetId": "XXXXXXXXXX",
"OwnerId": "XXXXXXXXXX",
"PrivateIpAddress": "XXXXXXXXXX"
}
],
"SourceDestCheck": XXXXXXXXXX,
"Placement": {
"Tenancy": "XXXXXXXXXX",
"GroupName": "XXXXXXXXXX",
"AvailabilityZone": "XXXXXXXXXX"
},
"Hypervisor": "XXXXXXXXXX",
"BlockDeviceMappings": [
{
"DeviceName": "XXXXXXXXXX",
"Ebs": {
"Status": "XXXXXXXXXX",
"DeleteOnTermination": XXXXXXXXXX,
"VolumeId": "XXXXXXXXXX",
"AttachTime": "XXXXXXXXXX"
}
}
],
"Architecture": "XXXXXXXXXX",
"StateReason": {
"Message": "XXXXXXXXXX",
"Code": "XXXXXXXXXX"
},
"IamInstanceProfile": {
"Id": "XXXXXXXXXX",
"Arn": "XXXXXXXXXX"
},
"RootDeviceName": "XXXXXXXXXX",
"VirtualizationType": "XXXXXXXXXX",
"Tags": [
{
"Value": "XXXXXXXXXX",
"Key": "XXXXXXXXXX"
},
{
"Value": "XXXXXXXXXX",
"Key": "XXXXXXXXXX"
},
{
"Value": "XXXXXXXXXX",
"Key": "XXXXXXXXXX"
},
{
"Value": "XXXXXXXXXX",
"Key": "XXXXXXXXXX"
},
{
"Value": "XXXXXXXXXX",
"Key": "XXXXXXXXXX"
},
{
"Value": "XXXXXXXXXX",
"Key": "XXXXXXXXXX"
}
],
"AmiLaunchIndex": XXXXXXXXXX
}
]
}
]
}
$outputand not actually in a JSON object". What do you imagine a "JSON object" to be? JSON is JavaScript Object Notation and is, by definition, a string of text. It is the job of theJSONmodule to convert between a JSON string and the data structure that it represents.$output. Do you mean$json?decode_json, you turn the string of JSON into a Perl data structure. The hierarchy inside that structure maps exactly to JSON. In your code you say you Loop through each EC2 instance, but you don't loop. You print the value behind the keyReservations. That's an array reference, and if you print that, it stringifies toARRAY(0xdeadb33f). That's expected. If you want to turn this back into JSON, you need to tell Perl that.encode_jsonsounds reasonable.