'How can I combine AWS EC2 describe-instances with describe-image?

I have 2 API calls, the second uses AMI-ID from the first

aws ec2 describe-instances \
    --query "Reservations[*].{ \
        OWNERID:OwnerId, \
        IP:Instances[0].PublicIpAddress, \
        ImageID:Instances[0].ImageId, \
        AZ:Instances[0].Placement.AvailabilityZone, \
        STATE:Instances[0].State.Name, \
        KEY:Instances[0].KeyName, \
        VPC:Instances[0].VpcId, \
        ID:Instances[0].InstanceId, \
        INSTANCETYPE:Instances[0].InstanceType \
        }" --output text

and

aws ec2 describe-images \
    --image-ids ami-xxx\
    --query "Images[*].{ImageName:Name, AWSDescription:Description}" \
    --output text

I am trying to have individual instances with the image name and description from their respective images on the same lines, for a single table with all the columns from both calls.

Is it possible to do it in the same command (i strongly doubt it)? If not, how would I go about in a shell script? Do I have to grep the AMI-ID from the first call and use it? But that first call returns a whole table, how can i then "append" the extra columns next to the first ones and not under?



Solution 1:[1]

JSON output and JQ is one method for manipulating awscli data

The following approach selects fields from both awscli calls and combines them into a CSV file. For larger tasks, use JQ to merge the JSON outputs and the @csv filter.

echo "InstanceId,InstanceName,ImageId,ImageName,ImageDescription" > ec2-instances.csv

instances_data=$(echo $instances | jq -r '.Reservations[].Instances[] | [.InstanceId, (.Tags[] | select(.Key=="Name") | .Value), .ImageId]  | join(",")')

for instance in $instances_data
do
    image_id=$(echo $instance | cut -f3 -d',')
    image_data=$(aws ec2 describe-images --image-ids $image_id | jq -r '.Images[] | [.Name, .Description] | join(",")')
    echo "${instance},${image_data}" >> ec2-instances.csv
done 

ec2-instances.csv:

InstanceId,InstanceName,ImageId,ImageName,ImageDescription
i-abc123,nat-instance,ami-e7ee9edd,amzn-ami-vpc-nat-hvm-2015.03.0.x86_64-gp2,Amazon Linux AMI VPC NAT x86_64 HVM GP2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 naaman