Created
January 28, 2018 11:22
-
-
Save asvignesh/77c709cb86759d9c297d7a997b12d02a to your computer and use it in GitHub Desktop.
Delete old EBS snapshots using Boto3 AWS Lambda Python
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
from datetime import datetime, timedelta, timezone | |
import boto3 | |
class Ec2Instances(object): | |
def __init__(self, region): | |
print("region "+ region) | |
self.ec2 = boto3.client('ec2', region_name=region) | |
def delete_snapshots(self, older_days=1): | |
delete_snapshots_num = 0 | |
snapshots = self.get_nimesa_created_snapshots() | |
for snapshot in snapshots['Snapshots']: | |
fmt_start_time = snapshot['StartTime'] | |
if (fmt_start_time < self.get_delete_data(older_days)): | |
self.delete_snapshot(snapshot['SnapshotId']) | |
delete_snapshots_num+1 | |
return delete_snapshots_num | |
def get_nimesa_created_snapshots(self): | |
snapshots = self.ec2.describe_snapshots(Filters=[{'Name': 'description', 'Values': ['Created by Nimesa']}]) | |
return snapshots | |
def get_delete_data(self, older_days): | |
delete_time = datetime.now(tz=timezone.utc) - timedelta(days=older_days) | |
return delete_time; | |
def delete_snapshot(self, snapshot_id): | |
self.ec2.delete_snapshot(SnapshotId=snapshot_id) | |
def lambda_handler(event, context): | |
print("event " + str(event)) | |
print("context " + str(context)) | |
ec2_reg = boto3.client('ec2') | |
regions = ec2_reg.describe_regions() | |
for region in regions['Regions']: | |
region_name = region['RegionName'] | |
instances = Ec2Instances(region_name) | |
deleted_counts = instances.delete_snapshots(1) | |
print("deleted_counts for region "+ str(region_name) +" is " + str(deleted_counts)) | |
return 'completed' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you please also share unit test/coverage test/pytest for this ?