Skip to content

Instantly share code, notes, and snippets.

@michalc
Created December 10, 2024 10:47
Show Gist options
  • Save michalc/93b7d9559e0bd872c3c818ddd4d51641 to your computer and use it in GitHub Desktop.
Save michalc/93b7d9559e0bd872c3c818ddd4d51641 to your computer and use it in GitHub Desktop.
Add a "--prod" tag suffix to existing ECR images
from pprint import pprint
import boto3
repository_name = 'REPLACE_ME'
client = boto3.client('ecr', region_name='eu-west-2')
image_details = (
image_detail
for page in client.get_paginator('describe_images').paginate(
repositoryName=repository_name,
)
for image_detail in page['imageDetails']
if (
not any('--prod' in tag for tag in image_detail['imageTags'])
and any('--' not in tag for tag in image_detail['imageTags'])
)
)
for image_detail in image_details:
basename_tag = sorted(image_detail['imageTags'], key=lambda tag: len(tag))[0]
manifest = client.batch_get_image(
repositoryName=repository_name, imageIds=[{"imageTag": basename_tag}]
)["images"][0]["imageManifest"]
new_tag = f"{basename_tag}--prod"
print("Adding", new_tag, "to", basename_tag)
try:
client.put_image(repositoryName=repository_name, imageTag=new_tag, imageManifest=manifest)
except client.exceptions.ImageAlreadyExistsException:
# Swallow the exception to support idempotency in the case of
# duplicated submissions
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment