Skip to content

Commit 5bce35b

Browse files
committed
Add a config_file flag so it doesn't have to be ./config.yaml
1 parent 3acf861 commit 5bce35b

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

aws_lambda/aws_lambda.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
log = logging.getLogger(__name__)
3434

3535

36-
def cleanup_old_versions(src, keep_last_versions):
36+
def cleanup_old_versions(src, keep_last_versions, config_file='config.yaml'):
3737
"""Deletes old deployed versions of the function in AWS Lambda.
3838
3939
Won't delete $Latest and any aliased version
@@ -47,7 +47,7 @@ def cleanup_old_versions(src, keep_last_versions):
4747
if keep_last_versions <= 0:
4848
print("Won't delete all versions. Please do this manually")
4949
else:
50-
path_to_config_file = os.path.join(src, 'config.yaml')
50+
path_to_config_file = os.path.join(src, config_file)
5151
cfg = read(path_to_config_file, loader=yaml.load)
5252

5353
aws_access_key_id = cfg.get('aws_access_key_id')
@@ -78,7 +78,7 @@ def cleanup_old_versions(src, keep_last_versions):
7878
.format(version_number, e.message))
7979

8080

81-
def deploy(src, requirements=False, local_package=None):
81+
def deploy(src, config_file='config.yaml', requirements=False, local_package=None):
8282
"""Deploys a new function to AWS Lambda.
8383
8484
:param str src:
@@ -89,7 +89,7 @@ def deploy(src, requirements=False, local_package=None):
8989
well (and/or is not available on PyPi)
9090
"""
9191
# Load and parse the config file.
92-
path_to_config_file = os.path.join(src, 'config.yaml')
92+
path_to_config_file = os.path.join(src, config_file)
9393
cfg = read(path_to_config_file, loader=yaml.load)
9494

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

106106

107-
def deploy_s3(src, requirements=False, local_package=None):
107+
def deploy_s3(src, config_file='config.yaml', requirements=False, local_package=None):
108108
"""Deploys a new function via AWS S3.
109109
110110
:param str src:
@@ -115,7 +115,7 @@ def deploy_s3(src, requirements=False, local_package=None):
115115
well (and/or is not available on PyPi)
116116
"""
117117
# Load and parse the config file.
118-
path_to_config_file = os.path.join(src, 'config.yaml')
118+
path_to_config_file = os.path.join(src, config_file)
119119
cfg = read(path_to_config_file, loader=yaml.load)
120120

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

134134

135-
def upload(src, requirements=False, local_package=None):
135+
def upload(src, config_file='config.yaml', requirements=False, local_package=None):
136136
"""Uploads a new function to AWS S3.
137137
138138
:param str src:
@@ -143,7 +143,7 @@ def upload(src, requirements=False, local_package=None):
143143
well (and/or is not available on PyPi)
144144
"""
145145
# Load and parse the config file.
146-
path_to_config_file = os.path.join(src, 'config.yaml')
146+
path_to_config_file = os.path.join(src, config_file)
147147
cfg = read(path_to_config_file, loader=yaml.load)
148148

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

157157

158-
def invoke(src, alt_event=None, verbose=False):
158+
def invoke(src, event_file='event.json', config_file='config.yaml', verbose=False):
159159
"""Simulates a call to your function.
160160
161161
:param str src:
@@ -167,7 +167,7 @@ def invoke(src, alt_event=None, verbose=False):
167167
Whether to print out verbose details.
168168
"""
169169
# Load and parse the config file.
170-
path_to_config_file = os.path.join(src, 'config.yaml')
170+
path_to_config_file = os.path.join(src, config_file)
171171
cfg = read(path_to_config_file, loader=yaml.load)
172172

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

180180
# Load and parse event file.
181-
if alt_event:
182-
path_to_event_file = os.path.join(src, alt_event)
183-
else:
184-
path_to_event_file = os.path.join(src, 'event.json')
181+
path_to_event_file = os.path.join(src, event_file)
185182
event = read(path_to_event_file, loader=json.loads)
186183

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

231228

232-
def build(src, requirements=False, local_package=None):
229+
def build(src, config_file='config.yaml', requirements=False, local_package=None):
233230
"""Builds the file bundle.
234231
235232
:param str src:
@@ -240,7 +237,7 @@ def build(src, requirements=False, local_package=None):
240237
well (and/or is not available on PyPi)
241238
"""
242239
# Load and parse the config file.
243-
path_to_config_file = os.path.join(src, 'config.yaml')
240+
path_to_config_file = os.path.join(src, config_file)
244241
cfg = read(path_to_config_file, loader=yaml.load)
245242

246243
# Get the absolute path to the output directory and create it if it doesn't
@@ -296,7 +293,7 @@ def build(src, requirements=False, local_package=None):
296293
if os.path.isfile(filename):
297294
if filename == '.DS_Store':
298295
continue
299-
if filename == 'config.yaml':
296+
if filename == config_file:
300297
continue
301298
print('Bundling: %r' % filename)
302299
files.append(os.path.join(src, filename))

scripts/lambda

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def init(folder, minimal):
3434

3535

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

4849

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

5557

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

6871

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

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

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

9297

9398
if __name__ == '__main__':

0 commit comments

Comments
 (0)