forked from aws-samples/aws-lambda-lifecycle-hooks-function
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_backup.py
134 lines (125 loc) · 4.74 KB
/
lambda_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
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
import boto3
import json
import logging
import time
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
ssm_client = boto3.client("ssm")
LIFECYCLE_KEY = "LifecycleHookName"
ASG_KEY = "AutoScalingGroupName"
EC2_KEY = "EC2InstanceId"
DOCUMENT_NAME = "ASGLogBackup"
RESPONSE_DOCUMENT_KEY = "DocumentIdentifiers"
def check_response(response_json):
try:
if response_json['ResponseMetadata']['HTTPStatusCode'] == 200:
return True
else:
return False
except KeyError:
return False
def list_document():
document_filter_parameters = {'key': 'Name', 'value': DOCUMENT_NAME}
response = ssm_client.list_documents(
DocumentFilterList=[ document_filter_parameters ]
)
return response
def check_document():
# If the document already exists, it will not create it.
try:
response = list_document()
if check_response(response):
logger.info("Documents list: %s", response)
if response[RESPONSE_DOCUMENT_KEY]:
logger.info("Documents exists: %s", response)
return True
else:
return False
else:
logger.error("Documents' list error: %s", response)
return False
except Exception, e:
logger.error("Document error: %s", str(e))
return None
def send_command(instance_id):
# Until the document is not ready, waits in accordance to a backoff mechanism.
while True:
timewait = 1
response = list_document()
if any(response[RESPONSE_DOCUMENT_KEY]):
break
time.sleep(timewait)
timewait += timewait
try:
response = ssm_client.send_command(
InstanceIds = [ instance_id ],
DocumentName = DOCUMENT_NAME,
TimeoutSeconds = 120
)
if check_response(response):
logger.info("Command sent: %s", response)
return response['Command']['CommandId']
else:
logger.error("Command could not be sent: %s", response)
return None
except Exception, e:
logger.error("Command could not be sent: %s", str(e))
return None
def check_command(command_id, instance_id):
timewait = 1
while True:
response_iterator = ssm_client.list_command_invocations(
CommandId = command_id,
InstanceId = instance_id,
Details=False
)
if check_response(response_iterator):
response_iterator_status = response_iterator['CommandInvocations'][0]['Status']
if response_iterator_status != 'Pending':
if response_iterator_status == 'InProgress' or response_iterator_status == 'Success':
logging.info( "Status: %s", response_iterator_status)
return True
else:
logging.error("ERROR: status: %s", response_iterator)
return False
time.sleep(timewait)
timewait += timewait
def abandon_lifecycle(life_cycle_hook, auto_scaling_group, instance_id):
asg_client = boto3.client('autoscaling')
try:
response = asg_client.complete_lifecycle_action(
LifecycleHookName=life_cycle_hook,
AutoScalingGroupName=auto_scaling_group,
LifecycleActionResult='ABANDON',
InstanceId=instance_id
)
if check_response(response):
logger.info("Lifecycle hook abandoned correctly: %s", response)
else:
logger.error("Lifecycle hook could not be abandoned: %s", response)
except Exception, e:
logger.error("Lifecycle hook abandon could not be executed: %s", str(e))
return None
def lambda_handler(event, context):
try:
logger.info(json.dumps(event))
message = event['detail']
if LIFECYCLE_KEY in message and ASG_KEY in message:
life_cycle_hook = message[LIFECYCLE_KEY]
auto_scaling_group = message[ASG_KEY]
instance_id = message[EC2_KEY]
if check_document():
command_id = send_command(instance_id)
if command_id != None:
if check_command(command_id, instance_id):
logging.info("Lambda executed correctly")
else:
abandon_lifecycle(life_cycle_hook, auto_scaling_group, instance_id)
else:
abandon_lifecycle(life_cycle_hook, auto_scaling_group, instance_id)
else:
abandon_lifecycle(life_cycle_hook, auto_scaling_group, instance_id)
else:
logging.error("No valid JSON message: %s", parsed_message)
except Exception, e:
logging.error("Error: %s", str(e))