Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlambda_function.py
More file actions
Latest commit
136 lines (104 loc) · 4.45 KB
/
Copy pathlambda_function.py
File metadata and controls
136 lines (104 loc) · 4.45 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
AWS Lambda function to create daily EBS snapshots and prune expired ones.
Iterates over all configured regions and creates a snapshot of every EBS
volume attached to an instance carrying the configured backup tag. Each
snapshot is tagged with a 'DeleteOn' date; snapshots whose 'DeleteOn' date
matches today are deleted.
"""
importlogging
importos
fromdatetimeimportdate, timedelta
importboto3
frombotocore.exceptionsimportClientError
logger=logging.getLogger()
logger.setLevel(logging.INFO)
BACKUP_TAG=os.environ.get("EC2_INSTANCE_TAG", "Backup")
RETENTION_DAYS=int(os.environ.get("RETENTION_DAYS", "10"))
REGIONS= [r.strip() forrinos.environ.get("REGIONS", "eu-central-1").split(",") ifr.strip()]
deffind_instance_name(instance: dict) ->str:
"""
Return the value of the 'Name' tag of an instance or an empty string.
"""
fortagininstance.get("Tags", []):
iftag["Key"] =="Name":
returntag["Value"]
return""
deffind_backup_instances(client) ->list[dict]:
"""
Return all instances carrying the configured backup tag.
"""
instances= []
paginator=client.get_paginator("describe_instances")
forpageinpaginator.paginate(Filters=[{"Name": "tag-key", "Values": [BACKUP_TAG]}]):
forreservationinpage["Reservations"]:
instances.extend(reservation["Instances"])
returninstances
defcreate_snapshots(client, instances: list[dict], today: date) ->int:
"""
Create a snapshot of every EBS volume of the given instances.
Returns the number of snapshots successfully created.
"""
delete_date=today+timedelta(days=RETENTION_DAYS)
created=0
forinstanceininstances:
instance_id=instance["InstanceId"]
instance_name=find_instance_name(instance)
fordeviceininstance["BlockDeviceMappings"]:
ifnotdevice.get("Ebs"):
# skip non EBS volumes
continue
vol_id=device["Ebs"]["VolumeId"]
device_name=device["DeviceName"]
logger.info("Found EBS volume %s on instance %s", vol_id, instance_id)
try:
client.create_snapshot(
Description=f"Snapshot of {device_name} from {instance_id} ({instance_name})",
VolumeId=vol_id,
TagSpecifications=[
{
"ResourceType": "snapshot",
"Tags": [
{"Key": "DeleteOn", "Value": delete_date.strftime("%Y-%m-%d")},
{"Key": "Name", "Value": f"{vol_id}_{today.strftime('%Y-%m-%d')}"},
{"Key": "DeviceName", "Value": device_name},
],
},
],
)
created+=1
exceptClientErrorasexc:
logger.error("Could not create snapshot of volume %s: %s", vol_id, exc)
returncreated
defdelete_expired_snapshots(client, today: date) ->int:
"""
Delete all snapshots owned by this account whose 'DeleteOn' tag matches today.
Returns the number of snapshots successfully deleted.
"""
filters= [
{"Name": "tag-key", "Values": ["DeleteOn"]},
{"Name": "tag-value", "Values": [today.strftime("%Y-%m-%d")]},
]
deleted=0
paginator=client.get_paginator("describe_snapshots")
forpageinpaginator.paginate(OwnerIds=["self"], Filters=filters):
forsnapshotinpage["Snapshots"]:
snapshot_id=snapshot["SnapshotId"]
try:
client.delete_snapshot(SnapshotId=snapshot_id)
logger.info("Deleted snapshot %s", snapshot_id)
deleted+=1
exceptClientErrorasexc:
logger.error("Could not delete snapshot %s: %s", snapshot_id, exc)
returndeleted
deflambda_handler(event, context): # pylint: disable=unused-argument
"""
Entrypoint for AWS Lambda.
"""
today=date.today()
forregioninREGIONS:
logger.info("Processing region %s", region)
client=boto3.client("ec2", region_name=region)
instances=find_backup_instances(client)
created=create_snapshots(client, instances, today)
deleted=delete_expired_snapshots(client, today)
logger.info("Region %s: created %d snapshot(s), deleted %d expired snapshot(s)", region, created, deleted)