【问题标题】:Creation of snapshots using AWS Lambda使用 AWS Lambda 创建快照
【发布时间】:2017-03-18 05:08:57
【问题描述】:

我尝试使用以下脚本为具有标签名称[备份或备份]的实例创建快照> 根据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 运行。那么,空响应背后的原因可能是什么?

【问题讨论】:

    标签: amazon-web-services lambda


    【解决方案1】:

    我得到了这个完全工作。它将根据它们的 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},
                ]
            )
    

    【讨论】:

      【解决方案2】:

      在 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】:

        我遇到了类似的问题,但我通过稍微不同的过滤解决了它:

            Filters=[
                {'Name': 'tag:toBackup', 'Values': ['yes', 'Yes']},
            ]
        

        对于我要备份的每个实例,我设置了一个标签,其中 Key 字段设置为 toBackupValue 字段设置为 yesYes.

        灵感来自this answer mootmoot,指向 Russell Ballestrini 博客 Filtering AWS resources with Boto3

        【讨论】:

          【解决方案4】:

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

          【讨论】:

          • 每个实例都有标签。根据代码,将为所有具有备份或备份标签的实例创建快照。因此,转到标签部分并将标签添加到您需要备份的所有实例中。这将解决。
          • 谢谢,这行得通..我在两个不同的地区工作,这就是为什么它不起作用..
          猜你喜欢
          • 1970-01-01
          • 2017-04-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-19
          • 1970-01-01
          • 2020-07-10
          • 1970-01-01
          相关资源
          最近更新 更多