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?
CodePudding user response:
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 = client_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'
.