Skip to content

Commit

Permalink
feat: enrich zip lambda functions (#4156)
Browse files Browse the repository at this point in the history
  • Loading branch information
moelasmar authored Aug 31, 2022
1 parent 0dcf74a commit b6ac337
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 7 deletions.
39 changes: 38 additions & 1 deletion samcli/hook_packages/terraform/hooks/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,42 @@ def _enrich_mapped_resources(
"""

def _enrich_zip_lambda_function(sam_metadata_resource: Dict, cfn_resource: Dict, cfn_resource_logical_id: str):
pass
sam_metadata_attributes = sam_metadata_resource.get("values", {}).get("triggers", {})
sam_metadata_resource_address = sam_metadata_resource.get("address")
if not sam_metadata_resource_address:
raise PrepareHookException(
"Invalid Terraform plan output. The address property should not be null to any terraform resource."
)

LOG.info(
"Enrich the ZIP lambda function %s using the metadata properties defined in resource %s",
cfn_resource_logical_id,
sam_metadata_resource_address,
)
cfn_resource_properties = cfn_resource.get("Properties", {})

_validate_referenced_resource_matches_sam_metadata_type(
cfn_resource, sam_metadata_attributes, sam_metadata_resource_address, ZIP
)

cfn_source_code_path = _get_lambda_function_source_code_path(
sam_metadata_attributes,
sam_metadata_resource_address,
terraform_application_dir,
"original_source_code",
"source_code_property",
"source code",
)
cfn_resource_properties["Code"] = cfn_source_code_path
if not cfn_resource.get("Metadata", {}):
cfn_resource["Metadata"] = {}
cfn_resource["Metadata"]["SkipBuild"] = False
cfn_resource["Metadata"]["BuildMethod"] = "makefile"
cfn_resource["Metadata"]["ContextPath"] = output_directory_path
cfn_resource["Metadata"]["WorkingDirectory"] = terraform_application_dir
# currently we set the terraform project root directory that contains all the terraform artifacts as the project
# directory till we work on the custom hook properties, and add a property for this value.
cfn_resource["Metadata"]["ProjectRootDirectory"] = terraform_application_dir

def _enrich_image_lambda_function(sam_metadata_resource: Dict, cfn_resource: Dict, cfn_resource_logical_id: str):
pass
Expand All @@ -465,6 +500,8 @@ def _enrich_image_lambda_function(sam_metadata_resource: Dict, cfn_resource: Dic
f"is not a correct resource type. The resource type should be one of these values "
f"{resources_types_enrichment_functions.keys()}"
)
cfn_resource, logical_id = _get_relevant_cfn_resource(sam_metadata_resource, cfn_resources)
enrichment_function(sam_metadata_resource.resource, cfn_resource, logical_id)


def _get_relevant_cfn_resource(
Expand Down
94 changes: 88 additions & 6 deletions tests/unit/hook_packages/terraform/test_prepare_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ def test_translate_to_cfn_with_root_module_with_sam_metadata_resource(
):
checksum_mock.return_value = self.mock_logical_id_hash
translated_cfn_dict = _translate_to_cfn(
self.tf_json_with_root_module_with_sam_metadata_resources, "/output/dir", "/project/root"
self.tf_json_with_root_module_with_sam_metadata_resources, self.output_dir, self.project_root
)

mock_enrich_mapped_resources.assert_called_once_with(
Expand All @@ -895,8 +895,8 @@ def test_translate_to_cfn_with_root_module_with_sam_metadata_resource(
),
],
translated_cfn_dict["Resources"],
"/output/dir",
"/project/root",
self.output_dir,
self.project_root,
)

@patch("samcli.hook_packages.terraform.hooks.prepare._enrich_mapped_resources")
Expand All @@ -906,7 +906,7 @@ def test_translate_to_cfn_with_child_modules_with_sam_metadata_resource(
):
checksum_mock.return_value = self.mock_logical_id_hash
translated_cfn_dict = _translate_to_cfn(
self.tf_json_with_child_modules_with_sam_metadata_resource, "/output/dir", "/project/root"
self.tf_json_with_child_modules_with_sam_metadata_resource, self.output_dir, self.project_root
)
mock_enrich_mapped_resources.assert_called_once_with(
[
Expand Down Expand Up @@ -936,8 +936,8 @@ def test_translate_to_cfn_with_child_modules_with_sam_metadata_resource(
),
],
translated_cfn_dict["Resources"],
"/output/dir",
"/project/root",
self.output_dir,
self.project_root,
)

@patch("samcli.hook_packages.terraform.hooks.prepare._enrich_mapped_resources")
Expand Down Expand Up @@ -1262,6 +1262,88 @@ def test_get_relevant_cfn_resource_exceptions(
with self.assertRaises(InvalidSamMetadataPropertiesException, msg=exception_message):
_get_relevant_cfn_resource(sam_metadata_resource, cfn_resources)

@patch("samcli.hook_packages.terraform.hooks.prepare._get_relevant_cfn_resource")
@patch("samcli.hook_packages.terraform.hooks.prepare._validate_referenced_resource_matches_sam_metadata_type")
@patch("samcli.hook_packages.terraform.hooks.prepare._get_lambda_function_source_code_path")
def test_enrich_mapped_resources_zip_functions(
self,
mock_get_lambda_function_source_code_path,
mock_validate_referenced_resource_matches_sam_metadata_type,
mock_get_relevant_cfn_resource,
):
mock_get_lambda_function_source_code_path.side_effect = ["src/code/path1", "src/code/path2"]
zip_function_1 = {
"Type": CFN_AWS_LAMBDA_FUNCTION,
"Properties": {
**self.expected_cfn_function_common_properties,
"Code": "file.zip",
},
"Metadata": {"SamResourceId": f"aws_lambda_function.func1", "SkipBuild": True},
}
zip_function_2 = {
"Type": CFN_AWS_LAMBDA_FUNCTION,
"Properties": {
**self.expected_cfn_function_common_properties,
"Code": "file2.zip",
},
"Metadata": {"SamResourceId": f"aws_lambda_function.func2", "SkipBuild": True},
}
cfn_resources = {
"logical_id1": zip_function_1,
"logical_id2": zip_function_2,
}
mock_get_relevant_cfn_resource.side_effect = [
(zip_function_1, "logical_id1"),
(zip_function_2, "logical_id2"),
]
sam_metadata_resources = [
SamMetadataResource(
current_module_address=None, resource=self.tf_lambda_function_resource_zip_sam_metadata
),
SamMetadataResource(
current_module_address=None, resource=self.tf_lambda_function_resource_zip_2_sam_metadata
),
]

expected_zip_function_1 = {
"Type": CFN_AWS_LAMBDA_FUNCTION,
"Properties": {
**self.expected_cfn_function_common_properties,
"Code": "src/code/path1",
},
"Metadata": {
"SamResourceId": "aws_lambda_function.func1",
"SkipBuild": False,
"BuildMethod": "makefile",
"ContextPath": "/output/dir",
"WorkingDirectory": "/terraform/project/root",
"ProjectRootDirectory": "/terraform/project/root",
},
}
expected_zip_function_2 = {
"Type": CFN_AWS_LAMBDA_FUNCTION,
"Properties": {
**self.expected_cfn_function_common_properties,
"Code": "src/code/path2",
},
"Metadata": {
"SamResourceId": "aws_lambda_function.func2",
"SkipBuild": False,
"BuildMethod": "makefile",
"ContextPath": "/output/dir",
"WorkingDirectory": "/terraform/project/root",
"ProjectRootDirectory": "/terraform/project/root",
},
}

expected_cfn_resources = {
"logical_id1": expected_zip_function_1,
"logical_id2": expected_zip_function_2,
}

_enrich_mapped_resources(sam_metadata_resources, cfn_resources, "/output/dir", "/terraform/project/root")
self.assertEquals(cfn_resources, expected_cfn_resources)

def test_enrich_mapped_resources_invalid_source_type(self):
image_function_1 = {
"Type": CFN_AWS_LAMBDA_FUNCTION,
Expand Down

0 comments on commit b6ac337

Please sign in to comment.