在 AWS 中计划创建和删除快照
Scheduled creation and deletion of snapshots in AWS
我写了一个 lambda 函数 来创建快照并在 [=21= 中应用了一个 cron 作业]Cloud Watch Events 每天中午 12 点创建。
import boto3
import datetime
ec2 = boto3.resource('ec2')
def lambda_handler(event,handler):
print("\n\nAWS Snapshots starting at %s" % datetime.datetime.now())
instances = ec2.instances.filter(Filters=[
{'Name': 'instance-state-name', 'Values': ['running']}
])
for instance in instances:
instance_name = filter(lambda tag: tag['Key'] == 'Name', instance.tags)[0]['Value']
print("name: %s -id: %s" % (instance_name , instance.id))
for volume in ec2.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)
if volume.create_snapshot(VolumeId=volume.volume_id, Description=description):
print("Snapshot created with description [%s]" % description)
print("\n\nAWS Snapshots completed at %s" % datetime.datetime.now())
return True
现在,我只想删除 7 天后的快照 通过 lambda 创建 function.And 我为此编写了以下内容。
import sys
import boto3
from datetime import datetime, timedelta
try:
days = int(sys.argv[1])
except IndexError:
days = 7
delete_time = datetime.utcnow() - timedelta(days=days)
print 'Deleting any snapshots older than {days} days'.format(days=days)
ec2 = boto3.resource('ec2')
snapshots = ec2.get_all_snapshots(filters=filters)
deletion_counter = 0
size_counter = 0
for snapshot in snapshots:
start_time = datetime.strptime(
snapshot.start_time,'%Y-%m-%dT%H:%M:%S.000Z'
)
if start_time < delete_time:
print 'Deleting {id}'.format(id=snapshot.id)
deletion_counter = deletion_counter + 1
size_counter = size_counter + snapshot.volume_size
snapshot.delete(dry_run=False)
print 'Deleted {number} snapshots totalling {size} GB'.format(number=deletion_counter,size=size_counter)
通过这样做,我得到以下错误:
module initialization error: 'ec2.ServiceResource' object has no attribute 'get_all_snapshots'
我该怎么办?
首先,我会更新创建快照的 Lamdba 函数,以便为每个快照添加一个标签,其中包含可以删除快照的日期。您可以将标签命名为:backup-expiry-time
,标签的值是可以删除标签的日期,例如03-11-2016
接下来我将创建另一个 Lamdba 函数,其唯一目的是删除快照(这样您就不会创建具有两个角色的 Lambda 函数,例如创建和删除快照)。
然后我会使用:http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.describe_snapshots
获取具有 backup-expiry-time
标签的所有快照的列表。遍历此快照列表并删除 backup-expiry-time
标记的值显示可以安全删除的任何内容。
通过 CloudWatch 使用 cron 调用快照删除 Lambda 函数,方法与您为快照创建函数所做的相同。
我写了一个 lambda 函数 来创建快照并在 [=21= 中应用了一个 cron 作业]Cloud Watch Events 每天中午 12 点创建。
import boto3
import datetime
ec2 = boto3.resource('ec2')
def lambda_handler(event,handler):
print("\n\nAWS Snapshots starting at %s" % datetime.datetime.now())
instances = ec2.instances.filter(Filters=[
{'Name': 'instance-state-name', 'Values': ['running']}
])
for instance in instances:
instance_name = filter(lambda tag: tag['Key'] == 'Name', instance.tags)[0]['Value']
print("name: %s -id: %s" % (instance_name , instance.id))
for volume in ec2.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)
if volume.create_snapshot(VolumeId=volume.volume_id, Description=description):
print("Snapshot created with description [%s]" % description)
print("\n\nAWS Snapshots completed at %s" % datetime.datetime.now())
return True
现在,我只想删除 7 天后的快照 通过 lambda 创建 function.And 我为此编写了以下内容。
import sys
import boto3
from datetime import datetime, timedelta
try:
days = int(sys.argv[1])
except IndexError:
days = 7
delete_time = datetime.utcnow() - timedelta(days=days)
print 'Deleting any snapshots older than {days} days'.format(days=days)
ec2 = boto3.resource('ec2')
snapshots = ec2.get_all_snapshots(filters=filters)
deletion_counter = 0
size_counter = 0
for snapshot in snapshots:
start_time = datetime.strptime(
snapshot.start_time,'%Y-%m-%dT%H:%M:%S.000Z'
)
if start_time < delete_time:
print 'Deleting {id}'.format(id=snapshot.id)
deletion_counter = deletion_counter + 1
size_counter = size_counter + snapshot.volume_size
snapshot.delete(dry_run=False)
print 'Deleted {number} snapshots totalling {size} GB'.format(number=deletion_counter,size=size_counter)
通过这样做,我得到以下错误:
module initialization error: 'ec2.ServiceResource' object has no attribute 'get_all_snapshots'
我该怎么办?
首先,我会更新创建快照的 Lamdba 函数,以便为每个快照添加一个标签,其中包含可以删除快照的日期。您可以将标签命名为:backup-expiry-time
,标签的值是可以删除标签的日期,例如03-11-2016
接下来我将创建另一个 Lamdba 函数,其唯一目的是删除快照(这样您就不会创建具有两个角色的 Lambda 函数,例如创建和删除快照)。
然后我会使用:http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.describe_snapshots
获取具有 backup-expiry-time
标签的所有快照的列表。遍历此快照列表并删除 backup-expiry-time
标记的值显示可以安全删除的任何内容。
通过 CloudWatch 使用 cron 调用快照删除 Lambda 函数,方法与您为快照创建函数所做的相同。