-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: handle intrinsic functions in Lambda FunctionName for GetAtt resolution #8820
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
Open
hoangsetup
wants to merge
10
commits into
aws:develop
Choose a base branch
from
hoangsetup:fix/joinfn_getatt
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8c6a6d1
fix: handle intrinsic functions in Lambda FunctionName for GetAtt res…
hoangsetup 6c1ba26
Merge branch 'develop' into fix/joinfn_getatt
hoangsetup 23c41c9
Merge branch 'develop' into fix/joinfn_getatt
hoangsetup c52fa01
Merge branch 'develop' into fix/joinfn_getatt
vicheey 5047f7e
fix: remove dead code from _get_function_name
hoangsetup ca8763e
Merge branch 'develop' into fix/joinfn_getatt
hoangsetup 5f9750a
Merge branch 'develop' into fix/joinfn_getatt
hoangsetup 7fc0a14
Merge branch 'develop' into fix/joinfn_getatt
hoangsetup 719e27c
Merge branch 'develop' into fix/joinfn_getatt
hoangsetup 996b581
Merge branch 'develop' into fix/joinfn_getatt
hoangsetup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
tests/unit/lib/intrinsic_resolver/test_fn_join_with_getatt.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| """ | ||
| Unit tests for Fn::Join with Fn::GetAtt bug fix | ||
| """ | ||
|
|
||
| from unittest import TestCase | ||
| from samcli.lib.intrinsic_resolver.intrinsic_property_resolver import IntrinsicResolver | ||
| from samcli.lib.intrinsic_resolver.intrinsics_symbol_table import IntrinsicsSymbolTable | ||
|
|
||
|
|
||
| class TestFnJoinWithGetAtt(TestCase): | ||
| """ | ||
| Test that Fn::Join works correctly with Fn::GetAtt for Lambda function ARNs, | ||
| especially when the FunctionName property contains intrinsic functions. | ||
| """ | ||
|
|
||
| def test_fn_join_with_getatt_simple_function_name(self): | ||
| """ | ||
| Test Fn::Join with Fn::GetAtt when FunctionName is a simple string | ||
| """ | ||
| template = { | ||
| "Resources": { | ||
| "MyFunction": { | ||
| "Type": "AWS::Lambda::Function", | ||
| "Properties": { | ||
| "FunctionName": "my-function-name", | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resolver = IntrinsicResolver( | ||
| template=template, symbol_resolver=IntrinsicsSymbolTable(logical_id_translator={}, template=template) | ||
| ) | ||
|
|
||
| uri_intrinsic = { | ||
| "Fn::Join": [ | ||
| "", | ||
| [ | ||
| "arn:", | ||
| {"Ref": "AWS::Partition"}, | ||
| ":apigateway:", | ||
| {"Ref": "AWS::Region"}, | ||
| ":lambda:path/2015-03-31/functions/", | ||
| {"Fn::GetAtt": ["MyFunction", "Arn"]}, | ||
| "/invocations", | ||
| ], | ||
| ] | ||
| } | ||
|
|
||
| result = resolver.intrinsic_property_resolver(uri_intrinsic, ignore_errors=False) | ||
|
|
||
| self.assertIsInstance(result, str) | ||
| self.assertIn("my-function-name", result) | ||
| self.assertIn("arn:aws:apigateway:", result) | ||
| self.assertIn("lambda:path/2015-03-31/functions/", result) | ||
| self.assertIn("/invocations", result) | ||
|
|
||
| def test_fn_join_with_getatt_intrinsic_function_name(self): | ||
| """ | ||
| Test Fn::Join with Fn::GetAtt when FunctionName contains Fn::Sub | ||
| This was the original bug - it would throw TypeError: unhashable type: 'dict' | ||
| """ | ||
| template = { | ||
| "Resources": { | ||
| "MyFunction": { | ||
| "Type": "AWS::Lambda::Function", | ||
| "Properties": { | ||
| "FunctionName": {"Fn::Sub": "my-${AWS::StackName}-function"}, | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resolver = IntrinsicResolver( | ||
| template=template, symbol_resolver=IntrinsicsSymbolTable(logical_id_translator={}, template=template) | ||
| ) | ||
|
|
||
| uri_intrinsic = { | ||
| "Fn::Join": [ | ||
| "", | ||
| [ | ||
| "arn:", | ||
| {"Ref": "AWS::Partition"}, | ||
| ":apigateway:", | ||
| {"Ref": "AWS::Region"}, | ||
| ":lambda:path/2015-03-31/functions/", | ||
| {"Fn::GetAtt": ["MyFunction", "Arn"]}, | ||
| "/invocations", | ||
| ], | ||
| ] | ||
| } | ||
|
|
||
| result = resolver.intrinsic_property_resolver(uri_intrinsic, ignore_errors=False) | ||
|
|
||
| self.assertIsInstance(result, str) | ||
| self.assertIn("MyFunction", result) | ||
| self.assertIn("arn:aws:apigateway:", result) | ||
| self.assertIn("lambda:path/2015-03-31/functions/", result) | ||
| self.assertIn("/invocations", result) | ||
|
|
||
| def test_fn_join_with_getatt_no_function_name(self): | ||
| """ | ||
| Test Fn::Join with Fn::GetAtt when FunctionName property is not defined | ||
| Should use the logical ID as the function name | ||
| """ | ||
| template = { | ||
| "Resources": { | ||
| "MyFunction": { | ||
| "Type": "AWS::Lambda::Function", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resolver = IntrinsicResolver( | ||
| template=template, symbol_resolver=IntrinsicsSymbolTable(logical_id_translator={}, template=template) | ||
| ) | ||
|
|
||
| uri_intrinsic = { | ||
| "Fn::Join": [ | ||
| "", | ||
| [ | ||
| "arn:", | ||
| {"Ref": "AWS::Partition"}, | ||
| ":apigateway:", | ||
| {"Ref": "AWS::Region"}, | ||
| ":lambda:path/2015-03-31/functions/", | ||
| {"Fn::GetAtt": ["MyFunction", "Arn"]}, | ||
| "/invocations", | ||
| ], | ||
| ] | ||
| } | ||
|
|
||
| result = resolver.intrinsic_property_resolver(uri_intrinsic, ignore_errors=False) | ||
|
|
||
| self.assertIsInstance(result, str) | ||
| self.assertIn("MyFunction", result) | ||
| self.assertIn("arn:aws:apigateway:", result) | ||
| self.assertIn("lambda:path/2015-03-31/functions/", result) | ||
| self.assertIn("/invocations", result) | ||
|
|
||
| def test_fn_sub_still_works_with_getatt(self): | ||
| """ | ||
| Ensure that the fix doesn't break Fn::Sub with Fn::GetAtt (which was already working) | ||
| """ | ||
| template = { | ||
| "Resources": { | ||
| "MyFunction": { | ||
| "Type": "AWS::Lambda::Function", | ||
| "Properties": { | ||
| "FunctionName": {"Fn::Sub": "my-${AWS::StackName}-function"}, | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resolver = IntrinsicResolver( | ||
| template=template, symbol_resolver=IntrinsicsSymbolTable(logical_id_translator={}, template=template) | ||
| ) | ||
|
|
||
| uri_intrinsic = { | ||
| "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFunction.Arn}/invocations" | ||
| } | ||
|
|
||
| result = resolver.intrinsic_property_resolver(uri_intrinsic, ignore_errors=False) | ||
|
|
||
| self.assertIsInstance(result, str) | ||
| self.assertIn("MyFunction", result) | ||
| self.assertIn("arn:aws:apigateway:", result) | ||
| self.assertIn("lambda:path/2015-03-31/functions/", result) | ||
| self.assertIn("/invocations", result) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.