whla
whla

Reputation: 811

Return only images ID's from ec2-describe-images command

As part of a script, I am trying to find the image ID's of already made images. When I run this command for instance:

ec2-describe-images --filter "description=$instance_ID" -O $O -W $W

It returns all information on the resulting images.

IMAGE ami-##dc64da ######/i-e45d81c4/141014 ##### available private x86_64 machine
aki-##### ebs paravirtual xen BLOCKDEVICEMAPPING EBS /dev/sda1 snap-##### 8 true standard
BLOCKDEVICEMAPPING EBS /dev/sdb snap-##### 100 false standard BLOCKDEVICEMAPPING EBS /dev/sdf
snap-##### 100 false standard

Is there anyway I can return only the image id ami-##dc64da?

Upvotes: 0

Views: 3059

Answers (3)

Romain
Romain

Reputation: 21958

It's possible to use, along with the --filter option, the built-in --query option to control the output and display only the ImageId.

$ aws ec2 describe-images --filter "Name=name,Values=$instance_ID" \                                                                                                                                                                           [±bench-branch ✓]
        --query 'Images[*].[ImageId]' --output text
# ami-xxxxxxx

Note: --output text permits to get the output as a text.

Upvotes: 1

Swapnil jaiswal
Swapnil jaiswal

Reputation: 310

I guess the answer is so late that it may be irrelevant, but anyways you may try the following :

aws ec2 describe-images --filters "Name=name,Values=i-imageid" | grep -i ami | awk '{ print  $9 }'

Upvotes: 0

Max
Max

Reputation: 8836

Use cut to get the second field (separated by spaces). Something like:

ec2-describe-images --filter "description=$instance_ID" -O $O -W $W |  cut -f2 -d ' '

Upvotes: 0

Related Questions