Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (7k points)

I am trying to describe AMI from AWS CLI by ImageId, and I used below code.

aws ec2 describe-images --profile xxxxxxxxxx \

--filter Name=tag:Name,Values=Backup*some-string* \

--query "Images[*].[Tags[?Key=='Name'].Value[]|[0],ImageId]" 

output :

[

    [

        "Backup-20191215T112530Z-utc-some-string",

        "ami-004"

    ],

    [

        "Backup-20191219T714631Z-utc-some-string",

        "ami-002"

    ],

    [

        "Backup-20191208T145534Z-utc-some-string",

        "ami-001"

    ],

    [

        "Backup-20191222T157530Z-utc-some-string",

        "ami-003"

    ],

    [

        "Backup-20191221T134530Z-utc-some-string",

        "ami-005"

    ]

]

I found sort_by method of JMESPath would be a solution but it is diffficult to understand.

1 Answer

0 votes
by (31.9k points)
edited by

Use the following code to achieve the same and (&) operator is need

aws ec2 describe-images --profile xxxxxxxxxx \

--filter Name=tag:Name,Values=Backup*some-string* \

--query "sort_by(Images[*].[Tags[?Key=='Name'].Value[]|[0],ImageId], &[0]"

or

aws ec2 describe-images --profile xxxxxxxxxx \

--filter Name=tag:Name,Values=Backup*some-string* \

--query "Images[*].[Tags[?Key=='Name'].Value[]|[0],ImageId] | sort_by(@, &[0])"

Want to Learn AWS, check out this AWS Course by Intellipaat. 

Related questions

0 votes
1 answer

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer
asked Dec 11, 2020 in AWS by Justin (7k points)
0 votes
1 answer

Browse Categories

...