Skip to content

Commit 2d80bcd

Browse files
committed
adding various changes based on review feedback
1 parent 8352d02 commit 2d80bcd

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

aws_lambda/aws_lambda.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import botocore
1616
import pip
1717
import yaml
18+
import md5
1819

1920
from .helpers import archive
2021
from .helpers import mkdir
@@ -457,15 +458,20 @@ def upload_s3(cfg, path_to_zip_file):
457458
"""Upload a function to AWS S3."""
458459

459460
print('Uploading your new Lambda function')
460-
byte_stream = read(path_to_zip_file, binary_file=True)
461461
aws_access_key_id = cfg.get('aws_access_key_id')
462462
aws_secret_access_key = cfg.get('aws_secret_access_key')
463-
464463
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-
467464
client = get_client('s3', aws_access_key_id, aws_secret_access_key,
468465
cfg.get('region'))
466+
role = get_role_name(account_id, cfg.get('role', 'basic_s3_upload'))
467+
byte_stream = b''
468+
with open(path_to_zip_file, mode='rb') as fh:
469+
byte_stream = fh.read()
470+
s3_key_prefix = cfg.get('s3_key_prefix', '/dist')
471+
checksum = md5.new(byte_stream).hexdigest()
472+
timestamp = str(time.time())
473+
filename = '{prefix}{checksum}-{ts}.zip'.format(prefix=s3_key_prefix, checksum=checksum, ts=timestamp)
474+
print(filename)
469475

470476
# Do we prefer development variable over config?
471477
buck_name = (
@@ -476,7 +482,7 @@ def upload_s3(cfg, path_to_zip_file):
476482
)
477483
kwargs = {
478484
'Bucket': '{}'.format(buck_name),
479-
'Key': cfg.get('s3_key', '{}.zip'.format(func_name)),
485+
'Key': '{}'.format(filename),
480486
'Body': byte_stream
481487
}
482488

aws_lambda/project_templates/config.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ region: us-east-1
22

33
function_name: my_lambda_function
44
handler: service.handler
5-
# role: lambda_basic_execution
6-
# bucket_name: my_s3_bucket
7-
# s3_key: 'path/to/file.zip'
85
description: My first lambda function
96
runtime: python2.7
7+
# role: lambda_basic_execution
8+
9+
# S3 upload requires appropriate role with s3:PutObject permission
10+
# (ex. basic_s3_upload), a destination bucket, and the key prefix
11+
# bucket_name: 'example-bucket'
12+
# s3_key_prefix: 'path/to/file/'
1013

1114
# if access key and secret are left blank, boto will use the credentials
1215
# defined in the [default] section of ~/.aws/credentials.

0 commit comments

Comments
 (0)