@@ -94,6 +94,27 @@ def deploy(src, requirements=False, local_package=None):
94
94
else :
95
95
create_function (cfg , path_to_zip_file )
96
96
97
+ def upload (src , requirements = False , local_package = None ):
98
+ """Uploads a new function to AWS S3.
99
+
100
+ :param str src:
101
+ The path to your Lambda ready project (folder must contain a valid
102
+ config.yaml and handler module (e.g.: service.py).
103
+ :param str local_package:
104
+ The path to a local package with should be included in the deploy as
105
+ well (and/or is not available on PyPi)
106
+ """
107
+ # Load and parse the config file.
108
+ path_to_config_file = os .path .join (src , 'config.yaml' )
109
+ cfg = read (path_to_config_file , loader = yaml .load )
110
+
111
+ # Copy all the pip dependencies required to run your code into a temporary
112
+ # folder then add the handler file in the root of this directory.
113
+ # Zip the contents of this folder into a single file and output to the dist
114
+ # directory.
115
+ path_to_zip_file = build (src , requirements , local_package )
116
+
117
+ upload_s3 (cfg , path_to_zip_file )
97
118
98
119
def invoke (src , alt_event = None , verbose = False ):
99
120
"""Simulates a call to your function.
@@ -432,6 +453,36 @@ def update_function(cfg, path_to_zip_file):
432
453
433
454
client .update_function_configuration (** kwargs )
434
455
456
+ def upload_s3 (cfg , path_to_zip_file ):
457
+ """Upload a function to AWS S3."""
458
+
459
+ print ('Uploading your new Lambda function' )
460
+ byte_stream = read (path_to_zip_file , binary_file = True )
461
+ aws_access_key_id = cfg .get ('aws_access_key_id' )
462
+ aws_secret_access_key = cfg .get ('aws_secret_access_key' )
463
+
464
+ account_id = get_account_id (aws_access_key_id , aws_secret_access_key )
465
+ role = get_role_name (account_id , cfg .get ('role' , 'lambda_basic_execution' ))
466
+
467
+ client = get_client ('s3' , aws_access_key_id , aws_secret_access_key ,
468
+ cfg .get ('region' ))
469
+
470
+ # Do we prefer development variable over config?
471
+ buck_name = (
472
+ os .environ .get ('S3_BUCKET_NAME' ) or cfg .get ('bucket_name' )
473
+ )
474
+ func_name = (
475
+ os .environ .get ('LAMBDA_FUNCTION_NAME' ) or cfg .get ('function_name' )
476
+ )
477
+ kwargs = {
478
+ 'Bucket' : cfg .get ('bucket_name' ),
479
+ 'Key' : cfg .get ('s3_key' , '{}' .format (func_name )),
480
+ 'Body' : {'ZipFile' : byte_stream }
481
+ }
482
+
483
+ client .put_object (** kwargs )
484
+ print ('Finished uploading {} to S3 bucket {}' .format (func_name , buck_name ))
485
+
435
486
436
487
def function_exists (cfg , function_name ):
437
488
"""Check whether a function exists or not"""
0 commit comments