Skip to content

Commit

Permalink
Support function alias type resolving (#7833)
Browse files Browse the repository at this point in the history
Co-authored-by: Roger Zhang <[email protected]>
  • Loading branch information
rangerthegood and roger-zhangg authored Jan 25, 2025
1 parent f271745 commit 65d47c9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
13 changes: 11 additions & 2 deletions samcli/lib/intrinsic_resolver/intrinsics_symbol_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,24 @@ def get_default_pseudo_resolver(self):
def get_default_attribute_resolver(self):
return {"Ref": lambda logical_id: logical_id, "Arn": self.arn_resolver}

@staticmethod
def get_default_type_resolver():
def handle_function_alias_type(self, logical_id):
node = self._resources.get(logical_id).get("Properties").get("FunctionName")

function_id = self.resolve_symbols(node.get("Ref"), IntrinsicResolver.REF)
functionArn = self.arn_resolver(function_id)
return functionArn

def get_default_type_resolver(self):
return {
"AWS::ApiGateway::RestApi": {
"RootResourceId": "/" # It usually used as a reference to the parent id of the RestApi,
},
"AWS::Lambda::LayerVersion": {
IntrinsicResolver.REF: lambda logical_id: {IntrinsicResolver.REF: logical_id}
},
"AWS::Lambda::Alias": {
IntrinsicResolver.REF: self.handle_function_alias_type,
},
"AWS::Serverless::LayerVersion": {
IntrinsicResolver.REF: lambda logical_id: {IntrinsicResolver.REF: logical_id}
},
Expand Down
22 changes: 20 additions & 2 deletions tests/unit/lib/intrinsic_resolver/test_intrinsics_symbol_table.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from unittest import TestCase

from unittest.mock import patch

from parameterized import parameterized

from samcli.lib.intrinsic_resolver.invalid_intrinsic_exception import InvalidSymbolException
from samcli.lib.intrinsic_resolver.intrinsic_property_resolver import IntrinsicResolver
from samcli.lib.intrinsic_resolver.intrinsics_symbol_table import IntrinsicsSymbolTable
from samcli.lib.intrinsic_resolver.invalid_intrinsic_exception import InvalidSymbolException


class TestIntrinsicsSymbolTablePseudoProperties(TestCase):
Expand Down Expand Up @@ -91,6 +90,25 @@ def test_default_type_resolver_function(self):

self.assertEqual(result, "MyApi")

def test_default_type_resolver_function_alias(self):
template = {
"Resources": {
"TestFunction17441592": {
"Type": "AWS::Lambda::Function",
},
"TestFunctionAlias31D71541": {
"Type": "AWS::Lambda::Alias",
"Properties": {
"FunctionName": {"Ref": "TestFunction17441592"},
},
},
}
}
symbol_resolver = IntrinsicsSymbolTable(template=template)
result = symbol_resolver.resolve_symbols("TestFunctionAlias31D71541", "Ref")

self.assertEqual(result, "arn:aws:lambda:us-east-1:123456789012:function:TestFunction17441592")

def test_custom_attribute_resolver(self):
template = {"Resources": {"MyApi": {"Type": "AWS::ApiGateway::RestApi"}}}
common_attribute_resolver = {"Arn": "test"}
Expand Down

0 comments on commit 65d47c9

Please sign in to comment.