1
1
""" SAM macro definitions """
2
2
3
3
import copy
4
+ import re
4
5
from contextlib import suppress
5
6
from typing import Any , Callable , Dict , List , Literal , Optional , Tuple , Union , cast
6
7
@@ -238,7 +239,6 @@ class SamFunction(SamResourceMacro):
238
239
239
240
# DeadLetterQueue
240
241
dead_letter_queue_policy_actions = {"SQS" : "sqs:SendMessage" , "SNS" : "sns:Publish" }
241
- #
242
242
243
243
# Conditions
244
244
conditions : Dict [str , Any ] = {} # TODO: Replace `Any` with something more specific
@@ -325,7 +325,7 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def] # noqa: P
325
325
resources .append (url_permission )
326
326
327
327
self ._validate_deployment_preference_and_add_update_policy (
328
- kwargs .get ("deployment_preference_collection" , None ),
328
+ kwargs .get ("deployment_preference_collection" ),
329
329
lambda_alias ,
330
330
intrinsics_resolver ,
331
331
cast (IntrinsicsResolver , mappings_resolver ), # TODO: better handle mappings_resolver's Optional
@@ -1002,7 +1002,22 @@ def _construct_alias(self, name: str, function: LambdaFunction, version: LambdaV
1002
1002
if not name :
1003
1003
raise InvalidResourceException (self .logical_id , "Alias name is required to create an alias" )
1004
1004
1005
- logical_id = f"{ function .logical_id } Alias{ name } "
1005
+ # Validate alias name against the required pattern: (?!^[0-9]+$)([a-zA-Z0-9-_]+)
1006
+ # This ensures the alias name:
1007
+ # 1. Contains only alphanumeric characters, hyphens, and underscores
1008
+ # 2. Is not purely numeric
1009
+ ALIAS_REGEX = r"(?!^[0-9]+$)([a-zA-Z0-9\-_]+)$"
1010
+ if not re .match (ALIAS_REGEX , name ):
1011
+ raise InvalidResourceException (
1012
+ self .logical_id ,
1013
+ f"AutoPublishAlias name ('{ name } ') must contain only alphanumeric characters, hyphens, or underscores matching (?!^[0-9]+$)([a-zA-Z0-9-_]+) pattern." ,
1014
+ )
1015
+
1016
+ # Strip hyphens and underscores from the alias name for the logical ID
1017
+ # This ensures the logical ID contains only alphanumeric characters
1018
+ alias_alphanumeric_name = name .replace ("-" , "D" ).replace ("_" , "U" )
1019
+
1020
+ logical_id = f"{ function .logical_id } Alias{ alias_alphanumeric_name } "
1006
1021
alias = LambdaAlias (logical_id = logical_id , attributes = self .get_passthrough_resource_attributes ())
1007
1022
alias .Name = name
1008
1023
alias .FunctionName = function .get_runtime_attr ("name" )
@@ -1014,7 +1029,7 @@ def _construct_alias(self, name: str, function: LambdaFunction, version: LambdaV
1014
1029
1015
1030
def _validate_deployment_preference_and_add_update_policy ( # noqa: PLR0913
1016
1031
self ,
1017
- deployment_preference_collection : DeploymentPreferenceCollection ,
1032
+ deployment_preference_collection : Optional [ DeploymentPreferenceCollection ] ,
1018
1033
lambda_alias : Optional [LambdaAlias ],
1019
1034
intrinsics_resolver : IntrinsicsResolver ,
1020
1035
mappings_resolver : IntrinsicsResolver ,
0 commit comments