Skip to content

Commit d18c854

Browse files
committed
fixup! Delete inactive function versions at deploy time (#5927)
1 parent f2478b2 commit d18c854

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

scripts/delete_other_function_versions.py renamed to scripts/delete_older_function_versions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ def main(argv: list[str]):
3535
required=True,
3636
help='The Lambda function version to keep')
3737
args = parser.parse_args(argv)
38-
log.info('Deleting function %r versions other than %r',
38+
log.info('Deleting function %r versions older than %r',
3939
args.function_name, args.function_version)
40-
Lambdas().delete_other_function_versions(args.function_name, args.function_version)
40+
Lambdas().delete_older_function_versions(args.function_name,
41+
args.function_version)
4142

4243

4344
if __name__ == '__main__':

src/azul/lambdas.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,11 @@ def list_lambdas(self) -> list[Lambda]:
114114
for function in response['Functions']
115115
]
116116

117-
def delete_other_function_versions(self,
117+
def delete_older_function_versions(self,
118118
function_name: str,
119119
keep_version: str) -> None:
120120
"""
121-
Delete all published versions of a Lambda function except for the
122-
version specified to keep.
121+
Delete all versions of a Lambda function prior to the specified one.
123122
Raises an error if the specified version does not exist.
124123
125124
:param function_name: The fully qualified name of the function
@@ -131,7 +130,10 @@ def delete_other_function_versions(self,
131130
function['Version']
132131
for page in paginator.paginate(FunctionName=function_name)
133132
for function in page['Versions']
134-
if function['Version'] != '$LATEST' # unpublished version
133+
if (
134+
function['Version'] != '$LATEST' # The so-called "unpublished" version
135+
and int(function['Version']) <= int(keep_version)
136+
)
135137
]
136138
try:
137139
versions.remove(keep_version)

terraform/api_gateway.tf.json.template.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,10 +784,9 @@ def add_waf_blocked_alarm(resources: JSON) -> JSON:
784784
'triggers_replace': ['${aws_lambda_alias.%s.function_version}' % resource_name],
785785
'provisioner': {
786786
'local-exec': {
787-
# Script deletes any published version *except* the specified one
788787
'command': ' '.join([
789788
'python',
790-
f'{config.project_root}/scripts/delete_other_function_versions.py',
789+
f'{config.project_root}/scripts/delete_older_function_versions.py',
791790
'--function-name ${aws_lambda_alias.%s.function_name}' % resource_name,
792791
'--function-version ${aws_lambda_alias.%s.function_version}' % resource_name
793792
])

0 commit comments

Comments
 (0)