Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ s3transfer-0.1.9.dist-info
botocore-1.4.68.dist-info
docutils-0.12.dist-info
.idea
.venv
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Automated Image Cleanup for Amazon ECR
The Python script and Lambda function described here help clean up images in [Amazon ECR](https://aws.amazon.com/ecr). The script looks for images that are not used in running [Amazon ECS](https://aws.amazon.com/ecs) tasks 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.
The Python script and Lambda function described here help clean up images in [Amazon ECR](https://aws.amazon.com/ecr). The script looks for images that are not used in running [Amazon ECS](https://aws.amazon.com/ecs) tasks and [Amazon Lambda](https://aws.amazon.com/lambda) container images 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.](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)
Expand Down
9 changes: 9 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ def discover_delete_images(regionname):
if container['image'] not in running_containers:
running_containers.append(container['image'])

lambda_client = boto3.client('lambda', region_name=regionname)
listlambdas_paginator = lambda_client.get_paginator('list_functions')
for response_listlambdapaginator in listlambdas_paginator.paginate():
for function in response_listlambdapaginator['Functions']:
if function["PackageType"] == "Image":
function_config = lambda_client.get_function(FunctionName=function["FunctionName"])
if function_config["Code"]["ImageUri"] not in running_containers:
running_containers.append(function_config["Code"]["ImageUri"])

print("Images that are running:")
for image in running_containers:
print(image)
Expand Down