@@ -7,6 +7,7 @@ The Python script and Lambda function described here help clean up images in [Am
7
7
## Use virtualenv for Python execution
8
8
9
9
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.
10
11
11
12
Install Python:
12
13
` pip install python 3 `
@@ -28,7 +29,7 @@ Install virtualenv:
28
29
## Upload the package to Lambda
29
30
30
31
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
32
33
--role {ARN_NUMBER} --handler main.handler --timeout 15
33
34
--zip-file fileb://{ZIP_FILE_PATH}`
34
35
@@ -47,19 +48,71 @@ Prints the images that are not used by running tasks and which are older than th
47
48
48
49
Deletes the images that are not used by running tasks and which are older than the last 100 versions, in all regions:
49
50
50
- ` python main.py – dryrun False `
51
+ ` python main.py -no- dryrun `
51
52
52
53
53
54
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:
54
55
55
- ` python main.py – dryrun False –imagestokeep 20 `
56
+ ` python main.py -no- dryrun –images_to_keep 20 `
56
57
57
58
58
59
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:
59
60
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 `
61
62
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 ` :
63
63
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 ` :
65
65
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
+ ```
0 commit comments