Prodinit Software Solutions
LinkedinGithub
  • Prodinit's Engineering Blog
  • aws
    • Ways to delete AWS ECR images
    • Enable Cloudwatch Alarm and SNS Topic for AWS Billing Alert
    • A-Z of AWS VPC and other services - with Terraform
    • How Internet Works?
    • How to download/view code running in your lambda functions?
  • backend engineering
    • What is idempotency?
  • databases
    • Database Optimisation - Indexing vs Sharding with Postgres and Django ORM examples
  • devops
    • Docker Best Practices
    • Docker Networking - Bridge vs Host vs Overlay
    • A comparision between multistage build and singlestage build in Docker
    • Things to remember before building your first blue/green deployment in Kubernetes
    • How to export env variables in circleci? (You wont find this in circleci documentation)
  • frontend engineering
    • Host your static website with s3, CloudFront, Route53, and domain from godaddy in 4 easy steps
  • product management
    • You'll fail as a lead developer, here's why ...
  • python
    • Achieve Peak Performance in Python
    • Play with List of dictionaries in Python
    • How we develop a custom autoscaling metrics based on number of tasks in the queues?
  • Contact Us
    • Who are we?
    • Work with us.
Powered by GitBook
On this page
  • Delete them manually (eww 🤮)
  • Delete them using CLI (okay.. but..)
  • Running a periodic script in lambda
  • ECR Lifecycle policy (Recommended in production)
  • Enjoyed the blog? If so, you'll appreciate collaborating with the minds behind it as well.
  1. aws

Ways to delete AWS ECR images

Since you are here, I'm sure that you are figuring out ways to clean the old and not required ECR images. But, before that lets talk about the basics.

ECR is AWS managed elatic container registery is what everybody already aware of. It hosts images in a highly scalable and available architecture just like dockerhub/gitlab registery.

It’s important to delete untagged and old images to maintain hygiene, sainity and to release the used storage space.

The pricing of AWS ECR is “$0.10 per GB / month for data stored in private or public repositories” and you would not unnecessarily want to pay for the storage spaces. 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.

Now that you have understood that you should clear these old and untagged images out of your ECR repositories because simply you do not need them, let's start discussing different ways to delete them.

Delete them manually (eww 🤮)

As an engineer, this should never come to your mind as an ideal solution. This can/should only be used when you are the initial stages of your development and devops process. When you are doing a lot of hit and trial and creating images that doesn't meet the requirements.

The manual way is, you go to the AWS ECR console, select the images you dont need and delete them. Simple!

Delete them using CLI (okay.. but..)

Similar to manual effort, deleting images from CLI is not going to be a periodic job, and, you everytime have to come and decide what needs to be deleted manually.

Configure AWS CLI and you can use the following command to delete an untagged ECR image.

aws ecr batch-delete-image --repository-name test-ecr-repo --image-ids imageTag=test-web-img

Here we are deleting the image tagged as test-web-img residing in the repository test-ecr-repo

Running a periodic script in lambda

You are smart enough to configure a cloudwatch event to periodically trigger a lambda, which will run a script with a custom logic to delete unused/untagged/old images from ECR.

Now that you have the image ids in the required list of dict format. Simply pass the list of dict generated from the above function to the following code

ecr_client.batch_delete_image(
    repositoryName="test-ecr-repo",
    imageIds=get_imageids_to_delete()
)

Tada 🎉 ...

ECR Lifecycle policy (Recommended in production)

So, ECR images comes with a lifecycle policy and you can customise the rules of policy according to your requirement. If you are comfortable with terrform, just a few lines of code can give a ECR lifecycle policy rule. Below is a sample ECR lifecycle rule which removes untagged images older than 15 days.

resource "aws_ecr_repository" "ecr_repo" {
 ...
}

resource "aws_ecr_lifecycle_policy" "ecr_lifecycle" {
  repository = aws_ecr_repository.ecr_repo.name

  policy = <<EOF
{
  "rules": [
    {
      "rulePriority": 1,
      "description": "Remove untagged images and images pushed before 15 days.",
      "selection": {
        "tagStatus": "untagged",
        "countType": "sinceImagePushed",
        "countUnit": "days",
        "countNumber": 15
      },
      "action": {
        "type": "expire"
      }
    }
  ]
}
EOF
}

Creating an ECR lifecycle policy and removing older images based on specific parameters is a straightforward process. AWS offers comprehensive documentation and sample lifecycle policies.

Additionally, you can explore different policies for tagged images, like criteria matching based on the upload date of the image.

Tags

Enjoyed the blog? If so, you'll appreciate collaborating with the minds behind it as well.

PreviousProdinit's Engineering BlogNextEnable Cloudwatch Alarm and SNS Topic for AWS Billing Alert

Last updated 1 year ago

Returns untagged images older than 15 days
A pagination function to return images based on nextToken value

Written by -

Dishant Sethi
describe-ecr-images