Skip to content

Commit 9ff2c3e

Browse files
committed
adding function to upload to s3
1 parent 51782e9 commit 9ff2c3e

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

aws_lambda/aws_lambda.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,27 @@ def deploy(src, requirements=False, local_package=None):
9494
else:
9595
create_function(cfg, path_to_zip_file)
9696

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)
97118

98119
def invoke(src, alt_event=None, verbose=False):
99120
"""Simulates a call to your function.
@@ -432,6 +453,36 @@ def update_function(cfg, path_to_zip_file):
432453

433454
client.update_function_configuration(**kwargs)
434455

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+
435486

436487
def function_exists(cfg, function_name):
437488
"""Check whether a function exists or not"""

aws_lambda/project_templates/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ region: us-east-1
33
function_name: my_lambda_function
44
handler: service.handler
55
# role: lambda_basic_execution
6+
# bucket_name: my_s3_bucket
7+
# s3_key: 'path/to/file.zip'
68
description: My first lambda function
79
runtime: python2.7
810

0 commit comments

Comments
 (0)