-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
executable file
·303 lines (224 loc) · 8.26 KB
/
handler.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import boto3
from boto3.session import Session
import time
from datetime import datetime as dt
import pprint
TAG_KEY_BACKUP_GENERATION = 'Backup-Generation'
TAG_KEY_AUTO_BACKUP = 'Backup-Type'
TAG_VAL_AUTO_BACKUP = 'auto'
print('Loading function')
pp = pprint.PrettyPrinter(indent=4)
# 関数名 : lambda_handler
def lambda_handler(event, context):
print("Received event: " + json.dumps(event, indent=2))
ec2_client = boto3.client('ec2')
ec2_resource = boto3.resource('ec2')
ret = execute_ami_backup_task(ec2_client, ec2_resource)
print 'AMI buckup task is completed(%s).' % (ret)
return 0
raise Exception('Something went wrong')
# 関数名 : execute_ami_backup_task
# 戻り値 : 実行結果
# 引数 : ec2_client
# : ec2_resource
# 機能 : AMIバックアップを実行する
def execute_ami_backup_task(ec2_client, ec2_resource):
response = ec2_client.describe_instances()
exec_time = dt.now().strftime('%Y%m%d%H%M%S')
result = True
for ec2_group in response['Reservations']:
for instance_info in ec2_group['Instances']:
ret = is_target(instance_info)
if (ret == False):
continue
ret = create_buckup_image(ec2_client, ec2_resource, instance_info, exec_time)
if not ret:
print 'create_buckup_image(%s) was failed.' % (instance_info['InstanceId'])
result = False
continue
ret = delete_old_image(ec2_client, ec2_resource, instance_info)
if not ret:
print 'delete_old_image(%s) was failed.' % (instance_info['InstanceId'])
result = False
continue
return result
# 関数名 : is_target
# 戻り値 : バックアップ要否
# 引数 : instance_info <dict>
# 機能 : バックアップ要否を判定する
def is_target(instance_info):
val = get_tag_value(
instance_info,
TAG_KEY_BACKUP_GENERATION
)
if val is None:
return False
return True
# 関数名 : get_tag_value
# 戻り値 : タグ値(当該キーに合致するものがなければNone)
# 引数 : instance_info <dict>
# : key <str>
# 機能 : インスタンス情報から指定キーのタグ値を取得する
def get_tag_value(instance_info, key):
tags = instance_info['Tags']
for tag in tags:
if not (key == tag['Key']):
continue
return tag['Value']
return None
# 関数名 : create_buckup_image
# 戻り値 : 実行結果
# 引数 : ec2_client
# : ec2_resource
# : instance_info <dict>
# : exec_time <str>
# 機能 : バックアップイメージを作成する
def create_buckup_image(ec2_client, ec2_resource, instance_info, exec_time):
inst_id = instance_info['InstanceId']
name = get_tag_value(instance_info, 'Name')
if name is None:
print('Get name error!!')
return False
image_name = name + '-' + exec_time
response = ec2_client.create_image(
InstanceId = inst_id,
Name = image_name,
Description = image_name,
NoReboot = True
)
image_id = response['ImageId']
print '%s was created.' % (image_id)
# 念のため、タグ設定対象のイメージ&スナップショットが出来上がるまで待つ
time.sleep(10)
tags = construct_backup_tags(instance_info)
image = ec2_resource.Image(image_id)
set_tags_to_image(image, tags)
set_tags_to_snapshot(ec2_resource, image, tags, image_name)
return True
# 関数名 : construct_backup_tags
# 戻り値 : タグ群
# 引数 : instance_info <dict>
# 機能 : バックアプ設定用のタグ群を構成する
def construct_backup_tags(instance_info):
ret_tags = []
tags = instance_info['Tags']
for tag in tags:
# バックアップ設定用のタグは除去する
if (TAG_KEY_BACKUP_GENERATION == tag['Key']):
continue
# "aws:"から始まるタグは予約タグなので除去(Cfnから作成時などに付与されている
if (tag['Key'].startswith("aws:")):
continue
ret_tags.append(tag)
t = {u'Value': TAG_VAL_AUTO_BACKUP, u'Key': TAG_KEY_AUTO_BACKUP}
ret_tags.append(t)
return ret_tags
# 関数名 : set_tags_to_image
# 戻り値 : non
# 引数 : image
# : tags <list>
# 機能 : AMIイメージにタグ情報を設定する
def set_tags_to_image(image, tags):
image.create_tags(Tags = tags)
return
# 関数名 : set_tags_to_snapshot
# 戻り値 : non
# 引数 : ec2_resource
# : image
# : tags <list>
# : image_name <str>
# 機能 : スナップショットにタグ情報を設定する
def set_tags_to_snapshot(ec2_resource, image, tags, image_name):
for dev in image.block_device_mappings:
# EBS以外は対象外
if not dev.has_key('Ebs'):
continue
# Nameタグ差し替えのため、一旦削除
name_idx = get_name_tag_index(tags)
tags.pop(name_idx)
# Nameタグ設定
dev_name = dev['DeviceName'][5:]
name = image_name + '-' + dev_name
t = {u'Value': name, u'Key': 'Name'}
tags.append(t)
snapshot_id = dev['Ebs']['SnapshotId']
snapshot = ec2_resource.Snapshot(snapshot_id)
snapshot.create_tags(Tags = tags)
return
# 関数名 : get_name_tag_index
# 戻り値 : Nameタグのインデックス位置(当該キーに合致するものがなければNone)
# 引数 : tags <list>
# 機能 : タグリストの中でNameタグのインデックス位置を取得する
def get_name_tag_index(tags):
idx = 0
for tag in tags:
if tag['Key'] == 'Name':
return idx
idx += 1
return None
# 関数名 : delete_old_image
# 戻り値 : 実行結果
# 引数 : ec2_client
# : ec2_resource
# : instance_info <dict>
# 機能 : 保持世代よりも古いイメージを削除する
def delete_old_image(ec2_client, ec2_resource, instance_info):
sorted_images = get_sorted_images(ec2_client, instance_info)
generation = int(get_tag_value(instance_info, TAG_KEY_BACKUP_GENERATION))
cnt = 0
for img in sorted_images:
cnt += 1
if generation >= cnt:
continue
image_id = img['ImageId']
snapshots = get_snapshots(ec2_resource, image_id)
# AMIイメージを解放する
ec2_client.deregister_image(
ImageId = image_id
)
print '%s was deregistered.' % (image_id)
# 解放完了まで待つ
time.sleep(10)
# 対応するスナップショットを削除する
for snapshot in snapshots:
snapshot.delete()
print '%s was deleted.' % (snapshot)
return True
# 関数名 : get_sorted_images
# 戻り値 : ソート済みイメージ <list>
# 引数 : ec2_client
# : instance_info <dict>
# 機能 : 作成順でソートしたAMIイメージリストを取得する
def get_sorted_images(ec2_client, instance_info):
sorted_images = []
name = get_tag_value(instance_info, 'Name')
response = ec2_client.describe_images(
Owners = ['self'],
Filters = [{'Name': 'tag:Name', 'Values': [name]},
{'Name': 'tag:' + TAG_KEY_AUTO_BACKUP, 'Values': [TAG_VAL_AUTO_BACKUP]}]
)
images = response['Images']
sorted_images = sorted(
images,
key = lambda x: x['CreationDate'],
reverse = True
)
return sorted_images
# 関数名 : get_snapshots
# 戻り値 : スナップショット群 <list>
# 引数 : ec2_resource
# : image_id <str>
# 機能 : AMIイメージに包含されるスナップショット群を取得する
def get_snapshots(ec2_resource, image_id):
snapshots = []
image = ec2_resource.Image(image_id)
for dev in image.block_device_mappings:
if not dev.has_key('Ebs'):
continue
snapshot_id = dev['Ebs']['SnapshotId']
snapshot = ec2_resource.Snapshot(snapshot_id)
snapshots.append(snapshot)
return snapshots