Last active
September 6, 2020 14:09
-
-
Save benyitzhaki/f8908c8e1eb5606db5f28681d83c8f46 to your computer and use it in GitHub Desktop.
AWS Lambda to find all empty buckets in AWS S3 account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import boto3 | |
import array | |
s3 = boto3.resource('s3') | |
def lambda_handler(event, context): | |
empty_buckets = []; | |
for bucket in s3.buckets.all(): | |
is_empty = [] == [i for i in bucket.objects.limit(1).all()] | |
if is_empty: | |
empty_buckets.append(bucket.name) | |
return { | |
'body': json.dumps(empty_buckets) | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Sid": "VisualEditor2", | |
"Effect": "Allow", | |
"Action": [ | |
"s3:ListAllMyBuckets", | |
"s3:ListBucket", | |
"s3:GetObject" | |
], | |
"Resource": "*" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by https://ls3.io/posts/find_rm_empty_s3_buckets/ and https://medium.com/@alannewcomer/how-do-i-see-which-s3-buckets-are-empty-76cdbb67da86