Skip to content
This repository was archived by the owner on Mar 15, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add a config_file flag so it doesn't have to be ./config.yaml
  • Loading branch information
johnclyde committed Oct 8, 2017
commit 5bce35b064a9d03f3c96802e62465f68c828a1e8
31 changes: 14 additions & 17 deletions aws_lambda/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
log = logging.getLogger(__name__)


def cleanup_old_versions(src, keep_last_versions):
def cleanup_old_versions(src, keep_last_versions, config_file='config.yaml'):
"""Deletes old deployed versions of the function in AWS Lambda.

Won't delete $Latest and any aliased version
Expand All @@ -47,7 +47,7 @@ def cleanup_old_versions(src, keep_last_versions):
if keep_last_versions <= 0:
print("Won't delete all versions. Please do this manually")
else:
path_to_config_file = os.path.join(src, 'config.yaml')
path_to_config_file = os.path.join(src, config_file)
cfg = read(path_to_config_file, loader=yaml.load)

aws_access_key_id = cfg.get('aws_access_key_id')
Expand Down Expand Up @@ -78,7 +78,7 @@ def cleanup_old_versions(src, keep_last_versions):
.format(version_number, e.message))


def deploy(src, requirements=False, local_package=None):
def deploy(src, config_file='config.yaml', requirements=False, local_package=None):
"""Deploys a new function to AWS Lambda.

:param str src:
Expand All @@ -89,7 +89,7 @@ def deploy(src, requirements=False, local_package=None):
well (and/or is not available on PyPi)
"""
# Load and parse the config file.
path_to_config_file = os.path.join(src, 'config.yaml')
path_to_config_file = os.path.join(src, config_file)
cfg = read(path_to_config_file, loader=yaml.load)

# Copy all the pip dependencies required to run your code into a temporary
Expand All @@ -104,7 +104,7 @@ def deploy(src, requirements=False, local_package=None):
create_function(cfg, path_to_zip_file)


def deploy_s3(src, requirements=False, local_package=None):
def deploy_s3(src, config_file='config.yaml', requirements=False, local_package=None):
"""Deploys a new function via AWS S3.

:param str src:
Expand All @@ -115,7 +115,7 @@ def deploy_s3(src, requirements=False, local_package=None):
well (and/or is not available on PyPi)
"""
# Load and parse the config file.
path_to_config_file = os.path.join(src, 'config.yaml')
path_to_config_file = os.path.join(src, config_file)
cfg = read(path_to_config_file, loader=yaml.load)

# Copy all the pip dependencies required to run your code into a temporary
Expand All @@ -132,7 +132,7 @@ def deploy_s3(src, requirements=False, local_package=None):
create_function(cfg, path_to_zip_file, use_s3, s3_file)


def upload(src, requirements=False, local_package=None):
def upload(src, config_file='config.yaml', requirements=False, local_package=None):
"""Uploads a new function to AWS S3.

:param str src:
Expand All @@ -143,7 +143,7 @@ def upload(src, requirements=False, local_package=None):
well (and/or is not available on PyPi)
"""
# Load and parse the config file.
path_to_config_file = os.path.join(src, 'config.yaml')
path_to_config_file = os.path.join(src, config_file)
cfg = read(path_to_config_file, loader=yaml.load)

# Copy all the pip dependencies required to run your code into a temporary
Expand All @@ -155,7 +155,7 @@ def upload(src, requirements=False, local_package=None):
upload_s3(cfg, path_to_zip_file)


def invoke(src, alt_event=None, verbose=False):
def invoke(src, event_file='event.json', config_file='config.yaml', verbose=False):
"""Simulates a call to your function.

:param str src:
Expand All @@ -167,7 +167,7 @@ def invoke(src, alt_event=None, verbose=False):
Whether to print out verbose details.
"""
# Load and parse the config file.
path_to_config_file = os.path.join(src, 'config.yaml')
path_to_config_file = os.path.join(src, config_file)
cfg = read(path_to_config_file, loader=yaml.load)

# Load environment variables from the config file into the actual
Expand All @@ -178,10 +178,7 @@ def invoke(src, alt_event=None, verbose=False):
os.environ[key] = value

# Load and parse event file.
if alt_event:
path_to_event_file = os.path.join(src, alt_event)
else:
path_to_event_file = os.path.join(src, 'event.json')
path_to_event_file = os.path.join(src, event_file)
event = read(path_to_event_file, loader=json.loads)

# Tweak to allow module to import local modules
Expand Down Expand Up @@ -229,7 +226,7 @@ def init(src, minimal=False):
copy(dest_path, src)


def build(src, requirements=False, local_package=None):
def build(src, config_file='config.yaml', requirements=False, local_package=None):
"""Builds the file bundle.

