Skip to content

Commit f039884

Browse files
committed
cfg refactor
1 parent 335b368 commit f039884

2 files changed

Lines changed: 46 additions & 45 deletions

File tree

aws_lambda/aws_lambda.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def deploy_function(function_module, function_name_suffix='', package_objects=No
109109
archive.write(os.path.join(root, file), arcname=arcname)
110110

111111
# create or update the function
112-
if function_exists(cfg, function_name):
112+
if function_exists(cfg):
113113
update_function(cfg, tmp_zip.name, extra_config)
114114
else:
115115
create_function(cfg, tmp_zip.name, extra_config)
@@ -210,14 +210,9 @@ def get_client(client, aws_access_key_id, aws_secret_access_key, region=None):
210210
def create_function(cfg, path_to_zip_file, extra_config=None):
211211
"""Register and upload a function to AWS Lambda."""
212212
byte_stream = read_file(path_to_zip_file, binary_file=True)
213-
aws_access_key_id = cfg.get('aws_access_key_id')
214-
aws_secret_access_key = cfg.get('aws_secret_access_key')
215-
216-
account_id = get_account_id(aws_access_key_id, aws_secret_access_key)
213+
account_id = get_account_id(cfg.get('aws_access_key_id'), cfg.get('aws_secret_access_key'))
217214
role = get_role_name(account_id, cfg.get('role', 'lambda_basic_execution'))
218-
219-
client = get_client('lambda', aws_access_key_id, aws_secret_access_key,
220-
cfg.get('region'))
215+
client = _client_from_cfg(cfg)
221216

222217
func_name = (
223218
os.environ.get('LAMBDA_FUNCTION_NAME') or cfg.get('function_name')
@@ -251,14 +246,9 @@ def create_function(cfg, path_to_zip_file, extra_config=None):
251246
def update_function(cfg, path_to_zip_file, extra_config=None):
252247
"""Updates the code of an existing Lambda function"""
253248
byte_stream = read_file(path_to_zip_file, binary_file=True)
254-
aws_access_key_id = cfg.get('aws_access_key_id')
255-
aws_secret_access_key = cfg.get('aws_secret_access_key')
256-
257-
account_id = get_account_id(aws_access_key_id, aws_secret_access_key)
249+
account_id = get_account_id(cfg.get('aws_access_key_id'), cfg.get('aws_secret_access_key'))
258250
role = get_role_name(account_id, cfg.get('role', 'lambda_basic_execution'))
259-
260-
client = get_client('lambda', aws_access_key_id, aws_secret_access_key,
261-
cfg.get('region'))
251+
client = _client_from_cfg(cfg)
262252

263253
log.info('Updating lambda function with name: {}'.format(cfg.get('function_name')))
264254
client.update_function_code(
@@ -288,47 +278,53 @@ def update_function(cfg, path_to_zip_file, extra_config=None):
288278

289279
def _client_from_cfg(cfg):
290280
"""
291-
Helper method for the below methods that sets up a lambda client given
292-
a config dictionary containing the relevant AWS keys.
281+
Helper method for several other methods that sets up a lambda client given
282+
a config dictionary containing the relevant AWS keys. If the keys aren't found
283+
and there are keys in the environment boto3 will locate them and proceed.
293284
"""
294285
aws_access_key_id = cfg.get('aws_access_key_id')
295286
aws_secret_access_key = cfg.get('aws_secret_access_key')
296287
region = cfg.get('region', 'us-east-1')
288+
if not aws_secret_access_key or not aws_access_key_id:
289+
log.warning('AWS Credentials not found in cfg! Falling back to env...')
297290
return get_client('lambda', aws_access_key_id, aws_secret_access_key,
298291
region)
299292

300293

301-
def function_exists(cfg, function_name):
302-
"""Check whether a function exists or not"""
294+
def function_exists(cfg):
295+
"""
296+
Check whether the given function in cfg exists or not
297+
"""
303298
client = _client_from_cfg(cfg)
304299
try:
305-
client.get_function(FunctionName=function_name)
300+
client.get_function(FunctionName=cfg.get('function_name'))
306301
except:
307302
return False
308303
return True
309304

310305

311-
def delete_function(cfg, function_name):
306+
def delete_function(cfg):
312307
"""
313-
Deletes the given function name from AWS Lambda. First checks that it exists.
308+
Deletes the given function name found in cfg.
314309
Returns True in success, False otherwise
315310
"""
316311
client = _client_from_cfg(cfg)
317312
try:
318-
client.get_function(FunctionName=function_name)
319-
client.delete_function(FunctionName=function_name)
313+
client.get_function(FunctionName=cfg.get('function_name'))
314+
client.delete_function(FunctionName=cfg.get('function_name'))
320315
except:
321316
return False
322317
return True
323318

324-
def invoke_function(cfg, function_name, invocation_type='RequestResponse', event={}):
319+
def invoke_function(cfg, invocation_type='Event', event={}):
325320
"""
326321
Invokes the given lambda function using the given config cfg.
327322
328323
invocation_type is one of 'Event'|'RequestResponse'|'DryRun'. The default
329-
is RequestResponse, which will cause this function to hang until the lambda
330-
is completed. 'Event' triggers the lambda asynchronously. 'DryRun' just
331-
validates parameters/permissions.
324+
is Event, which will cause this function to execute asynchronously.
325+
'RequestResponse' triggers the lambda serially. 'DryRun' just
326+
validates parameters/permissions. Note that if you use 'Event' you will likely
327+
get no response from this function.
332328
333329
event is a dictionary containing the arguments needed for this lambda. For
334330
example if your lambda is expecting fields 'a' and 'b' in the 'event' that is
@@ -343,11 +339,11 @@ def invoke_function(cfg, function_name, invocation_type='RequestResponse', event
343339
import json
344340
client = _client_from_cfg(cfg)
345341
try:
346-
resp = client.invoke(FunctionName=function_name,
342+
resp = client.invoke(FunctionName=cfg.get('function_name'),
347343
InvocationType=invocation_type,
348344
Payload=json.dumps(event))
349345
except:
350346
log.error('Failed to execute lambda fxn: %s with arguments: \n %s \n %s'
351-
% (function_name, invocation_type, event))
347+
% (cfg.get('function_name'), invocation_type, event))
352348
return None
353349
return resp['Payload'].read().decode('utf-8')

tests/test_aws_lambda.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,16 @@ def test_deploy_lambda(self, cfg):
7777
"""
7878
suff = 'integration_test'
7979
full_name = example_function.config['function_name'] + '_' + suff
80+
cfg['function_name'] = full_name
8081
deploy_function(example_function, function_name_suffix=suff)
81-
assert function_exists(cfg, full_name)
82-
resp = invoke_function(cfg, full_name)
82+
assert function_exists(cfg)
83+
resp = invoke_function(cfg, invocation_type='RequestResponse')
8384
assert resp == '"Hello! My input event is {}"'
8485
deploy_function(example_function_update, function_name_suffix=suff)
85-
resp = invoke_function(cfg, full_name)
86+
resp = invoke_function(cfg, invocation_type='RequestResponse')
8687
assert resp == '"Hello! I have been updated! My input event is {}"'
87-
assert delete_function(cfg, full_name)
88-
assert not delete_function(cfg, full_name)
88+
assert delete_function(cfg)
89+
assert not delete_function(cfg)
8990

9091
def test_deploy_lambda_with_requirements(self, cfg):
9192
"""
@@ -94,42 +95,46 @@ def test_deploy_lambda_with_requirements(self, cfg):
9495
"""
9596
import json
9697
full_name = function_with_requirements.config['function_name']
98+
cfg['function_name'] = full_name
9799
req_fpath = './tests/test_lambdas/requirements.txt'
98100
deploy_function(function_with_requirements, requirements_fpath=req_fpath)
99-
assert function_exists(cfg, full_name)
100-
resp = invoke_function(cfg, full_name, event={'magic': 17})
101+
assert function_exists(cfg)
102+
resp = invoke_function(cfg, invocation_type='RequestResponse', event={'magic': 17})
101103
assert resp == '"I successfully imported pytest! Magic: 17"'
102-
assert delete_function(cfg, full_name)
104+
assert delete_function(cfg)
103105

104106
def test_deploy_lambda_with_package(self, cfg):
105107
"""
106108
Deploys and invokes a lambda that has package based dependencies
107109
"""
108110
full_name = function_with_package.config['function_name']
111+
cfg['function_name'] = full_name
109112
deploy_function(function_with_package, package_objects=[package])
110-
assert function_exists(cfg, full_name)
111-
resp = invoke_function(cfg, full_name)
113+
assert function_exists(cfg)
114+
resp = invoke_function(cfg, invocation_type='RequestResponse')
112115
assert resp == '"I successfully called magic_function: 21"'
113-
assert delete_function(cfg, full_name)
116+
assert delete_function(cfg)
114117

115118
def test_deploy_lambda_with_package_and_requirements(self, cfg):
116119
"""
117120
Deploys and invokes a lambda that uses both an independently packaged
118121
dependency and one given from requirements.
119122
"""
120123
full_name = function_with_req_pack.config['function_name']
124+
cfg['function_name'] = full_name
121125
req_fpath = './tests/test_lambdas/requirements.txt'
122126
deploy_function(function_with_req_pack, requirements_fpath=req_fpath,
123127
package_objects=[package])
124-
assert function_exists(cfg, full_name)
125-
resp = invoke_function(cfg, full_name)
128+
assert function_exists(cfg)
129+
resp = invoke_function(cfg, invocation_type='RequestResponse')
126130
assert resp == '"Imported pytest, magic_function: 21"'
127-
assert delete_function(cfg, full_name)
131+
assert delete_function(cfg)
128132

129133
def test_invoke_invalid_lambda(self, cfg):
130134
"""
131135
Tries to execute an invalid lambda. Should fail
132136
"""
133137
full_name = 'not_a_lambda_name'
134-
resp = invoke_function(cfg, full_name)
138+
cfg['function_name'] = full_name
139+
resp = invoke_function(cfg, invocation_type='RequestResponse')
135140
assert resp is None

0 commit comments

Comments
 (0)