-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.py
More file actions
531 lines (458 loc) · 19.3 KB
/
lambda.py
File metadata and controls
531 lines (458 loc) · 19.3 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import json
import time
import boto3
import requests
from distutils.util import strtobool
from botocore.exceptions import ClientError
client_api = boto3.client('apigateway')
client_lambda = boto3.client('lambda')
def log(message, *args, **kwargs):
'''
log message to cloudwatch logs.
'''
if args or kwargs:
message = message.format(*args, **kwargs)
print(message)
def add_permission(function_name, version, source_arn, type='latest'):
'''
dynamically add permission to the provided 'source_arn' to invoke the given
lambda version(s).
@type, deployment type [blue|green|latest]
'''
#
# local variables
#
statement_id = 'lambda-apigw-invoke-{}'.format(type)
#
# check permission exists
#
try:
if version == '$LATEST' or type == 'latest':
permission = client_lambda.get_policy(FunctionName=function_name)
else:
permission = client_lambda.get_policy(
FunctionName=function_name,
Qualifier=version
)
policy = json.loads(permission['Policy'])
statement = policy['Statement']
except ClientError as e:
if version == '$LATEST' or type == 'latest':
log(
'Notice: failed executing client_lambda.get_policy using function {}',
function_name
)
else:
log(
'Notice: failed executing client_lambda.get_policy using function {}:{}',
function_name,
version
)
permission, policy, statement = None, None, None
policy_found = False
if permission and policy and statement:
policy_found = next([True for x in statement if x['Sid'] == statement_id], False)
#
# conditionally add permission
#
if version != '$LATEST':
function_name = '{}:{}'.format(function_name, version)
if not policy_found:
try:
client_lambda.add_permission(
FunctionName=function_name,
StatementId=statement_id,
Action='lambda:InvokeFunction',
Principal='apigateway.amazonaws.com',
SourceArn=source_arn
)
except ClientError as e:
log(
'Error: executing client_lambda.add_permission ({}) {}',
type,
str(e)
)
def lambda_handler(event, context, physicalResourceId=None, noEcho=False):
'''
custom lambda resource workaround used to associate an event trigger on a
given lambda function with a desired api gateway (created from a different
cloudformation parent-stack). Conventional approach utilizes the 'Events'
property under the 'AWS::Serverless::Function' SAM resource. However, this
automatically creates an api gateway, if an api gateway is not explicitly
created in the SAM template. To remove the side-effect (i.e. duplicated api
gateway), rather than registering the 'Events' property with the serverless
function in the SAM-child template, the below function will register the
lambda trigger with the already created api gateway from the parent-stack.
'''
properties = event['ResourceProperties']
tracing_enabled = bool(strtobool(properties.get('TracingEnabled', 'True')))
boundary_length = int(properties.get('BoundaryLength', 30))
boundary_character = properties.get('BoundaryCharacter', '=').strip()
debug_status = bool(strtobool(properties.get('DebugStatus', 'False').capitalize()))
function_name = properties.get('FunctionName').strip()
stage_name = properties.get('StageName', 'primary_stage').strip()
api_id = properties.get('ApiId').strip()
method_http_method = properties.get('MethodHttpMethod', 'GET').strip()
method_authorization_type = properties.get('MethodAuthorizationType', 'NONE').strip()
method_response_http_method = properties.get('MethodResponseHttpMethod', 'GET').strip()
method_response_status_code = properties.get('MethodResponseStatusCode', '200')
integration_http_method = properties.get('IntegrationHttpMethod', 'POST')
http_method_common = properties.get('HttpMethodCommon', 'POST').strip()
integration_type = properties.get('IntegrationType', 'AWS').strip()
integration_uri = properties.get('IntegrationUri', 'http://httpbin.org/robots.txt').strip()
integration_response_status_code = properties.get('IntegrationResponseStatusCode', '200').strip()
integration_response_template = json.loads(
properties.get(
'IntegrationResponseTemplate',
'{"application/json": ""}'
)
)
put_integration_credentials = properties.get('PutIntegrationCredentials', None)
method_response_models = properties.get('MethodResponseModels', {'application/json': 'Empty'})
canary_percent_traffic = float(properties.get('CanaryPercentTraffic', 20))
use_stage_cache = bool(strtobool(properties.get('UseStageCache', 'True')))
path_part = properties.get('PathPart', '/').strip()
source_arn = properties.get('SourceArn').strip()
version_latest = properties.get('VersionLatest', '$LATEST').strip()
version_name_blue = properties.get('VersionNameBlue', 'blue').strip()
version_name_green = properties.get('VersionNameGreen', 'green').strip()
aliases = client_lambda.list_aliases(FunctionName=function_name)['Aliases']
boundary = ''.format(boundary_character * boundary_length)
#
# @root_id: resource id obtained to be used relative to '/lambda' path part
# defined from earlier SAM template 'AWS::ApiGateway::Resource'
#
resource = client_api.get_resources(restApiId=api_id)
root_id = [x for x in resource['items'] if x['path'] == '/{}'.format(path_part)][0]['id']
#
# x-ray tracing
#
if tracing_enabled:
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch_all
patch_all()
#
# lambda version: obtain latest version of desired lambda function
#
marker = None
while True:
if marker:
response_iterator = client_lambda.list_versions_by_function(
FunctionName=function_name,
Marker=marker,
MaxItems=100
)
else:
response_iterator = client_lambda.list_versions_by_function(
FunctionName=function_name,
MaxItems=100
)
try:
marker = response_iterator['NextMarker']
except KeyError:
version_blue = response_iterator['Versions'][-1]['Version']
if len(response_iterator['Versions']) > 1:
version_blue = response_iterator['Versions'][-2]['Version']
version_green = response_iterator['Versions'][-1]['Version']
#
# blue deployment: create/update alias
#
if next(iter(filter(lambda x: x['Name'] == version_blue, aliases)), False):
update_alias_blue = client_lambda.update_alias(
FunctionName=function_name,
Name=version_name_blue,
FunctionVersion=version_blue,
Description='updated {} at version {} (blue)'.format(function_name, version_blue)
)
print(boundary)
log('Notice: update_alias_blue: {}', update_alias_blue)
print(boundary)
else:
create_alias_blue = client_lambda.create_alias(
FunctionName=function_name,
Name=version_name_blue,
FunctionVersion=version_blue,
Description='created {} at version {} (blue)'.format(function_name, version_blue)
)
print(boundary)
log('Notice: create_alias_blue: {}', create_alias_blue)
print(boundary)
#
# green deployment: create/update alias
#
if next(iter(filter(lambda x: x['Name'] == version_green, aliases)), False):
client_lambda.get_alias(function_name, version_name_green)
update_alias_green = client_lambda.update_alias(
FunctionName=function_name,
Name=version_name_green,
FunctionVersion=version_green,
Description='updated {} at version {} (green)'.format(function_name, version_green)
)
print(boundary)
log('Notice: update_alias_green: {}', update_alias_green)
print(boundary)
else:
create_alias_green = client_lambda.create_alias(
FunctionName=function_name,
Name=version_name_green,
FunctionVersion=version_green,
Description='created {} at version {} (green)'.format(function_name, version_green)
)
print(boundary)
log('Notice: create_alias_green: {}', create_alias_green)
print(boundary)
break
#
# debug: optional helpful debug statements
#
if debug_status:
print(boundary)
log('version_blue: {}', version_blue)
log('version_green: {}', version_green)
log('method_http_method: {}', method_http_method)
log('method_response_http_method: {}', method_response_http_method)
log('integration_uri: {}', integration_uri)
log('integration_http_method: {}', integration_http_method)
log('put_integration_credentials: {}', put_integration_credentials)
log('http_method_common: {}', http_method_common)
log('restApiId: {}', api_id)
log('resourceId: {}', root_id)
log('statusCode: {}', integration_response_status_code)
log('integration_response_template: {}', integration_response_template)
log('canary_percent_traffic: {}', canary_percent_traffic)
log('use_stage_cache: {}', use_stage_cache)
log('tracing_enabled: {}', tracing_enabled)
log('source_arn: {}', source_arn)
log('aliases: {}', aliases)
print(boundary)
#
# api gateway: required step to give source_arn permission to invoke lambda
#
add_permission(function_name, version_latest, source_arn, type='latest')
add_permission(function_name, version_blue, source_arn, type=version_name_blue)
add_permission(function_name, version_green, source_arn, type=version_name_green)
#
# method: add method to an existing resource
#
try:
method = client_api.put_method(
restApiId=api_id,
resourceId=root_id,
httpMethod=method_http_method,
authorizationType=method_authorization_type
)
put_method = True
time.sleep(10)
if debug_status:
print(boundary)
log('method: {}', method)
print(boundary)
except ClientError as e:
log('Error: executing client_api.put_method(..): {}', str(e))
put_method = False
#
# integration: set up a method's integration
#
# @httpMethod, between client and APIG
# @integrationHttpMethod, between APIG and backend. This is not required for
# lambda integration (but if defined needs to be POST), and required for
# HTTP or AWS
#
try:
if put_integration_credentials:
method_integration = client_api.put_integration(
credentials=put_integration_credentials,
restApiId=api_id,
resourceId=root_id,
httpMethod=http_method_common,
type=integration_type,
uri='{}:${{stageVariables.env}}/invocations'.format(integration_uri),
integrationHttpMethod=integration_http_method
)
else:
method_integration = client_api.put_integration(
restApiId=api_id,
resourceId=root_id,
httpMethod=http_method_common,
type=integration_type,
uri='{}:${{stageVariables.env}}/invocations'.format(integration_uri),
integrationHttpMethod=integration_http_method
)
put_integration = True
time.sleep(5)
if debug_status:
print(boundary)
log('method_integration: {}', method_integration)
print(boundary)
except ClientError as e:
log('Error: executing client_api.put_integration(..): {}', str(e))
put_integration = False
#
# method response: add MethodResponse to an existing method resource
#
try:
method_response = client_api.put_method_response(
restApiId=api_id,
resourceId=root_id,
httpMethod=method_response_http_method,
statusCode=method_response_status_code,
responseParameters={
'method.response.header.Access-Control-Allow-Origin': True
},
responseModels=json.loads(
method_response_models
) if isinstance(method_response_models, str) else method_response_models
)
put_method_response = True
time.sleep(5)
if debug_status:
print(boundary)
log('method_response: {}', method_response)
print(boundary)
except ClientError as e:
log('Error: executing client_api.put_method_response(..): {}', str(e))
put_method_response = False
#
# integration response: represents a put integration
#
# @responseParameters, enables CORS when the header access is defined via
# 'method.response.header.Access-Control-Allow-Origin'
#
try:
integration_response = client_api.put_integration_response(
restApiId=api_id,
resourceId=root_id,
httpMethod=http_method_common,
statusCode=integration_response_status_code,
responseTemplates=integration_response_template,
responseParameters={
'method.response.header.Access-Control-Allow-Origin': '\'*\''
}
)
put_integration_response = True
time.sleep(5)
if debug_status:
print(boundary)
log('integration_response: {}', integration_response)
print(boundary)
except ClientError as e:
log('Error: executing client_api.put_integration_response(..): {}', str(e))
put_integration_response = False
#
# base deployment: primary stage (equivalent to blue/stable stage)
#
try:
stage = client_api.get_stage(restApiId=api_id, stageName=stage_name)
except ClientError as e:
print(boundary)
log("Notice: executing client_api.get_stage(..): {}", str(e))
print(boundary)
stage = None
create_deployment = False
if not (stage and 'canarySettings' in stage):
try:
response_create_deployment = client_api.create_deployment(
restApiId=api_id,
stageName=stage_name,
stageDescription='primary stage',
description='base deployment',
tracingEnabled=tracing_enabled,
variables={
'env': version_name_blue
}
)
create_deployment = True
time.sleep(30) # avoid too many requests
if debug_status:
print(boundary)
log('response_create_deployment: {}', response_create_deployment)
print(boundary)
except ClientError as e:
print(boundary)
log("Notice: executing client_api.create_deployment(..): {}", str(e))
print(boundary)
create_deployment = False
#
# canary deployment: new stage (equivalent to green/new stage)
#
# @variables, inheritted from the base deployment configuration
# @tracingEnabled, inheritted from the base deployment configuration
#
try:
response_create_deployment_canary = client_api.create_deployment(
restApiId=api_id,
stageName=stage_name,
stageDescription='canary stage',
description='canary deployment',
canarySettings={
'percentTraffic': canary_percent_traffic,
'stageVariableOverrides': {
'env': version_name_green
},
'useStageCache': use_stage_cache
}
)
create_deployment_canary = True
if debug_status:
print(boundary)
log('response_create_deployment_canary: {}', response_create_deployment_canary)
print(boundary)
except ClientError as e:
print(boundary)
log("Error: executing client_api.create_deployment(..): {}", str(e))
print(boundary)
create_deployment_canary = False
#
# return condition: lambda invoked by cloudformation
#
if 'StackId' in event:
responseUrl = event['ResponseURL']
log(responseUrl)
responseBody = {}
responseBody['Status'] = 'SUCCESS'
responseBody['Reason'] = '{a}: {b}'.format(
a='See the details in CloudWatch Log Stream',
b=context.log_stream_name
)
responseBody['PhysicalResourceId'] = physicalResourceId or context.log_stream_name
responseBody['StackId'] = event['StackId']
responseBody['RequestId'] = event['RequestId']
responseBody['LogicalResourceId'] = event['LogicalResourceId']
responseBody['NoEcho'] = noEcho
responseBody['Data'] = {
'put_method': put_method,
'put_method_response': put_method_response,
'put_integration': put_integration,
'put_integration_response': put_integration_response,
'create_deployment': create_deployment,
'create_deployment_canary': create_deployment_canary
}
json_responseBody = json.dumps(responseBody)
log('Response body:\n{}', json_responseBody)
headers = {
'content-type': '',
'content-length': str(len(json_responseBody))
}
try:
response = requests.put(
responseUrl,
data=json_responseBody,
headers=headers
)
log('Status code: {}', response.reason)
except Exception as e:
log('send(..) failed executing requests.put(..): {}', str(e))
#
# return condition: lambda invoked by something else
#
else:
return {
'put_method': put_method,
'put_method_response': put_method_response,
'put_integration': put_integration,
'put_integration_response': put_integration_response,
'create_deployment': create_deployment,
'create_deployment_canary': create_deployment_canary
}
if __name__ == '__main__':
lambda_handler()