diff --git a/lib/ansible/modules/cloud/amazon/lambda.py b/lib/ansible/modules/cloud/amazon/lambda.py index 2c5ad38226e..0a0a9897b33 100644 --- a/lib/ansible/modules/cloud/amazon/lambda.py +++ b/lib/ansible/modules/cloud/amazon/lambda.py @@ -330,7 +330,7 @@ def main(): func_kwargs.update({'Timeout': timeout}) if memory_size and current_config['MemorySize'] != memory_size: func_kwargs.update({'MemorySize': memory_size}) - if (environment_variables is not None) and (current_config['Environment']['Variables'] != environment_variables): + if (environment_variables is not None) and (current_config.get('Environment', {}).get('Variables', {}) != environment_variables): func_kwargs.update({'Environment':{'Variables': environment_variables}}) if dead_letter_arn is not None: if current_config.get('DeadLetterConfig'): diff --git a/test/units/modules/cloud/amazon/test_lambda.py b/test/units/modules/cloud/amazon/test_lambda.py index db718df59f8..049d1394bc2 100644 --- a/test/units/modules/cloud/amazon/test_lambda.py +++ b/test/units/modules/cloud/amazon/test_lambda.py @@ -65,6 +65,9 @@ base_module_args={ "timeout" : 3, "handler": 'lambda_python.my_handler' } +module_args_with_environment=dict(base_module_args, environment_variables={ + "variable_name": "variable_value" +}) def make_mock_no_connection_connection(config): @@ -206,6 +209,30 @@ def test_update_lambda_if_only_one_config_item_changed(): assert(len(lambda_client_double.update_function_code.mock_calls) == 0), \ "updated lambda code when no change should have happened" +def test_update_lambda_if_added_environment_variable(): + + set_module_args(module_args_with_environment) + (boto3_conn_double,lambda_client_double)=make_mock_connection(base_lambda_config) + + with patch.object(lda, 'boto3_conn', boto3_conn_double): + try: + lda.main() + except SystemExit: + pass + + # guard against calling other than for a lambda connection (e.g. IAM) + assert(len(boto3_conn_double.mock_calls) == 1), "multiple boto connections used unexpectedly" + assert(len(lambda_client_double.update_function_configuration.mock_calls) > 0), \ + "failed to update lambda function when configuration changed" + assert(len(lambda_client_double.update_function_configuration.mock_calls) < 2), \ + "lambda function update called multiple times when only one time should be needed" + assert(len(lambda_client_double.update_function_code.mock_calls) == 0), \ + "updated lambda code when no change should have happened" + + (update_args, update_kwargs)=lambda_client_double.update_function_configuration.call_args + assert (len(update_kwargs) > 0), "expected update configuration called with keyword args, none found" + assert update_kwargs['Environment']['Variables'] == module_args_with_environment['environment_variables'] + def test_dont_update_lambda_if_nothing_changed(): set_module_args(base_module_args)