python-3.xamazon-web-servicesboto3aws-ecr

Boto3 waiter for ECR deletion


Is there a way to wait until the AWS ECR repository force deletion is successful. Similar to waiters in cfn, ecs etc... there are waiters in ecr as well but those are only for image scan and lifecycle policy preview.

A scenario where we must force delete a ECR repo and wait until the deletion is successful, so we can proceed with next steps. If there are no waiters, is the only way to implement a custom one using describe_repositories?


Solution

  • Just used the describe repo operation with custom polling times and interval, posting the code below, so might help someone

    while num_checks <= 30:
        print(f'\n\tWaiting for ECR: {ecr_repo} to be deleted')
        try:
            response = ecr_client.describe_repositories(
                repositoryNames=[ecr_repo]
            )
            if response['ResponseMetadata']['HTTPStatusCode'] == 200:
                num_checks += 1
                if num_checks == 30:
                    raise Exception(f'Cannot force delete the ECR - {ecr_repo}')
                time.sleep(10)
                continue
            elif response['ResponseMetadata']['HTTPStatusCode'] != 200:
                raise Exception(f'Cannot force delete the ECR - {ecr_repo}')
    
        except ClientError as ce:
            if ce.response['Error']['Code'] == 'RepositoryNotFoundException':
                print(f'\tECR:{ecr_repo} DELETED SUCCESSFULLY\n')
                break
            else:
                raise Exception(f"\n{ce.response['Error']['Message']}\n")