Skip to content

Instantly share code, notes, and snippets.

@mlapida
Last active April 29, 2022 08:10
Show Gist options
  • Select an option

  • Save mlapida/770aba3ad3be76f6b31f to your computer and use it in GitHub Desktop.

Select an option

Save mlapida/770aba3ad3be76f6b31f to your computer and use it in GitHub Desktop.
A lambda function for taking a snapshot of all EC2 instances in a region and cleaning the snapshots up after a set number of days. This ones now works best in conjunction with Asset Tagging https://gist.github.com/mlapida/931c03cce1e9e43f147b A full write up can be found on my site https://empty.coffee/tagging-and-snapshotting-with-lambda/
import boto3
import logging
from datetime import *
import re
#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.INFO)
#define the connection and the region
ec2 = boto3.resource('ec2', region_name="us-west-1")
#what is your aws account number?
AWSAccount = '10927310972'
#set the date to today for the snapshot
today = datetime.now().date()
#set the snapshot removal offset
cleanDate = today-timedelta(days=28)
def lambda_handler(event, context):
#snapshot all instances
base = ec2.instances.all()
#loop through by running instances
for instance in base:
for t in instance.tags:
#pull the name tag
if t['Key'] == 'Name':
instanceName = t['Value']
#snapshop the instances
for vol in instance.volumes.all():
description = str(today) + "-" + instanceName + "-" + vol.id + "-automated"
#snapshot that server
snapshot = ec2.create_snapshot(VolumeId=vol.id, Description=description)
print snapshot
#a regulare expressiong for YYYY-MM-DD
datePattern = re.compile('^(?:(?:(?:(?:(?:[13579][26]|[2468][048])00)|(?:[0-9]{2}(?:(?:[13579][26])|(?:[2468][048]|0[48]))))-(?:(?:(?:09|04|06|11)-(?:0[1-9]|1[0-9]|2[0-9]|30))|(?:(?:01|03|05|07|08|10|12)-(?:0[1-9]|1[0-9]|2[0-9]|3[01]))|(?:02-(?:0[1-9]|1[0-9]|2[0-9]))))|(?:[0-9]{4}-(?:(?:(?:09|04|06|11)-(?:0[1-9]|1[0-9]|2[0-9]|30))|(?:(?:01|03|05|07|08|10|12)-(?:0[1-9]|1[0-9]|2[0-9]|3[01]))|(?:02-(?:[01][0-9]|2[0-8])))))')
print "Cleaning out old entries starting on " + str(cleanDate)
#clean up old snapshots
for snap in ec2.snapshots.filter(Filters=[{'Name': 'owner-id', 'Values': [AWSAccount]}]):
results = datePattern.match(snap.description)
#veryify results have a value
if results is not None:
snapDate = datetime.strptime(results.group(0),'%Y-%m-%d').date()
#check the age of the snapshot, delete the old ones
if cleanDate > snapDate:
print "Deleteing: " + snap.id + " - From: " + str(snapDate)
snapshot = ec2.Snapshot(snap.id).delete()
print snapshot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment