-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.py
59 lines (45 loc) · 2.08 KB
/
index.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
import json
import shlex
import subprocess
from botocore.vendored import requests
def lambda_handler(event, context):
if 'RequestType' not in event or 'ResourceProperties' not in event:
send_cfn_response(event, context, "FAILED", {}, "Cannot identify RequestType or ResourceProperties.")
return
properties = event['ResourceProperties']
if "CreateAsDefault" in properties and properties["CreateAsDefault"] == "True":
command_request_type = "Create" # Ignore cfn RequestType
else:
command_request_type = event['RequestType']
if command_request_type in properties and properties[command_request_type] != "":
try:
output = run_cmd("/var/task/wrapper " + properties[command_request_type])
if output is None or output == "":
r = {}
else:
r = json.loads(output)
send_cfn_response(event, context, "SUCCESS", r, "Command Executed.")
except Exception as e:
print(e)
send_cfn_response(event, context, "FAILED", {}, "Failed to execute the command.")
else:
send_cfn_response(event, context, "SUCCESS", {}, command_request_type + " has no action.")
def run_cmd(command):
command = shlex.split("/bin/bash -c '" + command + "'")
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output, stderr = process.communicate()
return output
def send_cfn_response(event, context, response_status, response_data, reason):
response_body = {'Status': response_status,
'Reason': 'Log stream name: ' + context.log_stream_name,
'PhysicalResourceId': context.log_stream_name,
'StackId': event['StackId'],
'RequestId': event['RequestId'],
'LogicalResourceId': event['LogicalResourceId'],
'Data': response_data}
print("Log Reason: " + reason)
try:
requests.put(event['ResponseURL'], data=json.dumps(response_body))
except Exception as e:
print(e)
raise