Skip to content

Instantly share code, notes, and snippets.

@nathanieltalbot
Created November 4, 2019 19:34
Show Gist options
  • Save nathanieltalbot/784d49d2418c0fb8b656f1959d0f4624 to your computer and use it in GitHub Desktop.
Save nathanieltalbot/784d49d2418c0fb8b656f1959d0f4624 to your computer and use it in GitHub Desktop.
A simple Python function to check if an image tag exists in an ECR repo using boto3
# Checks if an image tag exists in the repo
def check_tag_exists(tag, repo):
ecr_client = boto3.client('ecr', region_name='us-east-1')
response = ecr_client.describe_images(repositoryName=repo, filter={'tagStatus': 'TAGGED'})
for i in response['imageDetails']:
if tag in i['imageTags']:
return True
return False
@ppi-agray
Copy link

I modified that code to include pagination, and to remove the hard coded region:

def check_tag_exists(tag, repo):
    ecr_client = boto3.client('ecr')

    paginator = ecr_client.get_paginator('describe_images')
    response_iterator = paginator.paginate(repositoryName=repo, filter={'tagStatus': 'TAGGED'})

    all_images = []
    for page in response_iterator:
        all_images.extend(page['imageDetails'])

    for image in all_images:
        if tag in image['imageTags']:
            return True
    return False

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment