Sign in
agent:

Filter Out Unused AWS S3 Buckets based on threshold days, empty bucket and website configuration

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

This task identifies unused AWS S3 buckets by checking three main criteria: the bucket's last modification date exceeds a specified threshold, it contains no objects, and it is not configured as a website. This helps in managing storage efficiently by pinpointing potentially redundant buckets, which can reduce costs and simplify cloud infrastructure management.

import boto3 from datetime import datetime, timedelta, timezone from botocore.exceptions import ClientError creds = _get_creds(cred_label)['creds'] access_key = creds['username'] secret_key = creds['password'] # Initialize S3 client def get_s3_client(): return boto3.client('s3', aws_access_key_id=access_key, aws_secret_access_key=secret_key) def list_all_buckets(s3_client): try: buckets = s3_client.list_buckets() return buckets['Buckets'] except Exception as e: print(f"Failed to list S3 buckets: {e}") return [] def get_last_modified_object(s3_client, bucket_name): try: paginator = s3_client.get_paginator('list_objects_v2') page_iterator = paginator.paginate(Bucket=bucket_name) last_modified = None for page in page_iterator: if 'Contents' in page: for obj in page['Contents']: if last_modified is None or obj['LastModified'] > last_modified: last_modified = obj['LastModified'] return last_modified except Exception as e: print(f"Error accessing objects in bucket {bucket_name}: {e}") return None def is_bucket_empty(s3_client, bucket_name): response = s3_client.list_objects_v2(Bucket=bucket_name) return 'Contents' not in response def is_bucket_website(s3_client, bucket_name): try: s3_client.get_bucket_website(Bucket=bucket_name) return True except ClientError as e: if e.response['Error']['Code'] == 'NoSuchWebsiteConfiguration': return False raise # Rethrow the exception if it's not the expected "NoSuchWebsiteConfiguration" def filter_unused_buckets(s3_client, days_threshold): unused_buckets = [] current_time = datetime.now(timezone.utc) threshold_time = current_time - timedelta(days=days_threshold) buckets = list_all_buckets(s3_client) for bucket in buckets: bucket_name = bucket['Name'] if is_bucket_empty(s3_client, bucket_name) and not is_bucket_website(s3_client, bucket_name): last_modified = get_last_modified_object(s3_client, bucket_name) if last_modified is None or last_modified < threshold_time: unused_buckets.append(bucket_name) return unused_buckets # Main Execution s3_client = get_s3_client() #last_accessed_threshold_days = 90 # Define the threshold days unused_buckets = filter_unused_buckets(s3_client, last_accessed_threshold_days) #print(unused_buckets) # for debugging if unused_buckets: print(f"Unused buckets (no modifications for at least {last_accessed_threshold_days} days):") for bucket in unused_buckets: print(bucket) else: print("No unused buckets found based on the specified threshold.")
copied