Link to home
Start Free TrialLog in
Avatar of introlux
introluxFlag for United Kingdom of Great Britain and Northern Ireland

asked on

AWS - Lambda - Backups (EBS)

Lambda backups via AWS - Currently the backup script in place does the backup perfectly fine however it does not add the label to the snapshot. We would like the backup to place the instance name to the snapshot backup so it is easy to understand. I believe such functionality must exist already as having a snapshot created with no label is not helpful, especially if that instance no longer exists.

Current Script:
import boto3
import collections
import datetime

ec = boto3.client('ec2')

def lambda_handler(event, context):
    reservations = ec.describe_instances(
        Filters=[
            {'Name': 'tag-key', 'Values': ['backup', 'Backup']},
        ]
    ).get(
        'Reservations', []
    )

    instances = [
        i for r in reservations
        for i in r['Instances']
    ]

    print "Found %d instances that need backing up" % len(instances)

    to_tag = collections.defaultdict(list)

    for instance in instances:
        try:
            retention_days = [
                int(t.get('Value')) for t in instance['Tags']
                if t['Key'] == 'Retention'][0]
        except IndexError:
            retention_days = 7

        for dev in instance['BlockDeviceMappings']:
            if dev.get('Ebs', None) is None:
                continue
            vol_id = dev['Ebs']['VolumeId']
            print "Found EBS volume %s on instance %s" % (
                vol_id, instance['InstanceId'])

            snap = ec.create_snapshot(
                VolumeId=vol_id,
            )

            to_tag[retention_days].append(snap['SnapshotId'])

            print "Retaining snapshot %s of volume %s from instance %s for %d days" % (
                snap['SnapshotId'],
                vol_id,
                instance['InstanceId'],
                retention_days,
            )


    for retention_days in to_tag.keys():
        delete_date = datetime.date.today() + datetime.timedelta(days=retention_days)
        delete_fmt = delete_date.strftime('%Y-%m-%d')
        print "Will delete %d snapshots on %s" % (len(to_tag[retention_days]), delete_fmt)
        ec.create_tags(
            Resources=to_tag[retention_days],
            Tags=[
                {'Key': 'DeleteOn', 'Value': delete_fmt},
            ]
        )

Open in new window


Attached completed preview of snapshot which displays no name or description.

Any help will be appreciated.

Thanks,

introlux
snapshot-noname.JPG
Avatar of Kyle Santos
Kyle Santos
Flag of United States of America image

Hi,

I am here to help you with your open question.  Do you still need help?  I have the ability to alert more experts if you still need help.

If you solved the problem on your own, would you please post the solution here in case others have the same problem?

If you need me to delete this question just say "Delete."

Thank you for using Experts Exchange.

Regards,

Kyle Santos
Customer Relations
Avatar of introlux

ASKER

No, still awaiting for a solution on this
I don't have the chance to test out lambda functions, but have you checked out lifecycle management for  ebs snapshots? https://aws.amazon.com/blogs/aws/new-lifecycle-management-for-amazon-ebs-snapshots/

It seems to cover what you're trying to do with Lambda functions.
introlux,

An expert has replied.  Is there anything else we may assist you with?

Regards,

Kyle Santos
Customer Relations
Hi Kyle,

I will take a look at this and give it a test to see if this works.

Thanks,

introlux
Looking at this, on the description it does not state what I was hoping for:

  • instance name
  • mount details
  • eba availability letter

So the idea here is, IF the instance was deleted, then we would be in a position to know how and what to mount it to. Without reference to the existing environments, these backups will be useless.

Does that make sense?

Thanks,

introlux
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.