@@ -23,12 +23,14 @@ class AliasVersionSyncFlow(SyncFlow):
2323
2424 _function_identifier : str
2525 _alias_name : str
26+ _delete_old_alias : bool
2627 _lambda_client : Any
2728
2829 def __init__ (
2930 self ,
3031 function_identifier : str ,
3132 alias_name : str ,
33+ delete_old_alias : bool ,
3234 build_context : "BuildContext" ,
3335 deploy_context : "DeployContext" ,
3436 sync_context : "SyncContext" ,
@@ -64,6 +66,7 @@ def __init__(
6466 self ._function_identifier = function_identifier
6567 self ._alias_name = alias_name
6668 self ._lambda_client = None
69+ self ._delete_old_alias = delete_old_alias
6770
6871 @property
6972 def sync_state_identifier (self ) -> str :
@@ -90,13 +93,18 @@ def compare_remote(self) -> bool:
9093
9194 def sync (self ) -> None :
9295 function_physical_id = self .get_physical_id (self ._function_identifier )
96+ current_alias_version = self ._get_version_alias_if_exists ()
9397 version = self ._lambda_client .publish_version (FunctionName = function_physical_id ).get ("Version" )
9498 self ._local_sha = str_checksum (str (version ), hashlib .sha256 ())
9599 LOG .debug ("%sCreated new function version: %s" , self .log_prefix , version )
96100 if version :
97101 self ._lambda_client .update_alias (
98102 FunctionName = function_physical_id , Name = self ._alias_name , FunctionVersion = version
99103 )
104+ if self ._delete_old_alias and current_alias_version :
105+ function_name_w_version = "{}:{}" .format (function_physical_id , current_alias_version )
106+ self ._lambda_client .delete_function (FunctionName = function_name_w_version )
107+
100108
101109 def gather_dependencies (self ) -> List [SyncFlow ]:
102110 return []
@@ -107,3 +115,11 @@ def _get_resource_api_calls(self) -> List[ResourceAPICall]:
107115 def _equality_keys (self ) -> Any :
108116 """Combination of function identifier and alias name can used to identify each unique SyncFlow"""
109117 return self ._function_identifier , self ._alias_name
118+
119+ def _get_version_alias_if_exists (self ) -> Optional [str ]:
120+ try :
121+ return str (self ._lambda_client .get_alias (FunctionName = self .get_physical_id (self ._function_identifier ),
122+ Name = self ._alias_name )
123+ .get ("FunctionVersion" ))
124+ except self ._lambda_client .exceptions .ResourceNotFoundException :
125+ return None
0 commit comments