Skip to content

Commit e5d583d

Browse files
committed
Merge branch 'master' of github.com:nficano/python-lambda
* 'master' of github.com:nficano/python-lambda: Patch up other invocations. Fix invocations of build(). Add a config_file flag so it doesn't have to be ./config.yaml
2 parents 3acf861 + 6347c4a commit e5d583d

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

aws_lambda/aws_lambda.py

Lines changed: 20 additions & 20 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,22 +89,23 @@ 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
9696
# folder then add the handler file in the root of this directory.
9797
# Zip the contents of this folder into a single file and output to the dist
9898
# directory.
99-
path_to_zip_file = build(src, requirements, local_package)
99+
path_to_zip_file = build(
100+
src, config_file=config_file, requirements=requirements, local_package=local_package)
100101

101102
if function_exists(cfg, cfg.get('function_name')):
102103
update_function(cfg, path_to_zip_file)
103104
else:
104105
create_function(cfg, path_to_zip_file)
105106

106107

107-
def deploy_s3(src, requirements=False, local_package=None):
108+
def deploy_s3(src, config_file='config.yaml', requirements=False, local_package=None):
108109
"""Deploys a new function via AWS S3.
109110
110111
:param str src:
@@ -115,14 +116,15 @@ def deploy_s3(src, requirements=False, local_package=None):
115116
well (and/or is not available on PyPi)
116117
"""
117118
# Load and parse the config file.
118-
path_to_config_file = os.path.join(src, 'config.yaml')
119+
path_to_config_file = os.path.join(src, config_file)
119120
cfg = read(path_to_config_file, loader=yaml.load)
120121

121122
# Copy all the pip dependencies required to run your code into a temporary
122123
# folder then add the handler file in the root of this directory.
123124
# Zip the contents of this folder into a single file and output to the dist
124125
# directory.
125-
path_to_zip_file = build(src, requirements, local_package)
126+
path_to_zip_file = build(
127+
src, config_file=config_file, requirements=requirements, local_package=local_package)
126128

127129
use_s3 = True
128130
s3_file = upload_s3(cfg, path_to_zip_file, use_s3)
@@ -132,7 +134,7 @@ def deploy_s3(src, requirements=False, local_package=None):
132134
create_function(cfg, path_to_zip_file, use_s3, s3_file)
133135

134136

135-
def upload(src, requirements=False, local_package=None):
137+
def upload(src, config_file='config.yaml', requirements=False, local_package=None):
136138
"""Uploads a new function to AWS S3.
137139
138140
:param str src:
@@ -143,19 +145,20 @@ def upload(src, requirements=False, local_package=None):
143145
well (and/or is not available on PyPi)
144146
"""
145147
# Load and parse the config file.
146-
path_to_config_file = os.path.join(src, 'config.yaml')
148+
path_to_config_file = os.path.join(src, config_file)
147149
cfg = read(path_to_config_file, loader=yaml.load)
148150

149151
# Copy all the pip dependencies required to run your code into a temporary
150152
# folder then add the handler file in the root of this directory.
151153
# Zip the contents of this folder into a single file and output to the dist
152154
# directory.
153-
path_to_zip_file = build(src, requirements, local_package)
155+
path_to_zip_file = build(
156+
src, config_file=config_file, requirements=requirements, local_package=local_package)
154157

155158
upload_s3(cfg, path_to_zip_file)
156159

157160

158-
def invoke(src, alt_event=None, verbose=False):
161+
def invoke(src, event_file='event.json', config_file='config.yaml', verbose=False):
159162
"""Simulates a call to your function.
160163
161164
:param str src:
@@ -167,7 +170,7 @@ def invoke(src, alt_event=None, verbose=False):
167170
Whether to print out verbose details.
168171
"""
169172
# Load and parse the config file.
170-
path_to_config_file = os.path.join(src, 'config.yaml')
173+
path_to_config_file = os.path.join(src, config_file)
171174
cfg = read(path_to_config_file, loader=yaml.load)
172175

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

180183
# 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')
184+
path_to_event_file = os.path.join(src, event_file)
185185
event = read(path_to_event_file, loader=json.loads)
186186

187187
# Tweak to allow module to import local modules
@@ -229,7 +229,7 @@ def init(src, minimal=False):
229229
copy(dest_path, src)
230230

231231

232-
def build(src, requirements=False, local_package=None):
232+
def build(src, config_file='config.yaml', requirements=False, local_package=None):
233233
"""Builds the file bundle.
234234
235235
:param str src:
@@ -240,7 +240,7 @@ def build(src, requirements=False, local_package=None):
240240
well (and/or is not available on PyPi)
241241
"""
242242
# Load and parse the config file.
243-
path_to_config_file = os.path.join(src, 'config.yaml')
243+
path_to_config_file = os.path.join(src, config_file)
244244
cfg = read(path_to_config_file, loader=yaml.load)
245245

246246
# Get the absolute path to the output directory and create it if it doesn't
@@ -296,7 +296,7 @@ def build(src, requirements=False, local_package=None):
296296
if os.path.isfile(filename):
297297
if filename == '.DS_Store':
298298
continue
299-
if filename == 'config.yaml':
299+
if filename == config_file:
300300
continue
301301
print('Bundling: %r' % filename)
302302
files.append(os.path.join(src, filename))

scripts/lambda

Lines changed: 18 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,22 @@ 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(
48+
CURRENT_DIR, config_file=config_file, use_requirements=use_requirements,
49+
local_package=local_package)
4750

4851

4952
@click.command(help='Run a local test of your function.')
5053
@click.option('--event-file', default=None, help='Alternate event file.')
54+
@click.option('--config-file', default=None, help='Alternate config file.')
5155
@click.option('--verbose', '-v', is_flag=True)
52-
def invoke(event_file, verbose):
53-
aws_lambda.invoke(CURRENT_DIR, event_file, verbose)
56+
def invoke(event_file, config_file, verbose):
57+
aws_lambda.invoke(
58+
CURRENT_DIR, event_file=event_file, config_file=config_file, verbose=verbose)
5459

5560

5661
@click.command(help='Register and deploy your code to lambda.')
62+
@click.option('--config-file', default=None, help='Alternate config file.')
5763
@click.option(
5864
'--use-requirements', default=False, is_flag=True,
5965
help='Install all packages defined in requirements.txt',
@@ -63,7 +69,9 @@ def invoke(event_file, verbose):
6369
help='Install local package as well.', multiple=True,
6470
)
6571
def deploy(use_requirements, local_package):
66-
aws_lambda.deploy(CURRENT_DIR, use_requirements, local_package)
72+
aws_lambda.deploy(
73+
CURRENT_DIR, config_file=config_file, use_requirements=use_requirements,
74+
local_package=local_package)
6775

6876

6977
@click.command(help='Upload your lambda to S3.')
@@ -79,15 +87,19 @@ def upload(use_requirements, local_package):
7987
aws_lambda.upload(CURRENT_DIR, use_requirements, local_package)
8088

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

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

92104

93105
if __name__ == '__main__':

0 commit comments

Comments
 (0)