Skip to content

Instantly share code, notes, and snippets.

@mlapida
Last active April 29, 2022 08:10
Show Gist options
  • Save mlapida/770aba3ad3be76f6b31f to your computer and use it in GitHub Desktop.
Save mlapida/770aba3ad3be76f6b31f to your computer and use it in GitHub Desktop.

Revisions

  1. mlapida revised this gist Feb 10, 2021. No changes.
  2. Michael Lapidakis revised this gist Jan 29, 2016. No changes.
  3. Michael Lapidakis revised this gist Jan 15, 2016. 1 changed file with 64 additions and 37 deletions.
    101 changes: 64 additions & 37 deletions EC2-Snapshot-Lambda.py
    Original file line number Diff line number Diff line change
    @@ -1,59 +1,86 @@
    import boto3
    import logging
    from datetime import *
    import datetime
    import re
    import time

    #setup simple logging for INFO
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    logger.setLevel(logging.ERROR)

    #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()
    #define the connection
    ec2 = boto3.resource('ec2', region_name="us-west-2")

    #set the snapshot removal offset
    cleanDate = today-timedelta(days=28)
    cleanDate = datetime.datetime.now()-datetime.timedelta(days=5)

    #Set this to True if you don't want the function to perform any actions
    debugMode = False

    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']
    if debugMode == True:
    print("-------DEBUG MODE----------")

    #snapshop the instances
    for vol in ec2.volumes.all():
    tempTags=[]

    #Prepare Volume tags to be importated into the snapshot
    if vol.tags != None:
    for t in vol.tags:

    #pull the name tag
    if t['Key'] == 'Name':
    instanceName = t['Value']
    tempTags.append(t)
    else:
    tempTags.append(t)
    else:
    print("Issue retriving tag")
    instanceName = "NoName"
    t['Key'] = 'Name'
    t['Value'] = 'Missing'
    tempTags.append(t)

    description = str(datetime.datetime.now()) + "-" + instanceName + "-" + vol.id + "-automated"

    #snapshop the instances
    for vol in instance.volumes.all():
    description = str(today) + "-" + instanceName + "-" + vol.id + "-automated"
    if debugMode != True:
    #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])))))')

    #write the tags to the snapshot
    tags = snapshot.create_tags(
    Tags=tempTags
    )
    print("[LOG] " + str(snapshot))

    else:
    print("[DEBUG] " + str(tempTags))

    print "Cleaning out old entries starting on " + str(cleanDate)
    print "[LOG] 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)

    for snap in ec2.snapshots.all():

    #veryify results have a value
    if results is not None:
    if snap.description.endswith("-automated"):

    snapDate = datetime.strptime(results.group(0),'%Y-%m-%d').date()
    #Pull the snapshot date
    snapDate = snap.start_time.replace(tzinfo=None)
    if debugMode == True:
    print("[DEBUG] " + str(snapDate) +" vs " + str(cleanDate))

    #check the age of the snapshot, delete the old ones
    #Compare the clean dates
    if cleanDate > snapDate:
    print "Deleteing: " + snap.id + " - From: " + str(snapDate)
    snapshot = ec2.Snapshot(snap.id).delete()
    print snapshot
    print("[INFO] Deleteing: " + snap.id + " - From: " + str(snapDate))
    if debugMode != True:
    try:
    snapshot = snap.delete()
    except:

    #if we timeout because of a rate limit being exceeded, give it a rest of a few seconds
    print("[INFO]: Waiting 5 Seconds for the API to Chill")
    time.sleep(5)
    snapshot = snap.delete()
    print("[INFO] " + str(snapshot))
  4. Michael Lapidakis created this gist Oct 17, 2015.
    59 changes: 59 additions & 0 deletions EC2-Snapshot-Lambda.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    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