diff --git a/.gitignore b/.gitignore index be50c1e..135eacb 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ s3transfer-0.1.9.dist-info botocore-1.4.68.dist-info docutils-0.12.dist-info .idea +.venv \ No newline at end of file diff --git a/README.md b/README.md index 9d22478..6e1f556 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/main.py b/main.py index 458a1e4..7aea1fc 100644 --- a/main.py +++ b/main.py @@ -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)