Created
July 9, 2025 21:13
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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