Skip to content

Commit a7b5e09

Browse files
committed
Delete inactive function versions at deploy time (#5927)
1 parent bb76b88 commit a7b5e09

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Delete all published versions of the AWS Lambda function except for the version specified to keep.
3+
"""
4+
import argparse
5+
import logging
6+
import sys
7+
8+
from azul import (
9+
R,
10+
config,
11+
)
12+
from azul.args import (
13+
AzulArgumentHelpFormatter,
14+
)
15+
from azul.lambdas import (
16+
Lambdas,
17+
)
18+
from azul.logging import (
19+
configure_script_logging,
20+
)
21+
22+
log = logging.getLogger(__name__)
23+
24+
25+
def main(argv: list[str]):
26+
assert config.terraform_component == '', R(
27+
'This script cannot be run with a Terraform component selected',
28+
config.terraform_component)
29+
parser = argparse.ArgumentParser(description=__doc__,
30+
formatter_class=AzulArgumentHelpFormatter)
31+
parser.add_argument('--function-name', '-f',
32+
required=True,
33+
help='The name of the Lambda function')
34+
parser.add_argument('--function-version', '-v',
35+
required=True,
36+
help='The Lambda function version to keep')
37+
args = parser.parse_args(argv)
38+
log.info('Deleting function %r versions other than %r',
39+
args.function_name, args.function_version)
40+
Lambdas().delete_other_function_versions(args.function_name, args.function_version)
41+
42+
43+
if __name__ == '__main__':
44+
configure_script_logging(log)
45+
main(sys.argv[1:])

src/azul/lambdas.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,44 @@ def _lambda(self):
106106
return aws.lambda_
107107

108108
def list_lambdas(self) -> list[Lambda]:
109-
# Note that this method returns the unpublished "$LATEST" version of
109+
# Note that this method returns the unpublished version ("$LATEST") of
110110
# each function.
111111
return [
112112
Lambda.from_response(function)
113113
for response in self._lambda.get_paginator('list_functions').paginate()
114114
for function in response['Functions']
115115
]
116116

117+
def delete_other_function_versions(self,
118+
function_name: str,
119+
keep_version: str) -> None:
120+
"""
121+
Delete all published versions of a Lambda function except for the
122+
version specified to keep.
123+
Raises an error if the specified version does not exist.
124+
125+
:param function_name: The fully qualified name of the function
126+
e.g. 'azul-service-dev'
127+
:param keep_version: The version of the function to not delete.
128+
"""
129+
paginator = self._lambda.get_paginator('list_versions_by_function')
130+
versions = [
131+
function['Version']
132+
for page in paginator.paginate(FunctionName=function_name)
133+
for function in page['Versions']
134+
if function['Version'] != '$LATEST' # unpublished version
135+
]
136+
try:
137+
versions.remove(keep_version)
138+
except ValueError:
139+
assert False, R(
140+
'Function version not found',
141+
function_name, keep_version, versions)
142+
for version in versions:
143+
log.info('Deleting version %r of %r', version, function_name)
144+
self._lambda.delete_function(FunctionName=function_name,
145+
Qualifier=version)
146+
117147
def manage_lambdas(self, enabled: bool):
118148
lambda_prefixes = [
119149
config.qualified_resource_name(lambda_infix)

terraform/api_gateway.tf.json.template.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,24 @@ def add_waf_blocked_alarm(resources: JSON) -> JSON:
778778
}
779779
for app in apps
780780
for resource_name in app.chalice.tf_function_resource_names
781+
},
782+
'terraform_data': {
783+
resource_name: {
784+
'triggers_replace': ['${aws_lambda_alias.%s.function_version}' % resource_name],
785+
'provisioner': {
786+
'local-exec': {
787+
# Script deletes any published version *except* the specified one
788+
'command': ' '.join([
789+
'python',
790+
f'{config.project_root}/scripts/delete_other_function_versions.py',
791+
'--function-name ${aws_lambda_alias.%s.function_name}' % resource_name,
792+
'--function-version ${aws_lambda_alias.%s.function_version}' % resource_name
793+
])
794+
}
795+
}
796+
}
797+
for app in apps
798+
for resource_name in app.chalice.tf_function_resource_names
781799
}
782800
}),
783801
*(

0 commit comments

Comments
 (0)