使用AWS Lambda创建快照

时间:2016-11-04 11:23:38

标签: amazon-web-services lambda

我尝试使用以下脚本为 标记 名称 [备份或备份] 根据https://serverlesscode.com/post/lambda-schedule-ebs-snapshot-backups/&& https://serverlesscode.com/post/lambda-schedule-ebs-snapshot-backups-2/

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 = sum(
    [
        [i for i in r['Instances']]
        for r in reservations
    ], [])

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},
        ]
    )

代码运行良好,但返回空响应说:

Found 0 instances that need backing up 

我的控制台中有两个实例,标签名为Backup。那么,什么可能是空响应背后的原因?

4 个答案:

答案 0 :(得分:0)

我已经解决了。到目前为止,我正以错误的方式分配我的标签。

答案 1 :(得分:0)

我有一个类似的问题,我用稍微不同的过滤解决了它:

    year USA1 USA2
USA 2000    7    8
USA 2001   15   16

对于我要备份的每个实例,我设置了一个标记,字段设置为 Filters=[ {'Name': 'tag:toBackup', 'Values': ['yes', 'Yes']}, ] 字段设置为{{ 1}}或toBackup

this answer mootmoot启发,指向Russell Ballestrini博客Filtering AWS resources with Boto3

答案 2 :(得分:0)

在Lambda控制台中,浏览功能>创建Lambda函数 - >配置功能。然后,使用以下参数:名称,描述,运行时。我使用AWS SDK for Python中的Boto库来获取以下代码:

    # Backup all in-use volumes in all regions


import boto3

def lambda_handler(event, context):
    ec2 = boto3.client('ec2')

    # Get list of regions
    regions = ec2.describe_regions().get('Regions',[] )

    # Iterate over regions
    for region in regions:
        print "Checking region %s " % region['RegionName']
        reg=region['RegionName']

        # Connect to region
        ec2 = boto3.client('ec2', region_name=reg)

        # Get all in-use volumes in all regions 
        result = ec2.describe_volumes( Filters=[{'Name': 'status', 'Values': ['in-use']}])

        for volume in result['Volumes']:
            print "Backing up %s in %s" % (volume['VolumeId'], volume['AvailabilityZone'])

            # Create snapshot
            result = ec2.create_snapshot(VolumeId=volume['VolumeId'],Description='Created by Lambda backup function ebs-snapshots')

            # Get snapshot resource
            ec2resource = boto3.resource('ec2', region_name=reg)
            snapshot = ec2resource.Snapshot(result['SnapshotId'])

            volumename = 'N/A'

            # Find name tag for volume if it exists
            if 'Tags' in volume:
                for tags in volume['Tags']:
                    if tags["Key"] == 'Name':
                        volumename = tags["Value"]

            # Add volume name to snapshot for easier identification
            snapshot.create_tags(Tags=[{'Key': 'Name','Value': volumename}])

它将为所有区域的任何卷创建快照。此外,它会将卷的名称添加到创建的快照名称标记中。因此,识别快照列表将更容易识别。

答案 3 :(得分:0)

我完全正常工作了。它将标记与其Instance_ID和Instance_Name相对应的快照。只需复制lambda函数并相应标记您的EC2。

#Tag to folllow
#Retention    number of days here
#backup
#backup-monthly

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']},
            # Uncomment this line if need to take snaphsot of running instances only
            # {'Name': 'instance-state-name', 'Values': ['running']},
        ]
    ).get(
        'Reservations', []
    )

    instances = sum(
        [
            [i for i in r['Instances']]
            for r in reservations
        ], [])

    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'])

            instance_id = instance['InstanceId']

            snapshot_name = 'N/A'
            if 'Tags' in instance:
                for tags in instance['Tags']:
                    if tags["Key"] == 'Name':
                        snapshot_name = tags["Value"]

            print "Tagging snapshot with Name: {} and Instance ID {}".format(snapshot_name, instance_id)

            snap = ec.create_snapshot(
                Description = 'Instance ID is {} and Snapshot taken by Lambda script'.format(instance_id),
                VolumeId = vol_id,
                TagSpecifications = [{
                    'ResourceType': 'snapshot',
                    'Tags': [{
                        'Key': 'Name',
                        'Value': snapshot_name
                    }, ]
                }, ]
                # DryRun = False
                )

            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},
            ]
        )