8181from samtranslator .model .intrinsics import (
8282 fnGetAtt ,
8383 fnSub ,
84+ get_logical_id_from_intrinsic ,
8485 is_intrinsic ,
8586 is_intrinsic_if ,
8687 is_intrinsic_no_value ,
@@ -265,6 +266,7 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def] # noqa: P
265266 """
266267 resources : List [Any ] = []
267268 intrinsics_resolver : IntrinsicsResolver = kwargs ["intrinsics_resolver" ]
269+ resource_resolver : ResourceResolver = kwargs ["resource_resolver" ]
268270 mappings_resolver : Optional [IntrinsicsResolver ] = kwargs .get ("mappings_resolver" )
269271 conditions = kwargs .get ("conditions" , {})
270272 feature_toggle = kwargs .get ("feature_toggle" )
@@ -303,7 +305,10 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def] # noqa: P
303305 else :
304306 lambda_function .Description = {"Fn::Join" : [" " , [description , code_sha256 ]]}
305307 lambda_version = self ._construct_version (
306- lambda_function , intrinsics_resolver = intrinsics_resolver , code_sha256 = code_sha256
308+ lambda_function ,
309+ intrinsics_resolver = intrinsics_resolver ,
310+ resource_resolver = resource_resolver ,
311+ code_sha256 = code_sha256 ,
307312 )
308313 lambda_alias = self ._construct_alias (alias_name , lambda_function , lambda_version )
309314 resources .append (lambda_version )
@@ -882,8 +887,12 @@ def _construct_inline_code(*args: Any, **kwargs: Dict[str, Any]) -> Dict[str, An
882887 dispatch_function : Callable [..., Dict [str , Any ]] = artifact_dispatch [filtered_key ]
883888 return dispatch_function (artifacts [filtered_key ], self .logical_id , filtered_key )
884889
885- def _construct_version (
886- self , function : LambdaFunction , intrinsics_resolver : IntrinsicsResolver , code_sha256 : Optional [str ] = None
890+ def _construct_version ( # noqa: PLR0912
891+ self ,
892+ function : LambdaFunction ,
893+ intrinsics_resolver : IntrinsicsResolver ,
894+ resource_resolver : ResourceResolver ,
895+ code_sha256 : Optional [str ] = None ,
887896 ) -> LambdaVersion :
888897 """Constructs a Lambda Version resource that will be auto-published when CodeUri of the function changes.
889898 Old versions will not be deleted without a direct reference from the CloudFormation template.
@@ -929,6 +938,26 @@ def _construct_version(
929938 # property that when set to true would change the lambda version whenever a property in the lambda function changes
930939 if self .AutoPublishAliasAllProperties :
931940 properties = function ._generate_resource_dict ().get ("Properties" , {})
941+
942+ # When a Lambda LayerVersion resource is updated, a new Lambda layer is created.
943+ # However, we need the Lambda function to automatically create a new version
944+ # and use the new layer. By setting the `PublishLambdaVersion` property to true,
945+ # a new Lambda function version will be created when the layer version is updated.
946+ if function .Layers :
947+ for layer in function .Layers :
948+ layer_logical_id = get_logical_id_from_intrinsic (layer )
949+ if not layer_logical_id :
950+ continue
951+
952+ layer_resource = resource_resolver .get_resource_by_logical_id (layer_logical_id )
953+ if not layer_resource :
954+ continue
955+
956+ layer_properties = layer_resource .get ("Properties" , {})
957+ publish_lambda_version = layer_properties .get ("PublishLambdaVersion" , False )
958+ if publish_lambda_version :
959+ properties .update ({layer_logical_id : layer_properties })
960+
932961 logical_dict = properties
933962 else :
934963 with suppress (AttributeError , UnboundLocalError ):
@@ -1596,6 +1625,7 @@ class SamLayerVersion(SamResourceMacro):
15961625 property_types = {
15971626 "LayerName" : PropertyType (False , one_of (IS_STR , IS_DICT )),
15981627 "Description" : PropertyType (False , IS_STR ),
1628+ "PublishLambdaVersion" : PropertyType (False , IS_BOOL ),
15991629 "ContentUri" : PropertyType (True , one_of (IS_STR , IS_DICT )),
16001630 "CompatibleArchitectures" : PropertyType (False , list_of (one_of (IS_STR , IS_DICT ))),
16011631 "CompatibleRuntimes" : PropertyType (False , list_of (one_of (IS_STR , IS_DICT ))),
@@ -1605,6 +1635,7 @@ class SamLayerVersion(SamResourceMacro):
16051635
16061636 LayerName : Optional [Intrinsicable [str ]]
16071637 Description : Optional [Intrinsicable [str ]]
1638+ PublishLambdaVersion : Optional [bool ]
16081639 ContentUri : Dict [str , Any ]
16091640 CompatibleArchitectures : Optional [List [Any ]]
16101641 CompatibleRuntimes : Optional [List [Any ]]
0 commit comments