Giter VIP home page Giter VIP logo

ecr-cleanup-lambda's Introduction

Automated Image Cleanup for Amazon ECR

The Python script and Lambda function described here help clean up images in Amazon ECR. The script looks for images that are not used in running Amazon ECS tasks or Kubernetes, that can be deleted. You can configure the script to print the image list first to confirm deletions, specify a region, or specify a number of images to keep for potential rollbacks.

Authenticate with AWS

Configuring the AWS Command Line Interface.

Use virtualenv for Python execution

To prevent any problems with your system Python version conflicting with the application, we recommend using virtualenv.

Examples

  • Prints the images that are not used by running tasks and which are older than the last 100 versions, in all regions:

     python3 main.py
    
  • Deletes the images that are not used by running tasks and which are older than the last 100 versions, in all regions:

      python3 main.py -dryrun False
    
  • Deletes the images that are not used by running tasks and which are older than the last 20 versions (in each repository), in all regions:

      python3 main.py -dryrun False -imagestokeep 20
    
  • Deletes the images that are not used by running tasks and which are older than the last 20 versions (in each repository), in Oregon only:

      python3 main.py -dryrun False -imagestokeep 20 -region us-west-2
    
  • Deletes the images that are not used by running tasks and which are older than the last 20 versions (in each repository), in Oregon only, and ignore image tags that contains release or archive:

      python3 main.py -dryrun False -imagestokeep 20 -region us-west-2 -ignoretagsregex release|archive
    
  • Deletes the images that are not used by running tasks and which are older than the last 20 versions (in foo and bar repositories), in Oregon only, and ignore image tags that contains release or archive:

      python3 main.py -dryrun False -imagestokeep 20 -region us-west-2 -ignoretagsregex release|archive -repositories foo bar
    
  • Deletes the images that are not used by running tasks and which are older than the last 20 versions (in foo and bar repositories), in Oregon only, select only images with tag beginning with ^re but ignore image tags that contains release or review:

      python3 main.py -dryrun False -imagestokeep 20 -region us-west-2 -ignoretagsregex release|review -filtertagsregex ^re -repositories foo bar
    
  • Deletes the images that are not used by running tasks in the AWS accounts referenced by production and staging AWS profiles, or production and staging Kubernetes contexts and which are older than the last 20 versions (in foo and bar repositories), in Oregon only, select only images with tag beginning with ^re but ignore image tags that contains release or review:

      python3 main.py -dryrun False -imagestokeep 20 -region us-west-2 -ignoretagsregex release|review -filtertagsregex ^re -repositories foo bar -aws-profiles production,staging -k8s-contexts production,staging
    

Environment variables can be used instead of arguments

export REGION=us-west-2
export DRY_RUN=False
export IMAGES_TO_KEEP=20
export REPOSITORIES_FILTER='foo bar'
export IGNORE_TAGS_REGEX='release|review'
export FILTER_TAGS_REGEX='^re'
export AWS_PROFILES='production,staging'
export K8S_CONTEXTS='production,staging'

python3 main.py

Deploying to Lambda

Instead of running as a local Python script this can also be deployed to AWS Lambda using the included Cloudformation template.

Generate the Lambda package

  1. cd to the folder that contains main.py.
  2. Run the following command: pip install -r requirements.txt -t pwd``
  3. Compress the contents of folder (not the folder).

Upload the package to Lambda

  1. Run the following command: aws lambda create-function --function-name {NAME_OF_FUNCTION} --runtime python3.8 --role {ARN_NUMBER} --handler main.handler --timeout 15 --zip-file fileb://{ZIP_FILE_PATH}

Send the package update to Lambda

  1. Run the following command:

    aws lambda update-function-code --function-name {NAME_OF_FUNCTION} --zip-file fileb://{ZIP_FILE_PATH}

ecr-cleanup-lambda's People

Contributors

anshrma avatar cablespaghetti avatar duydnguyen07 avatar dyroffk avatar hposca avatar jackdanger avatar jpeddicord avatar kcorupe avatar kevingrandon avatar nfvs avatar talset avatar taraspos avatar witsoej avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

acumenix

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.