2

I'm trying to get a list of the last 5 recently pushed images from AWS ECR by using ListImages. However, I observed that the ordering of the result is not what I expected.

For example:

I have an ECR repository some-service that has the following tags: 0.1.0, 0.2.0 ... 0.6.0, 0.6.1 and they have been being pushed in chronologically order.

When I tried to list the tags of the images, AWS ECR somehow gave me a strange ordering.

$ aws ecr list-images --repository-name some-service | jq '.imageIds[] | .imageTag'
"0.3.0"
"0.6.1"
"0.3.1"
"0.2.0"
"0.6.2"
"0.6.0"
"0.4.0"
"0.1.0"
"0.5.0"

It yielded consistently when I tried to use maxResults:

$ aws ecr list-images --max-results 5 --repository-name some-service | jq '.imageIds[] | .imageTag'
"0.3.0"
"0.6.1"
"0.3.1"
"0.2.0"
"0.6.2"

The ordering seemed to be consistent with DescribeImages. I tried to see which data the API was using as the sort key but I could not find any.

$ aws ecr describe-images --max-result 5 \
    --repository-name some-service \
    | jq '.imageDetails[] | {imageTags, imageDigest, imageSizeInBytes, imagePushedAt}'
{
  "imageTags": [
    "0.3.0"
  ],
  "imageDigest": "sha256:4497e8d...",
  "imageSizeInBytes": 190502899,
  "imagePushedAt": "2020-05-20T15:58:32+07:00"
}
{
  "imageTags": [
    "0.6.1"
  ],
  "imageDigest": "sha256:45a1be8...",
  "imageSizeInBytes": 218137213,
  "imagePushedAt": "2020-07-27T18:48:05+07:00"
}
{
  "imageTags": [
    "0.3.1"
  ],
  "imageDigest": "sha256:4b96a8ab...",
  "imageSizeInBytes": 190502957,
  "imagePushedAt": "2020-06-18T17:30:52+07:00"
}
{
  "imageTags": [
    "0.2.0"
  ],
  "imageDigest": "sha256:168e14fb6...",
  "imageSizeInBytes": 193764109,
  "imagePushedAt": "2020-05-11T18:57:26+07:00"
}
{
  "imageTags": [
    "0.6.2"
  ],
  "imageDigest": "sha256:2f24f...",
  "imageSizeInBytes": 218146252,
  "imagePushedAt": "2020-08-06T19:43:00+07:00"
}
  • Does anyone know internally how AWS ECR API sorts the images?
  • Is there a way to achieve what I want without using --query (because I'm actually using AWS Python SDK for the task).

UPDATE

I had implemented using Python and boto3. As the order was undeterministic, I had to use natsort 1 to sort the Docker Image tags.

from dataclasses import dataclass
from natsort import natsorted

@dataclass
class EcrRepository:
    name: str
    repository_arn: str


@dataclass
class EcrImage:
    repository: EcrRepository
    digest: str
    tags: List[str]
    
__ecr_client: "boto3_ecr.Client" = get_ecr_client()

def get_last_tagged_images(ecr_repository: EcrRepository) -> List[EcrImage]:

        list_image_response = __ecr_client.describe_images(
         repositoryName=ecr_repository.name, filter={"tagStatus": "TAGGED"},
        )

        return natsorted(
            [
                EcrImage(
                    repository=ecr_repository,
                    digest=img.get("imageDigest"),
                    tags=img.get("imageTags")
                )
                for img in list_image_response.get("imageDetails", [])
            ],
            key=lambda i: i.tags,
            reverse=True,
        )

Thanks a lot for your time!

Genzer

Genzer
  • 2,921
  • 2
  • 25
  • 38
  • Does this answer your question? [AWS CLI: ECR list-images, get newest](https://stackoverflow.com/questions/43331418/aws-cli-ecr-list-images-get-newest) – Amit Baranes Aug 11 '20 at 12:48
  • Hi @Amit Baranes, that was the first question I checked. Too bad, it was not the answer I'm looking for. – Genzer Aug 15 '20 at 07:00
  • @Genzer Can you share your AWS Python SDK so that we can have a look? You've just shown us AWS CLI code. – Bernardo Duarte May 05 '21 at 19:10
  • Hi @Bernardo-Duarte, I posted a snippet of the Python code that I did in my project. I also posted a question to AWS Forum in the same day but there was no answer until now. – Genzer May 07 '21 at 15:13

0 Answers0