Amazon ECR is integrated with Amazon Elastic Kubernetes Service (Amazon EKS), Amazon Elastic Container Service (Amazon ECS), and AWS Lambda, simplifying your development to production workflow.
Amazon ECR hosts the images in a highly scalable and available architecture, allowing you to deploy containers reliably for your applications. It’s important to delete untagged and old images to maintain hygiene.
Today, applications run as micro-service. The term micro-service is nothing but a container that packages all the code and its dependencies so the application can run quickly and reliably in any computing environment. Due to their portability, small size, and convenience, containers are becoming a method of choice for shipping modern applications.
Containers are designed from a read-only template called an image. These images need to be stored somewhere so they can be retrieved by any machine authorized to use them.
That’s where a container registry comes in. Not long ago, folks used DockerHub to store these images and artifacts. But, if you are using AWS cloud services, I am sure you are already using AWS ECR, which is an alternative to DockerHub.
AWS ECR is a fully managed container registry that provides high-performance hosting, allowing you to deploy application images and artifacts in the form of public and private repositories.
Every day, multiple AWS-hosted applications push and pull millions of images/application artifacts into/out of specific ECR repositories.
In this article, we will discuss how to clear old and obsolete AWS ECR and keep ECR repositories clean.
The Need: Delete Untagged and Old Images Now!
The primary reason for cleaning ECR repositories is development hygiene. At any time, no one would want to keep images older than ten deployments in their ECRs. It is also because rollbacks frequently happen in the industry, but a rollback that reverts the change from 5 artifacts earlier is rare.
In simpler terms, any image/artifacts more than five deployments old are useless. It is subject to change on your organization’s strategy report, but we wouldn’t recommend it as a best practice.
Across the industry, tagging is used to specify the most stable latest or last five latest images. As a part of the software development life cycle, the images are generated rapidly, and these tags are replaced with new images, leaving the older images untagged and useless.
In situations like this, where images/artifacts are large, it will also add storage charges on ECR. The pricing of AWS ECR is “$0.10 per GB / month for data stored in private or public repositories”.
This price might look small to you, but as they say, drops make up the ocean. All these images, if put in store for a longer period, will add higher bills to your AWS invoices.
The suggestion is to clear these old and untagged images out of your ECR repositories because you do not need them! Simple! Why keep it and pay for it?
Deleting AWS ECR Images Manually
Method 1: The GUI Way!
Step 1: Log in to the Amazon Web Services account and head to the repository you want to clear.
Step 2: Here, you can see that the repository has the latest tag to specify the most stable version. The other tags that you see can be called untagged. To delete, we just need to select the image and click delete.
Step 3: Confirm to Delete
Method 2: The CLI Way!
To delete an image using the CLI, you will need all AWS IAM access keys configured on your machine and the required IAM permission to give you access to the repositories.
Here we are doing something similar to what we did in the GUI. We will delete the image tagged as custom-image-6 residing in the repository test-ecr-policy.
Method 3: The Scripting Way!
The prerequisite for this method is having an AWS Access key configured in the machine you are running on.
Script to delete untagged images.
client = boto3.client('ecr')
response = client.list_images(repositoryName='test-ecr-policy')
untaggedImageList = [image for image in response['imageIds'] if image['imageTag'] == 'custom-build-4']
response2 = client.batch_delete_image(repositoryName='aws-test-ecrpolicy', imageIds=untaggedImageList)
The response would give you the list of deleted image IDs, along with a failure if there were any.
Scheduling Method to Delete ECR Images
If you are a DevOps engineer or regularly manage AWS ECR, you will already know the pains of deleting these images manually.
Running the script/command does make things easier, but we are sure you would have wished for something that would automatically delete these images on their own without you having to worry about them.
Good News, AWS ECR offers a Lifecycle Policy for your images, which you can set to delete these images in a timely or scheduled manner. Let’s see how to do it.
Method 1: The GUI Way!
Step 1: Head over to the repository where you want to set the Lifecycle policy. On the left panel, you can see the Lifecycle Policy. You can click on it to get started.
Step 2: You can click on it and create your first rule.
Step 3: ECR allows you to delete images on two conditions, one being if your images get specified days old or if they are tagged/untagged, and you want to retain them only for, say, X number of days.
Let’s see how it is done. Now you can set whether you want to delete untagged images if they are one day or older or if the image count of untagged images exceeds one.
Choose according to your use case. Don’t forget; you can increase these numbers to the number of your choice. Save to trigger the Lifecycle Rule.
Method 2: The CLI Way!
The AWS ECR CLI command to set lifecycle policy is put-lifecycle-policy.
Let’s see how to do it. For this, you must create a JSON file listing the policy conditions. You can name it policy.json or any name of your choice.
But before that, let’s look at the lifecycle policy elements.
rulePriority (Type: integer, Required: yes):
Lower-to-higher rule order. Lifecycle policy rules with priority one are applied first, then 2, etc. Lifecycle policy rules must each have a unique rule value.
Policy rules don’t need consecutive values. Any-tagged rules must have the highest rulePriority and be reviewed last.
description (Type: string, Required: no):
Explains what a rule in a lifecycle policy is for.
tagStatus (Type: string, Required: yes):
It checks if the added lifecycle policy rule specifies an image tag. Tagged, untagged, or any is OK. If none is specified, all images are evaluated. Tagged requires a tagPrefixList value. Untagged requires omitting tagPrefixList.
tagPrefixList (Type: list[string], Required: yes, only if tagStatus is set to tagged):
If “tagStatus” is “tagged,” your lifecycle policy requires a comma-separated list of image tag prefixes.
Using the tag prefix prod, you can specify all images marked prod, prod1, prod2, etc. Multiple tags pick only images with all tags.
countType (Type: string, Required: yes):
Specify countNumber if countType is imageCountMoreThan to limit the number of images in your repository.
Specify countUnit and countNumber if countType is sinceImagePushed to limit the repository’s images.
countUnit (Type: string, Required: yes, only if countType is set to sinceImagePushed):
Only specify a count unit when countType is sinceImagePushed; otherwise, an error occurs.
countNumber (Type: integer, Required: yes):
Positive integers only (0 is not an accepted value). If countType is imageCountMoreThan, the value is the maximum number of photographs to keep. Using sinceImagePushed as the countType determines the maximum image age.
type (Type: string, Required: yes):
Choose a type of action. The value that can be used is “expire.”