-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(sync): Skip auto-dependency layer for functions with intrinsic function Layers #8489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
83fa88a
bdd1c5b
c5e612b
0d35227
7e3ab96
2e8d675
ce6e323
0dc55b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -223,3 +223,47 @@ def list_active_stack_names(boto_client_provider: BotoProviderType, show_nested_ | |
| continue | ||
| yield stack_summary.get("StackName") | ||
| next_token = list_stacks_result.get("NextToken") | ||
|
|
||
|
|
||
| # CloudFormation intrinsic function names | ||
| # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html | ||
| CLOUDFORMATION_INTRINSIC_FUNCTIONS = { | ||
| "Fn::Base64", | ||
| "Fn::Cidr", | ||
| "Fn::FindInMap", | ||
| "Fn::ForEach", | ||
| "Fn::GetAtt", | ||
| "Fn::GetAZs", | ||
| "Fn::ImportValue", | ||
| "Fn::Join", | ||
| "Fn::Length", | ||
| "Fn::Select", | ||
| "Fn::Split", | ||
| "Fn::Sub", | ||
| "Fn::ToJsonString", | ||
| "Fn::Transform", | ||
| "Fn::And", | ||
| "Fn::Equals", | ||
| "Fn::If", | ||
| "Fn::Not", | ||
| "Fn::Or", | ||
| "Ref", | ||
| } | ||
|
|
||
|
|
||
| def is_intrinsic_function(value: Any) -> bool: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we have a intrinsc resolver already |
||
| """ | ||
| Checks if a value is a CloudFormation intrinsic function. | ||
|
|
||
| When YAML templates are parsed, intrinsic functions are represented as OrderedDict | ||
| with a single key that matches one of the CloudFormation intrinsic function names. | ||
| """ | ||
|
|
||
| if not isinstance(value, dict): | ||
| return False | ||
|
|
||
| if len(value) != 1: | ||
| return False | ||
|
|
||
| key = next(iter(value.keys())) | ||
| return key in CLOUDFORMATION_INTRINSIC_FUNCTIONS | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -244,3 +244,53 @@ def test_skipping_dependency_copy_when_function_has_no_dependencies( | |
| @parameterized.expand([("python3.8", True), ("ruby3.2", False)]) | ||
| def test_is_runtime_supported(self, runtime, supported): | ||
| self.assertEqual(NestedStackManager.is_runtime_supported(runtime), supported) | ||
|
|
||
| @patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.move_template") | ||
| @patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.osutils") | ||
| @patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.os.path.isdir") | ||
| def test_with_intrinsic_function_layers_skips_auto_layer( | ||
| self, patched_isdir, patched_osutils, patched_move_template | ||
| ): | ||
| """Test that functions with intrinsic function Layers are skipped with a warning""" | ||
| resources = { | ||
| "MyFunction": { | ||
| "Type": AWS_SERVERLESS_FUNCTION, | ||
| "Properties": { | ||
| "Runtime": "python3.8", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why 3.8 |
||
| "Handler": "FakeHandler", | ||
| "Layers": {"Fn::If": ["HasLayer", [{"Ref": "MyLayer"}], []]}, | ||
| }, | ||
| } | ||
| } | ||
| self.stack.resources = resources | ||
| template = {"Resources": resources} | ||
|
|
||
| # prepare build graph | ||
| dependencies_dir = Mock() | ||
| function = Mock() | ||
| function.name = "MyFunction" | ||
| functions = [function] | ||
| build_graph = Mock() | ||
| function_definition_mock = Mock(dependencies_dir=dependencies_dir, functions=functions) | ||
| build_graph.get_function_build_definition_with_logical_id.return_value = function_definition_mock | ||
| app_build_result = ApplicationBuildResult(build_graph, {"MyFunction": "path/to/build/dir"}) | ||
| patched_isdir.return_value = True | ||
|
|
||
| nested_stack_manager = NestedStackManager( | ||
| self.stack, self.stack_name, self.build_dir, template, app_build_result | ||
| ) | ||
|
|
||
| with patch.object(nested_stack_manager, "_add_layer_readme_info"): | ||
| result = nested_stack_manager.generate_auto_dependency_layer_stack() | ||
|
|
||
| # Should not create nested stack since function was skipped | ||
| patched_move_template.assert_not_called() | ||
|
|
||
| # Template should remain unchanged | ||
| self.assertEqual(template, result) | ||
|
|
||
| # Layers should still be the intrinsic function (not modified) | ||
| self.assertEqual( | ||
| result.get("Resources", {}).get("MyFunction", {}).get("Properties", {}).get("Layers"), | ||
| {"Fn::If": ["HasLayer", [{"Ref": "MyLayer"}], []]}, | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,15 @@ | ||
| from collections import OrderedDict | ||
| from unittest import TestCase | ||
| from unittest.mock import patch, Mock, ANY, call | ||
|
|
||
| from botocore.exceptions import ClientError | ||
| from parameterized import parameterized | ||
|
|
||
| from samcli.lib.utils.cloudformation import ( | ||
| CloudFormationResourceSummary, | ||
| get_resource_summaries, | ||
| get_resource_summary, | ||
| is_intrinsic_function, | ||
| list_active_stack_names, | ||
| get_resource_summary_from_physical_id, | ||
| ) | ||
|
|
@@ -215,3 +218,41 @@ def test_get_resource_summary_from_physical_id_fail(self, patched_log): | |
| resource_summary = get_resource_summary_from_physical_id(patched_cfn_client_provider, "invalid_physical_id") | ||
| self.assertIsNone(resource_summary) | ||
| patched_log.debug.assert_called_once() | ||
|
|
||
|
|
||
| class TestIsIntrinsicFunction(TestCase): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment, shouldn't this be in SAM-T |
||
| """Tests for the is_intrinsic_function utility""" | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ("Fn::If", ["Condition", "Value1", "Value2"]), | ||
| ("Ref", "MyParameter"), | ||
| ("Fn::GetAtt", ["Resource", "Attribute"]), | ||
| ("Fn::Sub", "${AWS::StackName}-bucket"), | ||
| ("Fn::ForEach", ["Item", ["a", "b"], {"Key": "Value"}]), | ||
| ("Fn::Length", ["item1", "item2", "item3"]), | ||
| ("Fn::ToJsonString", {"key": "value"}), | ||
| ("Fn::Base64", "value"), | ||
| ("Fn::Join", ["-", ["a", "b"]]), | ||
| ("Fn::Select", [0, ["a", "b"]]), | ||
| ] | ||
| ) | ||
| def test_intrinsic_functions_detected(self, function_name, function_value): | ||
| """Test that CloudFormation intrinsic functions are correctly detected""" | ||
| value = OrderedDict([(function_name, function_value)]) | ||
| self.assertTrue(is_intrinsic_function(value)) | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| (["item1", "item2", "item3"], "list"), | ||
| ({"key1": "value1", "key2": "value2"}, "multi_key_dict"), | ||
| ({"NotAnIntrinsic": "value"}, "single_key_non_intrinsic"), | ||
| ("just a string", "string"), | ||
| (None, "none"), | ||
| ({}, "empty_dict"), | ||
| (123, "number"), | ||
| ] | ||
| ) | ||
| def test_non_intrinsic_values_not_detected(self, value, description): | ||
| """Test that non-intrinsic values are not detected as intrinsic functions""" | ||
| self.assertFalse(is_intrinsic_function(value)) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shoudn't this be in SAM-T?