@@ -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):
210210def 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):
251246def 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
289279def _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' )
0 commit comments