agent: |
EgBjFdEoJfI8cbObKO5fDelete Detached EBS Volumes having low usage
Delete Detached EBS Volumes having low usage
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This task involves scanning through a list of EBS volumes and deleting them, provided they are not associated with any ec2 instances. Deleting these detached volumes that are no longer in use can help optimize storage resources and reduce unnecessary costs.
inputs
outputs
import boto3
creds = _get_creds(cred_label)['creds']
access_key = creds['username']
secret_key = creds['password']
def delete_detached_low_usage_volumes(volume_info):
"""
Delete detached low usage EBS volumes.
Args:
volume_info (dict): Dictionary containing the volume ID and its region.
Returns:
tuple: A tuple containing the count of deleted and skipped volumes.
"""
deleted_count, skipped_count = 0, 0
volume_id = volume_info['VolumeId']
region = volume_info['Region']
try:
ec2_client = boto3.client('ec2', aws_access_key_id=access_key, aws_secret_access_key=secret_key, region_name=region)
volume = ec2_client.describe_volumes(VolumeIds=[volume_id])['Volumes'][0]
if not volume['Attachments']:
ec2_client.delete_volume(VolumeId=volume_id)
deleted_count += 1
print(f"Deleted detached low usage EBS volume {volume_id} in region {region}")
else:
skipped_count += 1
print(f"Volume {volume_id} is attached to an EC2 instance. Skipping deletion in region {region}.")
except Exception as e:
print(f"Error in deleting volume {volume_id} in region {region}: {e}")
return deleted_count, skipped_count
# low_usage_volumes is a list of dictionaries received from the upstream task
total_deleted, total_skipped = 0, 0
for volume_info in low_usage_volumes:
deleted, skipped = delete_detached_low_usage_volumes(volume_info)
total_deleted += deleted
total_skipped += skipped
print(f"Summary: {total_deleted} detached low usage EBS volumes were deleted.")
print(f"{total_skipped} volumes were skipped (still attached).")
if total_deleted == 0:
print("No detached low usage EBS volumes were deleted.")
copied