agent: |
flcqDvNamXsOTVclwnPaDelete AWS RDS Instance
Delete AWS RDS Instance
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This task terminates the AWS RDS database and deletes all associated data. Before deletion, users often create a final snapshot to preserve the database's current state, enabling future restoration if necessary. It's essential to ensure that the "Deletion Protection" feature, designed to prevent accidental deletions, is disabled before proceeding. Once deleted, the RDS instance is no longer operational, and associated costs cease. However, any retained backups or snapshots will persist and may incur storage charges until they too are deleted.
inputs
outputs
import boto3
creds = _get_creds(cred_label)['creds']
access_key = creds['username']
secret_key = creds['password']
def delete_rds_instance(instance_info):
"""
Delete an RDS instance after taking necessary precautions like disabling deletion protection
and creating a final snapshot.
Parameters:
- instance_info (dict): Dictionary containing InstanceID and Region.
"""
instance_id = instance_info['InstanceID']
region = instance_info['Region']
# Initialize the boto3 client for the Amazon Relational Database Service (RDS) in the specified region
rds = boto3.client('rds', aws_access_key_id=access_key,aws_secret_access_key=secret_key,region_name=region)
try:
instance_details = rds.describe_db_instances(DBInstanceIdentifier=instance_id)
if instance_details['DBInstances'][0].get('DeletionProtection', False):
rds.modify_db_instance(DBInstanceIdentifier=instance_id, DeletionProtection=False)
print(f"Deletion protection disabled for {instance_id}")
except rds.exceptions.DBInstanceNotFoundFault:
print(f"RDS instance {instance_id} not found.")
return
except Exception as e:
print(f"Error modifying RDS instance {instance_id}: {e}")
return
try:
snapshot_name = f"final-snapshot-{instance_id}"
rds.create_db_snapshot(DBInstanceIdentifier=instance_id, DBSnapshotIdentifier=snapshot_name)
print(f"Final snapshot creation initiated for {instance_id}")
waiter = rds.get_waiter('db_snapshot_completed')
waiter.wait(DBSnapshotIdentifier=snapshot_name)
print(f"Final snapshot {snapshot_name} created for {instance_id}")
except rds.exceptions.SnapshotQuotaExceededFault:
print(f"Snapshot quota exceeded for {instance_id}.")
return
except rds.exceptions.DBInstanceNotFoundFault:
print(f"RDS instance {instance_id} not found.")
return
except Exception as e:
print(f"Error creating snapshot for RDS instance {instance_id}: {e}")
return
try:
rds.delete_db_instance(DBInstanceIdentifier=instance_id, SkipFinalSnapshot=True)
print(f"RDS instance {instance_id} deletion initiated")
except rds.exceptions.DBInstanceNotFoundFault:
print(f"RDS instance {instance_id} not found.")
except Exception as e:
print(f"Error deleting RDS instance {instance_id}: {e}")
rds_instances_to_delete = low_cpu_rds # Make sure low_cpu_rds is a list of dictionaries with 'InstanceID' and 'Region' keys
# Check if the list is empty
if not rds_instances_to_delete:
print("No RDS instances provided for deletion.")
else:
# Loop through each RDS instance in the list and call the delete function
for instance_info in rds_instances_to_delete:
delete_rds_instance(instance_info)
copied