Browse Source

Add previous scripts rewritten in python

master
lavenderguitar 2 years ago
parent
commit
69b5be11c0
  1. 42
      delete_aws_amis.py
  2. 56
      delete_orphaned_snapshots.py

42
delete_aws_amis.py

@ -0,0 +1,42 @@
import boto3
from datetime import datetime
# Replace with your own values
region = 'us-east-1'
date_cutoff = '2022-01-01'
# Initialize the EC2 client
ec2 = boto3.client('ec2', region_name=region)
# Get a list of all the AMIs in the account
all_amis = ec2.describe_images(Owners=['self'])
# Loop through the list and print the names and IDs of any AMIs that were created before the cutoff date
amis_to_delete = []
for ami in all_amis['Images']:
created_date = datetime.strptime(ami['CreationDate'][:10], '%Y-%m-%d')
cutoff_date = datetime.strptime(date_cutoff, '%Y-%m-%d')
if created_date < cutoff_date:
print(f"AMI {ami['ImageId']} ({ami['Name']}) was created before the cutoff date.")
amis_to_delete.append(ami)
# Ask the user for verification before deleting the AMIs
if not amis_to_delete:
print("No AMIs to delete.")
else:
confirmation = input(f"\nAre you sure you want to delete {len(amis_to_delete)} AMIs? (y/n): ")
if confirmation.lower() == 'y':
delete_snapshots = input("Do you also want to delete the associated snapshots? (y/n): ")
# Loop through the list of AMIs to delete and delete the associated snapshots if specified
for ami in amis_to_delete:
print(f"Deleting AMI {ami['ImageId']} ({ami['Name']})")
snapshot_ids = [block_device['Ebs']['SnapshotId'] for block_device in ami['BlockDeviceMappings']]
ec2.deregister_image(ImageId=ami['ImageId'])
if delete_snapshots.lower() == 'y':
for snapshot_id in snapshot_ids:
print(f"Deleting snapshot {snapshot_id}")
ec2.delete_snapshot(SnapshotId=snapshot_id)
else:
print(f"Skipping deletion of {len(snapshot_ids)} associated snapshots.")
else:
print("Aborting deletion of AMIs.")

56
delete_orphaned_snapshots.py

@ -0,0 +1,56 @@
import boto3
def list_orphaned_snapshots():
# Connect to AWS using Boto3
ec2 = boto3.resource('ec2')
# Get a list of all snapshots
snapshots = ec2.snapshots.all()
# Get a list of all volumes
volumes = ec2.volumes.all()
# Create a set to hold the volume IDs
volume_ids = set()
# Add all volume IDs to the set
for volume in volumes:
volume_ids.add(volume.id)
# Create a list to hold the orphaned snapshots
orphaned_snapshots = []
# Check each snapshot to see if its volume ID is in the set
for snapshot in snapshots:
if snapshot.volume_id not in volume_ids:
orphaned_snapshots.append(snapshot.id)
# Return the list of orphaned snapshots
return orphaned_snapshots
def delete_snapshots(snapshot_ids):
# Connect to AWS using Boto3
ec2 = boto3.resource('ec2')
# Confirm with user before deleting snapshots
confirmation = input(f"Are you sure you want to delete {len(snapshot_ids)} snapshots? (y/n): ")
if confirmation.lower() != "y":
print("Aborted snapshot deletion.")
return
# Delete each snapshot in the list
for snapshot_id in snapshot_ids:
snapshot = ec2.Snapshot(snapshot_id)
snapshot.delete()
print(f"Deleted snapshot {snapshot_id}")
# Call the function to list orphaned snapshots
orphaned_snapshots = list_orphaned_snapshots()
# Print the list of orphaned snapshots
print("Orphaned Snapshots:")
for snapshot_id in orphaned_snapshots:
print(snapshot_id)
# Call the function to delete the orphaned snapshots
delete_snapshots(orphaned_snapshots)
Loading…
Cancel
Save