agent: |
cE2VyVuRge1jnahfJtVuFilter Out Old AWS EBS Snapshots
Filter Out Old AWS EBS Snapshots
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This task identifies old Amazon Elastic Block Store (EBS) snapshots. By setting an age threshold, it scans across specified AWS regions, highlighting snapshots that exceed the set duration. This facilitates better management, paving the way for timely deletions and efficient storage utilization.
inputs
outputs
import boto3
from botocore.exceptions import ClientError
from datetime import datetime, timedelta
creds = _get_creds(cred_label)['creds']
access_key = creds['username']
secret_key = creds['password']
def find_old_snapshots(ec2_client, days_old, region):
"""
Find EBS snapshots in a specified AWS region that are older than a given number of days.
Args:
ec2_client (boto3.client): Boto3 EC2 client object.
days_old (int): The age in days to consider an EBS snapshot as old.
region (str): The AWS region to search for old snapshots.
Returns:
list[str]: List of old snapshot IDs. Returns None if there's an error.
"""
old_snapshots = [] # Initialize an empty list to store the IDs of old snapshots
try:
# Fetch all snapshots owned by the current AWS account
snapshots = ec2_client.describe_snapshots(OwnerIds=['self'])['Snapshots']
# Calculate the cutoff date for old snapshots, removing timezone information to make it "naive"
cutoff_date = datetime.now().replace(tzinfo=None) - timedelta(days=days_old)
# Loop through each snapshot to check its age
for snapshot in snapshots:
# Remove timezone information from the snapshot's start time to make it "naive"
snapshot_time_naive = snapshot['StartTime'].replace(tzinfo=None)
# Compare snapshot's start time with the cutoff date
if snapshot_time_naive < cutoff_date:
old_snapshots.append(snapshot['SnapshotId']) # Append old snapshot IDs to the list
return old_snapshots # Return the list of old snapshot IDs
except ClientError as e:
print(f"A ClientError occurred in region {region}: {e}") # Handle any ClientErrors
return None
except Exception as e:
print(f"An unknown error occurred in region {region}: {e}") # Handle any general exceptions
return None
# List of AWS regions to check for old snapshots
#regions_to_check = ['us-east-1', 'us-east-2'] #, 'us-west-2']
# Age in days to consider an EBS snapshot as old
#days_old = 5
# Initialize an empty dictionary to store the snapshot IDs by region
snapshots_by_region = {}
# Initialize a list to store regions where no old snapshots were found
regions_without_snapshots = []
# Loop through each AWS region to find old snapshots
for region in regions:
#print(f"Checking region {region}...")
ec2_client = boto3.client('ec2', aws_access_key_id=access_key,aws_secret_access_key=secret_key,region_name=region) # Initialize EC2 client for the region
old_snapshots = find_old_snapshots(ec2_client, int(days_old), region) # Find old snapshots in the region
# If old snapshots are found, add them to the dictionary
if old_snapshots:
snapshots_by_region[region] = old_snapshots
else:
regions_without_snapshots.append(region)
# Print the resulting dictionary
print("\nSummary of old snapshots by region:")
for region, snapshot_ids in snapshots_by_region.items():
print(f"{region}: {snapshot_ids}")
# Print regions without old snapshots
if regions_without_snapshots:
print(f"\nNo old snapshots found in the following regions: {', '.join(regions_without_snapshots)}")
context.skip_sub_tasks=True
copied
- 1lfKDvBDnuorGNlk0HPYZDelete AWS EBS Snapshots
1
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.This task removes specified Amazon Elastic Block Store (EBS) snapshots. Designed to streamline storage management, this procedure efficiently purges selected snapshots across designated AWS regions, ensuring optimal resource utilization and reducing unnecessary storage costs.
inputsoutputsimport boto3 from botocore.exceptions import ClientError creds = _get_creds(cred_label)['creds'] access_key = creds['username'] secret_key = creds['password'] # Function to delete a list of EBS snapshots given their IDs def delete_snapshots(ec2_client, snapshot_ids, region): """ Delete a list of specified EBS snapshots in a given AWS region. Args: ec2_client (boto3.client): Boto3 EC2 client object. snapshot_ids (list[str]): List of EBS snapshot IDs to be deleted. region (str): The AWS region where the snapshots are located. Returns: None: This function does not return any value. """ for snapshot_id in snapshot_ids: try: # Delete the snapshot ec2_client.delete_snapshot(SnapshotId=snapshot_id) print(f"Deleted snapshot {snapshot_id} in region {region}") # Confirm deletion except ClientError as e: print(f"Could not delete snapshot {snapshot_id} in region {region}: {e}") # Handle any ClientErrors ''' #Example structure of snapshots_by_region # Dictionary mapping AWS regions to their respective old snapshots snapshots_by_region = { 'us-east-1': ['snap-04cbc2182c8f5e1ed', 'snap-0004bbdd1e7b0d35c'], 'us-west-2': [] # Just as an example, no snapshots listed for us-west-2 } ''' # Loop through each AWS region in the dictionary to delete old snapshots for region, old_snapshots in snapshots_by_region.items(): print(f"Checking region {region}...") ec2_client = boto3.client('ec2',aws_access_key_id=access_key,aws_secret_access_key=secret_key, region_name=region) # Initialize EC2 client for the region # Delete old snapshots if any are found for the current region if old_snapshots: print(f"Found {len(old_snapshots)} old snapshots in {region}. Deleting them...") delete_snapshots(ec2_client, old_snapshots, region) else: print(f"No old snapshots found in {region}.") # Confirm if no old snapshots are found in the current regioncopied1