Skip to content

Commit c8f413c

Browse files
committed
Full refactor
* find running digests also in k8s (also: `check_eks`, `check_ecs` flags) * add loads of logging (instead of print) * rename `ignore_tags_regex` to `protect_tags_regex` and address awslabs#34 * set ecs_client Config params (connect_timeout, read_timeout, max_attempts) * add `repo_name_regex` * add `protect_latest` flag * add `older_than` filter inspired by awslabs#18 * README.md updated
1 parent 8bc6e39 commit c8f413c

File tree

4 files changed

+759
-221
lines changed

4 files changed

+759
-221
lines changed

README.md

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The Python script and Lambda function described here help clean up images in [Am
77
## Use virtualenv for Python execution
88

99
To prevent any problems with your system Python version conflicting with the application, we recommend using virtualenv.
10+
This code was tested with Python 3.13, but would probably work with any Python >= 3.11.
1011

1112
Install Python:
1213
`pip install python 3`
@@ -28,7 +29,7 @@ Install virtualenv:
2829
## Upload the package to Lambda
2930

3031
1. Run the following command:
31-
`aws lambda create-function --function-name {NAME_OF_FUNCTION} --runtime python2.7
32+
`aws lambda create-function --function-name {NAME_OF_FUNCTION} --runtime python3.13
3233
--role {ARN_NUMBER} --handler main.handler --timeout 15
3334
--zip-file fileb://{ZIP_FILE_PATH}`
3435

@@ -47,19 +48,71 @@ Prints the images that are not used by running tasks and which are older than th
4748

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

50-
`python main.py dryrun False`
51+
`python main.py -no-dryrun`
5152

5253

5354
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:
5455

55-
`python main.py dryrun False –imagestokeep 20`
56+
`python main.py -no-dryrun –images_to_keep 20`
5657

5758

5859
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:
5960

60-
`python main.py dryrun False –imagestokeep 20 –region us-west-2`
61+
`python main.py -no-dryrun –images_to_keep 20 –region us-west-2`
6162

62-
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`:
6363

64-
`python main.py –dryrun False –imagestokeep 20 –region us-west-2 -ignoretagsregex release|archive`
64+
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`:
6565

66+
`python main.py -no-dryrun –images_to_keep 20 –region us-west-2 --protect_tags_regex release|archive`
67+
68+
69+
For full option list, please refer to the help, by running:
70+
71+
`python main.py -h`
72+
73+
````
74+
usage: main.py [-h] [-no-dryrun] [-region REGION]
75+
[-repo_name_regex REPO_NAME_REGEX]
76+
[-images_to_keep IMAGES_TO_KEEP] [-older_than OLDER_THAN]
77+
[-protect_tags_regex PROTECT_TAGS_REGEX] [-unprotect-latest]
78+
[-no-ecs] [-no-lambda] [-no-eks]
79+
[-connect_timeout CONNECT_TIMEOUT] [-read_timeout READ_TIMEOUT]
80+
[-max_attempts MAX_ATTEMPTS]
81+
82+
Deletes stale ECR images
83+
84+
options:
85+
-h, --help show this help message and exit
86+
-no-dryrun Don't just prints the repository to be deleted,
87+
actually delete them
88+
-region REGION ECR/ECS region
89+
-repo_name_regex REPO_NAME_REGEX
90+
Regex of repo names to search
91+
-images_to_keep IMAGES_TO_KEEP
92+
Number of image tags to keep
93+
-older_than OLDER_THAN
94+
Only delete images older than a specified amount of
95+
days
96+
-protect_tags_regex PROTECT_TAGS_REGEX
97+
Regex of tag names to protect (not delete)
98+
-unprotect-latest Allow deletion images with `latest` tag
99+
-no-ecs Don't search ECS for running images
100+
-no-lambda Don't search Lambda for running images
101+
-no-eks Don't search EKS for running images
102+
-connect_timeout CONNECT_TIMEOUT
103+
ECS connection timeout (in seconds)
104+
-read_timeout READ_TIMEOUT
105+
ECS read timeout (in seconds)
106+
-max_attempts MAX_ATTEMPTS
107+
ECS maximum number of attempts
108+
109+
Deletion logic: In each ECR repository that contains more than
110+
`images_to_keep` non running images (running images are images that currently
111+
deployed on a container), Iterate through a list of images, sorted from oldest
112+
to newest (image date: if an image was pulled - last pull date, otherwise push
113+
date), Mark images for deletion, until less then `images_to_keep` are left, or
114+
until iterated through the entire list. An image is marked for deletion if it
115+
is older than `older_than`; AND is not tagged `latest`, AND does not have has
116+
any tags that matches `protect_tags_regex`. Note that if not enough images are
117+
marked for deletion, more than `images_to_keep` may be left untouched.
118+
```

lambda-cloudformation.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Resources:
1414
Type: AWS::Serverless::Function
1515
Properties:
1616
Handler: main.handler
17-
Runtime: python2.7
17+
Runtime: python3.13
1818
Description: ECR Cleanup Lambda
1919
CodeUri: ./
2020
MemorySize: 128

0 commit comments

Comments
 (0)