agent: |
lfKDvBDnuorGNlk0HPYZDelete AWS EBS Snapshots
Delete AWS EBS Snapshots
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.
inputs
outputs
import 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 region
copied