1
1
# -*- coding: utf-8 -*-
2
2
from __future__ import print_function
3
3
4
+ import hashlib
4
5
import json
5
6
import logging
6
7
import os
15
16
import botocore
16
17
import pip
17
18
import yaml
18
- import hashlib
19
19
20
20
from .helpers import archive
21
+ from .helpers import get_environment_variable_value
21
22
from .helpers import mkdir
22
23
from .helpers import read
23
24
from .helpers import timestamp
24
- from .helpers import get_environment_variable_value
25
25
26
26
27
27
log = logging .getLogger (__name__ )
@@ -47,11 +47,13 @@ def cleanup_old_versions(src, keep_last_versions):
47
47
aws_access_key_id = cfg .get ('aws_access_key_id' )
48
48
aws_secret_access_key = cfg .get ('aws_secret_access_key' )
49
49
50
- client = get_client ('lambda' , aws_access_key_id , aws_secret_access_key ,
51
- cfg .get ('region' ))
50
+ client = get_client (
51
+ 'lambda' , aws_access_key_id , aws_secret_access_key ,
52
+ cfg .get ('region' ),
53
+ )
52
54
53
55
response = client .list_versions_by_function (
54
- FunctionName = cfg .get ('function_name' )
56
+ FunctionName = cfg .get ('function_name' ),
55
57
)
56
58
versions = response .get ('Versions' )
57
59
if len (response .get ('Versions' )) < keep_last_versions :
@@ -63,7 +65,7 @@ def cleanup_old_versions(src, keep_last_versions):
63
65
try :
64
66
client .delete_function (
65
67
FunctionName = cfg .get ('function_name' ),
66
- Qualifier = version_number
68
+ Qualifier = version_number ,
67
69
)
68
70
except botocore .exceptions .ClientError as e :
69
71
print ('Skipping Version {}: {}'
@@ -95,6 +97,7 @@ def deploy(src, requirements=False, local_package=None):
95
97
else :
96
98
create_function (cfg , path_to_zip_file )
97
99
100
+
98
101
def upload (src , requirements = False , local_package = None ):
99
102
"""Uploads a new function to AWS S3.
100
103
@@ -117,6 +120,7 @@ def upload(src, requirements=False, local_package=None):
117
120
118
121
upload_s3 (cfg , path_to_zip_file )
119
122
123
+
120
124
def invoke (src , alt_event = None , verbose = False ):
121
125
"""Simulates a call to your function.
122
126
@@ -132,7 +136,8 @@ def invoke(src, alt_event=None, verbose=False):
132
136
path_to_config_file = os .path .join (src , 'config.yaml' )
133
137
cfg = read (path_to_config_file , loader = yaml .load )
134
138
135
- # Load environment variables from the config file into the actual environment.
139
+ # Load environment variables from the config file into the actual
140
+ # environment.
136
141
for key , value in cfg .get ('environment_variables' ).items ():
137
142
os .environ [key ] = value
138
143
@@ -177,7 +182,8 @@ def init(src, minimal=False):
177
182
"""
178
183
179
184
templates_path = os .path .join (
180
- os .path .dirname (os .path .abspath (__file__ )), 'project_templates' )
185
+ os .path .dirname (os .path .abspath (__file__ )), 'project_templates' ,
186
+ )
181
187
for filename in os .listdir (templates_path ):
182
188
if (minimal and filename == 'event.json' ) or filename .endswith ('.pyc' ):
183
189
continue
@@ -213,22 +219,28 @@ def build(src, requirements=False, local_package=None):
213
219
output_filename = '{0}-{1}.zip' .format (timestamp (), function_name )
214
220
215
221
path_to_temp = mkdtemp (prefix = 'aws-lambda' )
216
- pip_install_to_target (path_to_temp ,
217
- requirements = requirements ,
218
- local_package = local_package )
222
+ pip_install_to_target (
223
+ path_to_temp ,
224
+ requirements = requirements ,
225
+ local_package = local_package ,
226
+ )
219
227
220
228
# Hack for Zope.
221
229
if 'zope' in os .listdir (path_to_temp ):
222
- print ('Zope packages detected; fixing Zope package paths to '
223
- 'make them importable.' )
230
+ print (
231
+ 'Zope packages detected; fixing Zope package paths to '
232
+ 'make them importable.' ,
233
+ )
224
234
# Touch.
225
235
with open (os .path .join (path_to_temp , 'zope/__init__.py' ), 'wb' ):
226
236
pass
227
237
228
238
# Gracefully handle whether ".zip" was included in the filename or not.
229
- output_filename = ('{0}.zip' .format (output_filename )
230
- if not output_filename .endswith ('.zip' )
231
- else output_filename )
239
+ output_filename = (
240
+ '{0}.zip' .format (output_filename )
241
+ if not output_filename .endswith ('.zip' )
242
+ else output_filename
243
+ )
232
244
233
245
files = []
234
246
for filename in os .listdir (src ):
@@ -364,7 +376,7 @@ def get_client(client, aws_access_key_id, aws_secret_access_key, region=None):
364
376
client ,
365
377
aws_access_key_id = aws_access_key_id ,
366
378
aws_secret_access_key = aws_secret_access_key ,
367
- region_name = region
379
+ region_name = region ,
368
380
)
369
381
370
382
@@ -379,8 +391,10 @@ def create_function(cfg, path_to_zip_file):
379
391
account_id = get_account_id (aws_access_key_id , aws_secret_access_key )
380
392
role = get_role_name (account_id , cfg .get ('role' , 'lambda_basic_execution' ))
381
393
382
- client = get_client ('lambda' , aws_access_key_id , aws_secret_access_key ,
383
- cfg .get ('region' ))
394
+ client = get_client (
395
+ 'lambda' , aws_access_key_id , aws_secret_access_key ,
396
+ cfg .get ('region' ),
397
+ )
384
398
385
399
# Do we prefer development variable over config?
386
400
func_name = (
@@ -396,7 +410,7 @@ def create_function(cfg, path_to_zip_file):
396
410
'Description' : cfg .get ('description' ),
397
411
'Timeout' : cfg .get ('timeout' , 15 ),
398
412
'MemorySize' : cfg .get ('memory_size' , 512 ),
399
- 'Publish' : True
413
+ 'Publish' : True ,
400
414
}
401
415
402
416
if 'environment_variables' in cfg :
@@ -406,8 +420,8 @@ def create_function(cfg, path_to_zip_file):
406
420
key : get_environment_variable_value (value )
407
421
for key , value
408
422
in cfg .get ('environment_variables' ).items ()
409
- }
410
- }
423
+ },
424
+ },
411
425
)
412
426
413
427
client .create_function (** kwargs )
@@ -424,13 +438,15 @@ def update_function(cfg, path_to_zip_file):
424
438
account_id = get_account_id (aws_access_key_id , aws_secret_access_key )
425
439
role = get_role_name (account_id , cfg .get ('role' , 'lambda_basic_execution' ))
426
440
427
- client = get_client ('lambda' , aws_access_key_id , aws_secret_access_key ,
428
- cfg .get ('region' ))
441
+ client = get_client (
442
+ 'lambda' , aws_access_key_id , aws_secret_access_key ,
443
+ cfg .get ('region' ),
444
+ )
429
445
430
446
client .update_function_code (
431
447
FunctionName = cfg .get ('function_name' ),
432
448
ZipFile = byte_stream ,
433
- Publish = True
449
+ Publish = True ,
434
450
)
435
451
436
452
kwargs = {
@@ -442,8 +458,8 @@ def update_function(cfg, path_to_zip_file):
442
458
'MemorySize' : cfg .get ('memory_size' , 512 ),
443
459
'VpcConfig' : {
444
460
'SubnetIds' : cfg .get ('subnet_ids' , []),
445
- 'SecurityGroupIds' : cfg .get ('security_group_ids' , [])
446
- }
461
+ 'SecurityGroupIds' : cfg .get ('security_group_ids' , []),
462
+ },
447
463
}
448
464
449
465
if 'environment_variables' in cfg :
@@ -453,29 +469,32 @@ def update_function(cfg, path_to_zip_file):
453
469
key : get_environment_variable_value (value )
454
470
for key , value
455
471
in cfg .get ('environment_variables' ).items ()
456
- }
457
- }
472
+ },
473
+ },
458
474
)
459
475
460
476
client .update_function_configuration (** kwargs )
461
477
478
+
462
479
def upload_s3 (cfg , path_to_zip_file ):
463
480
"""Upload a function to AWS S3."""
464
481
465
482
print ('Uploading your new Lambda function' )
466
483
aws_access_key_id = cfg .get ('aws_access_key_id' )
467
484
aws_secret_access_key = cfg .get ('aws_secret_access_key' )
468
- account_id = get_account_id ( aws_access_key_id , aws_secret_access_key )
469
- client = get_client ( 's3' , aws_access_key_id , aws_secret_access_key ,
470
- cfg .get ('region' ))
471
- role = get_role_name ( account_id , cfg . get ( 'role' , 'basic_s3_upload' ) )
485
+ client = get_client (
486
+ 's3' , aws_access_key_id , aws_secret_access_key ,
487
+ cfg .get ('region' ),
488
+ )
472
489
byte_stream = b''
473
490
with open (path_to_zip_file , mode = 'rb' ) as fh :
474
491
byte_stream = fh .read ()
475
492
s3_key_prefix = cfg .get ('s3_key_prefix' , '/dist' )
476
493
checksum = hashlib .new ('md5' , byte_stream ).hexdigest ()
477
494
timestamp = str (time .time ())
478
- filename = '{prefix}{checksum}-{ts}.zip' .format (prefix = s3_key_prefix , checksum = checksum , ts = timestamp )
495
+ filename = '{prefix}{checksum}-{ts}.zip' .format (
496
+ prefix = s3_key_prefix , checksum = checksum , ts = timestamp ,
497
+ )
479
498
480
499
# Do we prefer development variable over config?
481
500
buck_name = (
@@ -487,19 +506,22 @@ def upload_s3(cfg, path_to_zip_file):
487
506
kwargs = {
488
507
'Bucket' : '{}' .format (buck_name ),
489
508
'Key' : '{}' .format (filename ),
490
- 'Body' : byte_stream
509
+ 'Body' : byte_stream ,
491
510
}
492
511
493
512
client .put_object (** kwargs )
494
513
print ('Finished uploading {} to S3 bucket {}' .format (func_name , buck_name ))
495
514
515
+
496
516
def function_exists (cfg , function_name ):
497
517
"""Check whether a function exists or not"""
498
518
499
519
aws_access_key_id = cfg .get ('aws_access_key_id' )
500
520
aws_secret_access_key = cfg .get ('aws_secret_access_key' )
501
- client = get_client ('lambda' , aws_access_key_id , aws_secret_access_key ,
502
- cfg .get ('region' ))
521
+ client = get_client (
522
+ 'lambda' , aws_access_key_id , aws_secret_access_key ,
523
+ cfg .get ('region' ),
524
+ )
503
525
functions = client .list_functions ().get ('Functions' , [])
504
526
for fn in functions :
505
527
if fn .get ('FunctionName' ) == function_name :
0 commit comments