Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save benyitzhaki/f8908c8e1eb5606db5f28681d83c8f46 to your computer and use it in GitHub Desktop.
Save benyitzhaki/f8908c8e1eb5606db5f28681d83c8f46 to your computer and use it in GitHub Desktop.
AWS Lambda to find all empty buckets in AWS S3 account
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)
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor2",
"Effect": "Allow",
"Action": [
"s3:ListAllMyBuckets",
"s3:ListBucket",
"s3:GetObject"
],
"Resource": "*"
}
]
}
@benyitzhaki
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment