Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto3: How to get newest docker image from ECR?

I want to get the newest Docker image from ECR using boto3. Currently I'm using describe_images method from the ecr client and I get a dictionary with imageDetails

import boto3

registry_name = 'some_registry_name_in_aws'

client = boto3.client('ecr')

response = client.describe_images(
    repositoryName=registry_name,
)

there's a solution for this using aws-cli, but documentation don't describe any --query argument that can be passed to describe_images. So, how can I get the newest docker image from ECR using boto3?

like image 507
Miguel Trejo Avatar asked Nov 02 '25 17:11

Miguel Trejo


1 Answers

TL;DR

You need to use a paginator on describe_images and a JMESPath expression

import boto3

registry_name = 'some_registry_name_in_aws'
jmespath_expression = 'sort_by(imageDetails, &to_string(imagePushedAt))[-1].imageTags'

client = boto3.client('ecr')

paginator = client.get_paginator('describe_images')

iterator = paginator.paginate(repositoryName=registry_name)
filter_iterator = iterator.search(jmespath_expression)
result = list(filter_iterator)[0]
result
>>>
'latest_image_tag'

Explanation

After reading the cli describe-images documentation find out that

describe-images is a paginated operation.

and boto3 can provide you a paginated operation on specific methods with get_paginator method.

However, if you try to apply the JMESPath expression 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]' directly, you'll get an error because the result from imagePushedAt is a datetime.datetime object and according to this answer

Boto3 Jmespath implementation does not support dates filtering

so, you need to cast imagePushedAt to string 'sort_by(imageDetails, &to_string(imagePushedAt))[-1].imageTags'.

like image 144
Miguel Trejo Avatar answered Nov 05 '25 09:11

Miguel Trejo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!