forked from chef-boneyard/lambda_ebs_snapshot
-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
ami_backup.py
53 lines (47 loc) · 1.77 KB
/
ami_backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Automated AMI Backups
#
# @author Robert Kozora <[email protected]>
#
# "retention_days" is environment variable which will be used as a retention policy number in days. If there is no
# environment variable with that name, it will use a 14 days default value for each AMI.
#
# After creating the AMI it creates a "DeleteOn" tag on the AMI indicating when
# it will be deleted using the Retention value and another Lambda function
from __future__ import print_function
import boto3
import collections
import datetime
import sys
import pprint
import os
import json
ec = boto3.client('ec2')
ec2_instance_id = os.environ['instance_id']
label_id = os.environ['label_id']
no_reboot = os.environ['reboot'] == '0'
block_device_mappings = json.loads(str(os.environ['block_device_mappings']))
def lambda_handler(event, context):
try:
retention_days = int(os.environ['retention'])
except ValueError:
retention_days = 14
create_time = datetime.datetime.now()
create_fmt = create_time.strftime('%Y-%m-%d')
AMIid = ec.create_image(InstanceId=ec2_instance_id,
Name=label_id + "-" + ec2_instance_id + "-" + create_fmt,
Description=label_id + "-" + ec2_instance_id + "-" + create_fmt,
NoReboot=no_reboot, DryRun=False,
BlockDeviceMappings=block_device_mappings)
print("Retaining AMI %s of instance %s for %d days" % (
AMIid['ImageId'],
ec2_instance_id,
retention_days,
))
delete_date = datetime.date.today() + datetime.timedelta(days=retention_days)
delete_fmt = delete_date.strftime('%m-%d-%Y')
ec.create_tags(
Resources=[AMIid['ImageId']],
Tags=[
{'Key': 'DeleteOn', 'Value': delete_fmt},
]
)