@@ -23,12 +23,14 @@ class AliasVersionSyncFlow(SyncFlow):
23
23
24
24
_function_identifier : str
25
25
_alias_name : str
26
+ _delete_old_alias : bool
26
27
_lambda_client : Any
27
28
28
29
def __init__ (
29
30
self ,
30
31
function_identifier : str ,
31
32
alias_name : str ,
33
+ delete_old_alias : bool ,
32
34
build_context : "BuildContext" ,
33
35
deploy_context : "DeployContext" ,
34
36
sync_context : "SyncContext" ,
@@ -64,6 +66,7 @@ def __init__(
64
66
self ._function_identifier = function_identifier
65
67
self ._alias_name = alias_name
66
68
self ._lambda_client = None
69
+ self ._delete_old_alias = delete_old_alias
67
70
68
71
@property
69
72
def sync_state_identifier (self ) -> str :
@@ -90,13 +93,18 @@ def compare_remote(self) -> bool:
90
93
91
94
def sync (self ) -> None :
92
95
function_physical_id = self .get_physical_id (self ._function_identifier )
96
+ current_alias_version = self ._get_version_alias_if_exists ()
93
97
version = self ._lambda_client .publish_version (FunctionName = function_physical_id ).get ("Version" )
94
98
self ._local_sha = str_checksum (str (version ), hashlib .sha256 ())
95
99
LOG .debug ("%sCreated new function version: %s" , self .log_prefix , version )
96
100
if version :
97
101
self ._lambda_client .update_alias (
98
102
FunctionName = function_physical_id , Name = self ._alias_name , FunctionVersion = version
99
103
)
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
+
100
108
101
109
def gather_dependencies (self ) -> List [SyncFlow ]:
102
110
return []
@@ -107,3 +115,11 @@ def _get_resource_api_calls(self) -> List[ResourceAPICall]:
107
115
def _equality_keys (self ) -> Any :
108
116
"""Combination of function identifier and alias name can used to identify each unique SyncFlow"""
109
117
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