Skip to content

Commit

Permalink
fix metadata file generation performance issue (#4334)
Browse files Browse the repository at this point in the history
  • Loading branch information
moelasmar authored Oct 25, 2022
1 parent ed8be3c commit 5b37562
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
19 changes: 11 additions & 8 deletions samcli/hook_packages/terraform/hooks/prepare/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def _translate_to_cfn(tf_json: dict, output_directory_path: str, terraform_appli

lambda_layers_terraform_resources: Dict[str, Dict] = {}
lambda_funcs_conf_cfn_resources: Dict[str, List] = {}
lambda_config_funcs_conf_cfn_resources: Dict[str, TFResource] = {}

# create and iterate over queue of modules to handle child modules
module_queue = [(root_module, root_tf_module)]
Expand Down Expand Up @@ -321,12 +322,15 @@ def _translate_to_cfn(tf_json: dict, output_directory_path: str, terraform_appli
matched_lambdas = lambda_funcs_conf_cfn_resources.get(resolved_config_address, [])
matched_lambdas.append(translated_resource)
lambda_funcs_conf_cfn_resources[resolved_config_address] = matched_lambdas
lambda_config_funcs_conf_cfn_resources[resolved_config_address] = config_resource

# map s3 object sources to corresponding functions
LOG.debug("Mapping S3 object sources to corresponding functions")
_map_s3_sources_to_functions(s3_hash_to_source, cfn_dict.get("Resources", {}))

_link_lambda_functions_to_layers(root_tf_module, lambda_funcs_conf_cfn_resources, lambda_layers_terraform_resources)
_link_lambda_functions_to_layers(
lambda_config_funcs_conf_cfn_resources, lambda_funcs_conf_cfn_resources, lambda_layers_terraform_resources
)

if sam_metadata_resources:
LOG.debug("Enrich the mapped resources with the sam metadata information and generate Makefile")
Expand Down Expand Up @@ -377,7 +381,7 @@ def _add_child_modules_to_queue(curr_module: Dict, curr_module_configuration: TF


def _link_lambda_functions_to_layers(
root_module: TFModule,
lambda_config_funcs_conf_cfn_resources: Dict[str, TFResource],
lambda_funcs_conf_cfn_resources: Dict[str, List],
lambda_layers_terraform_resources: Dict[str, Dict],
):
Expand All @@ -386,8 +390,8 @@ def _link_lambda_functions_to_layers(
Parameters
----------
root_module: TFModule
the parsed terraform project root module
lambda_config_funcs_conf_cfn_resources: Dict[str, TFResource]
Dictionary of configuration lambda resources
lambda_funcs_conf_cfn_resources: Dict[str, List]
Dictionary containing resolved configuration addresses matched up to the cfn Lambda functions
lambda_layers_terraform_resources: Dict[str, Dict]
Expand All @@ -399,12 +403,11 @@ def _link_lambda_functions_to_layers(
dict
The CloudFormation resulting from translating tf_json
"""
for resource in root_module.get_all_resources():
resolved_config_address = _get_configuration_address(resource.full_address)
if resolved_config_address in lambda_funcs_conf_cfn_resources:
for config_address, resource in lambda_config_funcs_conf_cfn_resources.items():
if config_address in lambda_funcs_conf_cfn_resources:
LOG.debug("Linking layers for Lambda function %s", resource.full_address)
_link_lambda_function_to_layer(
resource, lambda_funcs_conf_cfn_resources[resolved_config_address], lambda_layers_terraform_resources
resource, lambda_funcs_conf_cfn_resources[config_address], lambda_layers_terraform_resources
)


Expand Down
23 changes: 7 additions & 16 deletions tests/unit/hook_packages/terraform/hooks/prepare/test_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,8 @@ def test_translate_to_cfn_with_root_module_only(
child_modules.__contains__.return_value = True
child_modules.get.return_value = root_module
root_module.child_modules = child_modules
resources_mock.__getitem__.return_value = Mock()
config_resource = Mock()
resources_mock.__getitem__.return_value = config_resource
resources_mock.__contains__.return_value = True
mock_build_module.return_value = root_module
checksum_mock.return_value = self.mock_logical_id_hash
Expand All @@ -1120,7 +1121,7 @@ def test_translate_to_cfn_with_root_module_only(
)
)
expected_arguments_in_call = [
root_module,
{mock_get_configuration_address(): config_resource},
{mock_get_configuration_address(): [val for _, val in lambda_functions.items()]},
{},
]
Expand Down Expand Up @@ -1202,7 +1203,8 @@ def test_translate_to_cfn_with_child_modules(
child_modules.__contains__.return_value = True
child_modules.get.return_value = root_module
root_module.child_modules = child_modules
resources_mock.__getitem__.return_value = Mock()
conf_resource = Mock()
resources_mock.__getitem__.return_value = conf_resource
resources_mock.__contains__.return_value = True
mock_build_module.return_value = root_module
checksum_mock.return_value = self.mock_logical_id_hash
Expand All @@ -1216,7 +1218,7 @@ def test_translate_to_cfn_with_child_modules(
)
)
expected_arguments_in_call = [
root_module,
{mock_get_configuration_address(): conf_resource},
{mock_get_configuration_address(): [val for _, val in lambda_functions.items()]},
{},
]
Expand Down Expand Up @@ -3112,18 +3114,7 @@ def test_link_lambda_functions_to_layers(self, mock_get_configuration_address, m
),
"aws_lambda_function.root_lambda": TFResource("aws_lambda_function.root_lambda", "", None, {}),
}
module = TFModule("", None, {}, resources, {}, {})
mock_get_configuration_address.side_effect = [
"aws_lambda_function.remote_lambda_code",
"aws_lambda_function.root_lambda",
]
_link_lambda_functions_to_layers(module, lambda_funcs_config_resources, terraform_layers_resources)
mock_get_configuration_address.assert_has_calls(
[
call("aws_lambda_function.remote_lambda_code"),
call("aws_lambda_function.root_lambda"),
]
)
_link_lambda_functions_to_layers(resources, lambda_funcs_config_resources, terraform_layers_resources)
mock_link_lambda_function_to_layer.assert_has_calls(
[
call(
Expand Down

0 comments on commit 5b37562

Please sign in to comment.