Skip to content

Commit 1104f81

Browse files
committed
Merge branch 'master' of github.com:nficano/python-lambda
* 'master' of github.com:nficano/python-lambda: Should address issue nficano#38. The calls to determine whether or not a function exists wasn't calling multiple times if you have more than 50 lambda functions configured. For those users who have lots of functions defined, the deploy will try to re-create an existing lambda function Found some holes in error handling when the config.yaml properties aren't set for the build, build/source_directories properties. We now handle these cases correctly and default to the existing behavior if the values are missing. Related to issue nficano#64 "Subdirectories not included in zip package". Revert "Related to issue nficano#64 "Subdirectories not included in zip package"." Related to issue nficano#64 "Subdirectories not included in zip package".
2 parents a635dc7 + 3c677aa commit 1104f81

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

aws_lambda/aws_lambda.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
import time
1010
from imp import load_source
1111
from shutil import copy
12-
from shutil import copyfile
12+
from shutil import copyfile, copytree
1313
from tempfile import mkdtemp
14+
from collections import defaultdict
1415

1516
import boto3
1617
import botocore
@@ -246,24 +247,36 @@ def build(src, requirements=False, local_package=None):
246247
else output_filename
247248
)
248249

250+
# Allow definition of source code directories we want to build into our zipped package.
251+
build_config = defaultdict(**cfg.get('build', {}))
252+
build_source_directories = build_config.get('source_directories', '')
253+
build_source_directories = build_source_directories if build_source_directories is not None else ''
254+
source_directories = [d.strip() for d in build_source_directories.split(',')]
255+
249256
files = []
250257
for filename in os.listdir(src):
251258
if os.path.isfile(filename):
252259
if filename == '.DS_Store':
253260
continue
254261
if filename == 'config.yaml':
255262
continue
256-
# TODO: Check subdirectories for '.DS_Store' files
257-
print('Bundling: %r' % filename)
258-
files.append(os.path.join(src, filename))
263+
print('Bundling: %r' % filename)
264+
files.append(os.path.join(src, filename))
265+
elif os.path.isdir(filename) and filename in source_directories:
266+
print('Bundling directory: %r' % filename)
267+
files.append(os.path.join(src, filename))
259268

260269
# "cd" into `temp_path` directory.
261270
os.chdir(path_to_temp)
262271
for f in files:
263-
_, filename = os.path.split(f)
272+
if os.path.isfile(f):
273+
_, filename = os.path.split(f)
264274

265-
# Copy handler file into root of the packages folder.
266-
copyfile(f, os.path.join(path_to_temp, filename))
275+
# Copy handler file into root of the packages folder.
276+
copyfile(f, os.path.join(path_to_temp, filename))
277+
elif os.path.isdir(f):
278+
destination_folder = os.path.join(path_to_temp, f[len(src) + 1:])
279+
copytree(f, destination_folder)
267280

268281
# Zip them together into a single file.
269282
# TODO: Delete temp directory created once the archive has been compiled.
@@ -527,8 +540,13 @@ def function_exists(cfg, function_name):
527540
'lambda', aws_access_key_id, aws_secret_access_key,
528541
cfg.get('region'),
529542
)
530-
functions = client.list_functions().get('Functions', [])
531-
for fn in functions:
532-
if fn.get('FunctionName') == function_name:
533-
return True
534-
return False
543+
544+
# Need to loop through until we get all of the lambda functions returned. It appears
545+
# to be only returning 50 functions at a time.
546+
functions = []
547+
functions_resp = client.list_functions()
548+
functions.extend([f['FunctionName'] for f in functions_resp.get('Functions', [])])
549+
while('NextMarker' in functions_resp):
550+
functions_resp = client.list_functions(Marker=functions_resp.get('NextMarker'))
551+
functions.extend([f['FunctionName'] for f in functions_resp.get('Functions', [])])
552+
return function_name in functions

aws_lambda/project_templates/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ aws_secret_access_key:
2525
environment_variables:
2626
env_1: foo
2727
env_2: baz
28+
29+
# Build options
30+
build:
31+
source_directories: lib # a comma delimited list of directories in your project root that contains source to package.

0 commit comments

Comments
 (0)