[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.
/ ecr-gc Public archive

AWS Lambda function to clean up untagged images stored in ECR

License

Notifications You must be signed in to change notification settings

dtan4/ecr-gc

Repository files navigation

ecr-gc

AWS Lambda function / Docker image to clean up untagged images stored in ECR.

Why

The default maxinum number of images per one ECR repository is 1000. In addition, users are changed with storage pricing: $0.10 per GB-month.

If you build new image and push to ECR frequently, this repository capacity will be filled up soon.

By the way, most images which are untagged (= old) image may be no longer needed. We have no reason to retain unnecessary image for a long time.

Why don't you delete unnecessary images to save cost?

Install

IAM Role

These 3 operation must be authorized:

  • ecr:BatchDeleteImage
  • ecr:DescribeRepositories
  • ecr:ListImages

You can deploy this function as a part of Apex project, standalone Lambda function or Kubernetes CronJob.

1. Apex project

Add ecr-gc to your Apex project:

$ git submodule add https://github.com/dtan4/ecr-gc.git functions/ecr-gc

Deploy it:

$ apex deploy ecr-gc

Try it:

$ cp functions/ecr-gc/event.json.sample functions/ecr-gc/event.json
$ vim functions/ecr-gc/event.json
$ cat functions/ecr-gc/event.json
{
  "strategy": "untagged",
  "repositories": [
    "reponame"
  ]
}
$ apex invoke ecr-gc < functions/ecr-gc/event.json

If repositories is empty, ALL repositories will be cleaned up.

$ cat functions/ecr-gc/event.json
{
  "strategy": "untagged"
}
$ apex invoke ecr-gc < functions/ecr-gc/event.json

2. Standalone

Build .zip package:

$ npm install
$ npm run dist

Upload dist/ecr-gc.zip via Management Console or awscli.

3. Kubernetes CronJob

Set these Secrets:

name key description
dotenv AWS_ACCESS_KEY_ID AWS access key ID
dotenv AWS_SECRET_ACCESS_KEY AWS secret access key
dotenv AWS_REGION AWS region
dotenv GC_STRATEGY GC strategy (only untagged is supported)
dotenv REPOSITORIES comma-separated target ECR repositories (if empty, all repositories will be GCed)

deploy CronJob resource:

kubectl create -f kubernetes/cronjob.yaml [-n NAMESPACE]

ecr-gc Job will be invoked at 0:30 GMT in default.

License

MIT License

About

AWS Lambda function to clean up untagged images stored in ECR

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published