:param str src:
Expand All @@ -240,7 +237,7 @@ def build(src, requirements=False, local_package=None):
well (and/or is not available on PyPi)
"""
# Load and parse the config file.
path_to_config_file = os.path.join(src, 'config.yaml')
path_to_config_file = os.path.join(src, config_file)
cfg = read(path_to_config_file, loader=yaml.load)

# Get the absolute path to the output directory and create it if it doesn't
Expand Down Expand Up @@ -296,7 +293,7 @@ def build(src, requirements=False, local_package=None):
if os.path.isfile(filename):
if filename == '.DS_Store':
continue
if filename == 'config.yaml':
if filename == config_file:
continue
print('Bundling: %r' % filename)
files.append(os.path.join(src, filename))
Expand Down
17 changes: 11 additions & 6 deletions scripts/lambda
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def init(folder, minimal):


@click.command(help='Bundles package for deployment.')
@click.option('--config-file', default=None, help='Alternate config file.')
@click.option(
'--use-requirements', default=False, is_flag=True,
help='Install all packages defined in requirements.txt',
Expand All @@ -43,17 +44,19 @@ def init(folder, minimal):
help='Install local package as well.', multiple=True,
)
def build(use_requirements, local_package):
aws_lambda.build(CURRENT_DIR, use_requirements, local_package)
aws_lambda.build(CURRENT_DIR, config_file=config_file, use_requirements, local_package)


@click.command(help='Run a local test of your function.')
@click.option('--event-file', default=None, help='Alternate event file.')
@click.option('--config-file', default=None, help='Alternate config file.')
@click.option('--verbose', '-v', is_flag=True)
def invoke(event_file, verbose):
aws_lambda.invoke(CURRENT_DIR, event_file, verbose)
def invoke(event_file, config_file, verbose):
aws_lambda.invoke(CURRENT_DIR, event_file=event_file, config_file=config_file, verbose)


@click.command(help='Register and deploy your code to lambda.')
@click.option('--config-file', default=None, help='Alternate config file.')
@click.option(
'--use-requirements', default=False, is_flag=True,
help='Install all packages defined in requirements.txt',
Expand All @@ -63,7 +66,7 @@ def invoke(event_file, verbose):
help='Install local package as well.', multiple=True,
)
def deploy(use_requirements, local_package):
aws_lambda.deploy(CURRENT_DIR, use_requirements, local_package)
aws_lambda.deploy(CURRENT_DIR, config_file=config_file, use_requirements, local_package)


@click.command(help='Upload your lambda to S3.')
Expand All @@ -79,15 +82,17 @@ def upload(use_requirements, local_package):
aws_lambda.upload(CURRENT_DIR, use_requirements, local_package)

@click.command(help="Deploy your lambda via S3.")
@click.option('--config-file', default=None, help='Alternate config file.')
@click.option('--use-requirements', default=False, is_flag=True, help='Install all packages defined in requirements.txt')
@click.option('--local-package', default=None, help='Install local package as well.', type=click.Path(), multiple=True)
def deploy_s3(use_requirements, local_package):
aws_lambda.deploy_s3(CURRENT_DIR, use_requirements, local_package)
aws_lambda.deploy_s3(CURRENT_DIR, config_file=config_file, use_requirements, local_package)

@click.command(help="Delete old versions of your functions")
@click.option('--config-file', default=None, help='Alternate config file.')
@click.option("--keep-last", type=int, prompt="Please enter the number of recent versions to keep")
def cleanup(keep_last):
aws_lambda.cleanup_old_versions(CURRENT_DIR, keep_last)
aws_lambda.cleanup_old_versions(CURRENT_DIR, keep_last, config_file=config_file)


if __name__ == '__main__':
Expand Down