Created
December 13, 2022 10:33
-
-
Save markobrien1/578f0076f6f42c769d2d0dc2e7308be9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
""" | |
This is a script which will check for a tag with a key of `Delete`. The value | |
associated should be an integer which represents the amount of days after which | |
the resource should be deleted. | |
Currently The script checks for AMIs, EC2 instances, EBS volumes and Snapshots. | |
@author Mark O'Brien | |
""" | |
import datetime | |
import dateutil.parser | |
import botocore | |
import boto3 | |
def get_amis(ec2_client): | |
"""Get list of AMIs owned and return them to be processed""" | |
print("getting amis") | |
images = ec2_client.describe_images(Owners=["self"]) | |
return images | |
def get_volumes(ec2_client): | |
"""Get list of EBS Volumes and return them to be processed""" | |
print("getting volumes") | |
volumes = ec2_client.describe_volumes( | |
Filters=[{'Name': 'status', 'Values': ['available']}] | |
) | |
return volumes | |
def get_snapshots(ec2_client): | |
"""Get list of EBS Snapshots and return them to be processed""" | |
print("getting snaphots") | |
snapshots = ec2_client.describe_snapshots(OwnerIds=["self"]) | |
return snapshots | |
def get_instances(ec2_client): | |
"""Get list of EC2 instances and return them to be processed""" | |
print("Getting EC2 instances") | |
instances = ec2_client.describe_instances( | |
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}] | |
) | |
return instances['Reservations'] | |
def delete_old_amis(ec2_client): | |
"""Delete AMIs if they are marked past deletion date""" | |
images = check_tag(get_amis(ec2_client), "Images") | |
for item in images: | |
ami = item[0] | |
age = item[1] | |
ami_age = dateutil.parser.parse(ami["CreationDate"]) | |
old_age = datetime.datetime.today() - datetime.timedelta(days=int(age)) | |
if ami_age.replace(tzinfo=None) < old_age: | |
print(ami["ImageId"]) | |
try: | |
ec2_client.deregister_image(ImageId=ami["ImageId"]) | |
except botocore.exceptions.ClientError as error : | |
print("Image " + ami["ImageId"] + " cannot be deleted.") | |
print(error) | |
def delete_old_volumes(ec2_client): | |
"""Delete EBS Volumes if they are marked past deletion date""" | |
volumes = check_tag(get_volumes(ec2_client), "Volumes") | |
for item in volumes: | |
volume = item[0] | |
age = item[1] | |
volume_age = volume["CreateTime"] | |
old_age = datetime.datetime.today() - datetime.timedelta(days=int(age)) | |
if volume_age.replace(tzinfo=None) < old_age: | |
print(volume["VolumeId"]) | |
try: | |
ec2_client.delete_volume(VolumeId=volume["VolumeId"]) | |
except botocore.exceptions.ClientError as error : | |
print("Volume " + volume["VolumeId"] + " cannot be deleted.") | |
print(error) | |
def delete_old_snapshots(ec2_client): | |
"""Delete EBS Snapshots if they are marked past deletion date""" | |
snapshots = check_tag(get_snapshots(ec2_client), "Snapshots") | |
for item in snapshots: | |
snapshot = item[0] | |
age = item[1] | |
snapshot_age = snapshot["StartTime"] | |
old_age = datetime.datetime.today() - datetime.timedelta(days=int(age)) | |
if snapshot_age.replace(tzinfo=None) < old_age: | |
try: | |
ec2_client.delete_snapshot(SnapshotId=snapshot["SnapshotId"]) | |
except botocore.exceptions.ClientError as error: | |
print("Snapshot " + snapshot["SnapshotId"] + " cannot be deleted.") | |
print(error) | |
def delete_old_instances(ec2_client): | |
"""Delete EC2 instances if they are marked past deletion date""" | |
instance_list = get_instances(ec2_client) | |
for instance in instance_list: | |
instances = check_tag(instance, "Instances") | |
for item in instances: | |
instance = item[0] | |
age = item[1] | |
instance_age = instance["LaunchTime"] | |
old_age = datetime.datetime.today() - datetime.timedelta(days=int(age)) | |
if instance_age.replace(tzinfo=None) < old_age: | |
print(instance) | |
try: | |
ec2_client.terminate_instances(InstanceIds=[instance["InstanceId"]]) | |
except botocore.exceptions.ClientError as error : | |
print("Instance " + instance["InstanceId"] + " cannot be terminated.") | |
print(error) | |
def check_tag(items, resource): | |
"""Check if the Delete tag is present and if so return the value attached""" | |
tagged_for_delete = [] | |
for item in items[resource]: | |
if 'Tags' in item: | |
for tag in item['Tags']: | |
if tag['Key'] == 'Delete': | |
delete_tag = tag['Value'] | |
tagged_for_delete.append(tuple([item,delete_tag])) | |
return tagged_for_delete | |
def lambda_handler(event, context): | |
"""Get a list of all regions and run the clean up for each region""" | |
client = boto3.client('ec2') | |
regions = [region['RegionName'] for region in client.describe_regions()['Regions']] | |
for region in regions: | |
ec2_client = boto3.client('ec2', region) | |
print('Running in region: ' + region) | |
delete_old_volumes(ec2_client) | |
delete_old_amis(ec2_client) | |
delete_old_snapshots(ec2_client) | |
delete_old_instances(ec2_client) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment