-
Notifications
You must be signed in to change notification settings - Fork 45
/
custom_resource_lambda.py
166 lines (121 loc) · 4.53 KB
/
custom_resource_lambda.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
"""
This lambda implements the custom resource handler for creating an SSH key
and storing in in SSM parameter store.
e.g.
SSHKeyCR:
Type: Custom::CreateSSHKey
Version: "1.0"
Properties:
ServiceToken: !Ref FunctionArn
KeyName: MyKey
An SSH key called MyKey will be created.
"""
from json import dumps
import sys
import traceback
import urllib.request
import boto3
def log_exception():
"Log a stack trace"
exc_type, exc_value, exc_traceback = sys.exc_info()
print(repr(traceback.format_exception(
exc_type,
exc_value,
exc_traceback)))
def send_response(event, context, response):
"Send a response to CloudFormation to handle the custom resource lifecycle"
responseBody = {
'Status': response,
'Reason': 'See details in CloudWatch Log Stream: ' + \
context.log_stream_name,
'PhysicalResourceId': context.log_stream_name,
'StackId': event['StackId'],
'RequestId': event['RequestId'],
'LogicalResourceId': event['LogicalResourceId'],
}
print('RESPONSE BODY: \n' + dumps(responseBody))
data = dumps(responseBody).encode('utf-8')
req = urllib.request.Request(
event['ResponseURL'],
data,
headers={'Content-Length': len(data), 'Content-Type': ''})
req.get_method = lambda: 'PUT'
try:
with urllib.request.urlopen(req) as response:
print(f'response.status: {response.status}, ' +
f'response.reason: {response.reason}')
print('response from cfn: ' + response.read().decode('utf-8'))
except urllib.error.URLError:
log_exception()
raise Exception('Received non-200 response while sending ' +\
'response to AWS CloudFormation')
return True
def custom_resource_handler(event, context):
'''
This function creates a PEM key, commits it as a key pair in EC2,
and stores it, encrypted, in SSM.
To retrieve the key with currect RSA format, you must use the command line:
aws ssm get-parameter \
--name <KEYNAME> \
--with-decryption \
--region <REGION> \
--output text
Copy the values from (and including) -----BEGIN RSA PRIVATE KEY----- to
-----END RSA PRIVATE KEY----- into a file.
To use it, change the permissions to 600
Ensure to bundle the necessary packages into the zip stored in S3
'''
print("Event JSON: \n" + dumps(event))
# session = boto3.session.Session()
# region = session.region_name
# Original
# pem_key_name = os.environ['KEY_NAME']
pem_key_name = event['ResourceProperties']['KeyName']
response = 'FAILED'
ec2 = boto3.client('ec2')
if event['RequestType'] == 'Create':
try:
print("Creating key name %s" % str(pem_key_name))
key = ec2.create_key_pair(KeyName=pem_key_name)
key_material = key['KeyMaterial']
ssm_client = boto3.client('ssm')
param = ssm_client.put_parameter(
Name=pem_key_name,
Value=key_material,
Type='SecureString')
print(param)
print(f'The parameter {pem_key_name} has been created.')
response = 'SUCCESS'
except Exception as e:
print(f'There was an error {e} creating and committing ' +\
f'key {pem_key_name} to the parameter store')
log_exception()
response = 'FAILED'
send_response(event, context, response)
return
if event['RequestType'] == 'Update':
# Do nothing and send a success immediately
send_response(event, context, response)
return
if event['RequestType'] == 'Delete':
#Delete the entry in SSM Parameter store and EC2
try:
print(f"Deleting key name {pem_key_name}")
ssm_client = boto3.client('ssm')
rm_param = ssm_client.delete_parameter(Name=pem_key_name)
print(rm_param)
_ = ec2.delete_key_pair(KeyName=pem_key_name)
response = 'SUCCESS'
except Exception as e:
print(f"There was an error {e} deleting the key {pem_key_name} ' +\
from SSM Parameter store or EC2")
log_exception()
response = 'FAILED'
send_response(event, context, response)
def lambda_handler(event, context):
"Lambda handler for the custom resource"
try:
return custom_resource_handler(event, context)
except Exception:
log_exception()
raise