Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions aws_lambda/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import time
from imp import load_source
from shutil import copy
from shutil import copyfile
from shutil import copyfile, copytree
from tempfile import mkdtemp
from collections import defaultdict

import boto3
import botocore
Expand Down Expand Up @@ -242,24 +243,36 @@ def build(src, requirements=False, local_package=None):
else output_filename
)

# Allow definition of source code directories we want to build into our zipped package.
build_config = defaultdict(**cfg.get('build', {}))
build_source_directories = build_config.get('source_directories', '')
build_source_directories = build_source_directories if build_source_directories is not None else ''
source_directories = [d.strip() for d in build_source_directories.split(',')]

files = []
for filename in os.listdir(src):
if os.path.isfile(filename):
if filename == '.DS_Store':
continue
if filename == 'config.yaml':
continue
# TODO: Check subdirectories for '.DS_Store' files
print('Bundling: %r' % filename)
files.append(os.path.join(src, filename))
print('Bundling: %r' % filename)
files.append(os.path.join(src, filename))
elif os.path.isdir(filename) and filename in source_directories:
print('Bundling directory: %r' % filename)
files.append(os.path.join(src, filename))

# "cd" into `temp_path` directory.
os.chdir(path_to_temp)
for f in files:
_, filename = os.path.split(f)
if os.path.isfile(f):
_, filename = os.path.split(f)

# Copy handler file into root of the packages folder.
copyfile(f, os.path.join(path_to_temp, filename))
# Copy handler file into root of the packages folder.
copyfile(f, os.path.join(path_to_temp, filename))
elif os.path.isdir(f):
destination_folder = os.path.join(path_to_temp, f[len(src) + 1:])
copytree(f, destination_folder)

# Zip them together into a single file.
# TODO: Delete temp directory created once the archive has been compiled.
Expand Down Expand Up @@ -522,8 +535,13 @@ def function_exists(cfg, function_name):
'lambda', aws_access_key_id, aws_secret_access_key,
cfg.get('region'),
)
functions = client.list_functions().get('Functions', [])
for fn in functions:
if fn.get('FunctionName') == function_name:
return True
return False

# Need to loop through until we get all of the lambda functions returned. It appears
# to be only returning 50 functions at a time.
functions = []
functions_resp = client.list_functions()
functions.extend([f['FunctionName'] for f in functions_resp.get('Functions', [])])
while('NextMarker' in functions_resp):
functions_resp = client.list_functions(Marker=functions_resp.get('NextMarker'))
functions.extend([f['FunctionName'] for f in functions_resp.get('Functions', [])])
return function_name in functions
4 changes: 4 additions & 0 deletions aws_lambda/project_templates/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ aws_secret_access_key:
environment_variables:
env_1: foo
env_2: baz

# Build options
build:
source_directories: lib # a comma delimited list of directories in your project root that contains source to package.