Skip to content

Instantly share code, notes, and snippets.

@madhurprash
Created July 9, 2025 21:13
Show Gist options
  • Select an option

  • Save madhurprash/078a11e1afdf77f85cd431a54cdae6ae to your computer and use it in GitHub Desktop.

Select an option

Save madhurprash/078a11e1afdf77f85cd431a54cdae6ae to your computer and use it in GitHub Desktop.
This gist gets the latest image uri based on the registry id and the repo id. View an example below.
import boto3
from botocore.config import Config
def get_latest_image_uri(registry_id: str,
repository_name: str,
region: str = "us-east-1") -> str:
"""
Fetch the URI of the most recently pushed image in the given ECR repository.
"""
ecr = boto3.client("ecr", region_name=region, config=Config(retries={"max_attempts": 10}))
paginator = ecr.get_paginator("describe_images")
page_iterator = paginator.paginate(
registryId=registry_id,
repositoryName=repository_name,
filter={"tagStatus": "TAGGED"}
)
latest = None
for page in page_iterator:
for img in page["imageDetails"]:
# skip unpushed or untagged
if "imagePushedAt" not in img or not img.get("imageTags"):
continue
if latest is None or img["imagePushedAt"] > latest["imagePushedAt"]:
latest = img
if not latest:
raise RuntimeError(f"No tagged images found in {repository_name}")
# assume the first tag is the primary one
tag = latest["imageTags"][0]
return f"{registry_id}.dkr.ecr.{region}.amazonaws.com/{repository_name}:{tag}"
if __name__ == "__main__":
registry = "763104351884"
repo = "huggingface-pytorch-tgi-inference"
print("Latest image URI →", get_latest_image_uri(registry, repo))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment