通过AWS Lambda创建包含描述的快照

时间:2016-11-28 11:47:45

标签: amazon-web-services amazon-ec2 aws-lambda boto3

这是我通过AWS Lambda创建快照的代码。

Sub FindNextCell()
'
'Macro1 Macro

Cells.Find(What:="", _
             After:=Range("F2:I22")(Cells(ActiveCell.Row, _
             Columns.Count).End(xlToLeft).Offset(0, 1)), _
             LookIn:=xlFormulas, LookAt:=xlPart, _
             SearchOrder:=xlByRows, _
             SearchDirection:=xlNext, _
             MatchCase:=False, _
             SearchFormat:=False).Activate
End Sub

我收到了以下回复:

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 = 14

        for volume in ec.volumes.filter(Filters=[
            {'Name': 'attachment.instance-id', 'Values': [instance.id]}
        ]):
            description = 'scheduled-%s.%s-%s' % (instance_name, volume.volume_id, datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))

            print 'description: %s' % (description)

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

Whgen我使用 ec = boto3.resource('ec2') 而不是 ec = boto3.client('ec2') ,我得到了说明,但其他一些例如 describe_instances不起作用

所以,请告诉我 的替代品在 boto3.client('ec2')

5 个答案:

答案 0 :(得分:1)

boto3.resource是低等级boto3.client的抽象。你混合两者。如果您使用的是client.describe_instances,请使用client.describe_volumes

如果您想使用resource.volumes,请使用resource.instances。我更喜欢resource.instances,因为它具有强大的过滤和抽象功能。如果您出于某种原因使用资源并希望访问底层客户端,则可以使用meta获取低级客户端。

ec2 = boto3.resource('ec2')
client = ec2.meta.client

避免处理预订等,请使用resource.instances。如果你谷歌的话,有很多例子。几行代码并且非常易读。

答案 1 :(得分:1)

我遇到了同样的问题,但在我的情况下,我需要将EC2实例中的所有标签复制到快照中,看看我的代码,它可能对您有所帮助,或者可以指导您:

https://github.com/christianhxc/aws-lambda-automated-snapshots/blob/master/src/schedule-ebs-snapshot-backups.py

这样做,您只需要确保实例具有“Name”标签,以便将其复制到快照中,因为CostCenter标记,我也需要这样做

答案 2 :(得分:0)

我发现这些家伙的解决方案是迄今为止最好的解决方案:

https://blog.powerupcloud.com/2016/02/15/automate-ebs-snapshots-using-lambda-function/

他们的代码库中有一些描述示例被添加到快照中。

答案 3 :(得分:0)

我发现联机解决方案https://serverlesscode.com/post/lambda-schedule-ebs-snapshot-backups/不符合我的需求,所以我编写了以下备份脚本。这仍然是我的第一稿,但我发现它更容易阅读,也是一个更好的实现,因为我不是针对实例,而是针对卷。标记也得到了改进。

import boto3
import collections
import datetime
import os

ec = boto3.client('ec2')
ec2 = boto3.resource('ec2')
TAG = os.environ['TAG']

def lambda_handler(event, context):
    volumes = ec.describe_volumes(
        Filters=[
            {'Name': 'tag-key', 'Values': [ "Backup", TAG ]},
            {'Name': 'status', 'Values': [ "in-use" ] },
        ]
    ).get(
        'Volumes', []
    )

    print "Found {0} volumes that need backing up".format(len(volumes))

    for v in volumes:
      vol_id = v['VolumeId']
      print(vol_id)
      vol_name = None
      snap = None
      vol_tags = v.get('Tags', None)

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

      delete_date = datetime.date.today() + datetime.timedelta(days=retention_days)
      delete_fmt = delete_date.strftime('%Y-%m-%d')
      print "Will delete volume: {0} on {1}".format(vol_id, delete_fmt)

      if vol_tags is not None:
        for tag in vol_tags:
          try:
            if tag['Key'] == 'Name':
              vol_name = tag.get('Value')

              print(vol_name)
              snap = ec.create_snapshot(
                  VolumeId=vol_id,
              )

              ec2.Snapshot(id=snap['SnapshotId']).create_tags(
                  Tags=[
                      {'Key': 'DeleteOn', 'Value': delete_fmt},
                      {'Key': 'Name', 'Value': vol_name},
                  ]
              )

              break
          except:
            print "No Tag key 'Name' found."

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

答案 4 :(得分:0)

只需复制该功能并在代码中查找说明,然后将其替换为单引号中的自定义说明。

希望它有所帮助!

#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 = 'Description goes here',
                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},
            ]
        )