Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CLI: ECR list-images, get newest

Using AWS CLI, and jq if needed, I'm trying to get the tag of the newest image in a particular repo.

Let's call the repo foo, and say the latest image is tagged bar. What query do I use to return bar?

I got as far as

aws ecr list-images --repository-name foo 

and then realized that the list-images documentation gives no reference to the date as a queryable field. Sticking the above in a terminal gives me keypairs with just the tag and digest, no date.

Is there still some way to get the "latest" image? Can I assume it'll always be the first, or the last in the returned output?

like image 574
Alex Avatar asked Apr 10 '17 19:04

Alex


People also ask

How do I view ECR images?

Open the Amazon ECR console at https://console.aws.amazon.com/ecr/repositories . From the navigation bar, choose the Region that contains the repository containing your image. In the navigation pane, choose Repositories. On the Repositories page, choose the repository to view.

What is image Digest in ECR?

An object with identifying information for an image in an Amazon ECR repository. imageDigest -> (string) The sha256 digest of the image manifest. imageTag -> (string) The tag used for the image.

Which command must be used to pull an image from ECR?

If you want to run a Docker image that is available in Amazon ECR, you can pull it to your local environment with the docker pull command.


2 Answers

You can use describe-images instead.

aws ecr describe-images --repository-name foo  

returns imagePushedAt which is a timestamp property which you can use to filter.

I dont have examples in my account to test with but something like following should work

aws ecr describe-images --repository-name foo \ --query 'sort_by(imageDetails,& imagePushedAt)[*]' 

If you want another flavor of using sort method, you can review this post

like image 132
Frederic Henri Avatar answered Sep 18 '22 17:09

Frederic Henri


To add to Frederic's answer, if you want the latest, you can use [-1]:

aws ecr describe-images --repository-name foo \ --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]' 

Assuming you are using a singular tag on your images... otherwise you might need to use imageTags[*] and do a little more work to grab the tag you want.

like image 30
Brett Green Avatar answered Sep 20 '22 17:09

Brett Green