Sign in
agent:

Filter Out AWS Unused S3 Buckets

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

This task filters out unused AWS S3 buckets involves identifying buckets that have not been accessed or modified within a set period, such as 90 or 180 days. Once identified, these buckets can be reviewed for important data, then archived, transferred to cost-effective storage, or deleted.

import boto3 from datetime import datetime, timedelta, timezone 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 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: 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 #last_accessed_threshold_days = 90 s3_client = get_s3_client() 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