Skip to content

Commit 5ac2d05

Browse files
committed
fixup! Fix: 502 response caused by Function not found during alias updates (#7883)
1 parent 0a773c3 commit 5ac2d05

3 files changed

Lines changed: 13 additions & 22 deletions

File tree

scripts/delete_older_function_versions.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,12 @@ def main(argv: list[str]):
3636
parser.add_argument('--function-version', '-v',
3737
type=int,
3838
required=True,
39-
help='The version of the function to retain. Versions '
40-
'older than this (minus any buffer) will be '
41-
'deleted.')
42-
parser.add_argument('--buffer', '-b',
43-
type=int,
44-
default=0,
45-
help='Number of older versions to retain in addition '
46-
'to the specified version.')
39+
help='The version of the function to retain, along '
40+
'with the preceding one. All older versions will '
41+
'be deleted.')
4742
args = parser.parse_args(argv)
48-
assert args.buffer >= 0, R(
49-
'The buffer size cannot be less than zero.', args.buffer)
50-
retain_version = args.function_version - args.buffer
51-
log.info('Deleting function %r versions older than %r',
52-
args.function_name, retain_version)
5343
functions = LambdaFunctions()
54-
functions.delete_older_versions(args.function_name, retain_version)
44+
functions.delete_older_versions(args.function_name, args.function_version)
5545

5646

5747
if __name__ == '__main__':

src/azul/lambdas.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,22 @@ def delete_older_versions(self,
126126
:param function_name: The fully qualified name of the function
127127
e.g. 'azul-service-dev'
128128
129-
:param function_version: The version of the function to retain. Versions
130-
older than this will be deleted.
129+
:param function_version: The version of the function to retain, along
130+
with the preceding one.
131131
"""
132+
# We retain one previous version to guard against a race condition
133+
# caused by the eventual consistency of Lambda alias updates.
134+
retain_version = function_version - 1
135+
log.info('Deleting function %r versions older than %r',
136+
function_name, retain_version)
132137
paginator = self._lambda.get_paginator('list_versions_by_function')
133138
versions = [
134139
function['Version']
135140
for page in paginator.paginate(FunctionName=function_name)
136141
for function in page['Versions']
137142
if (
138143
function['Version'] != '$LATEST' # The so-called "unpublished" version
139-
and int(function['Version']) < function_version
144+
and int(function['Version']) < retain_version
140145
)
141146
]
142147
for version in versions:

terraform/api_gateway.tf.json.template.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -799,11 +799,7 @@ def add_waf_blocked_alarm(resources: JSON) -> JSON:
799799
'python',
800800
f'{config.project_root}/scripts/delete_older_function_versions.py',
801801
'--function-name ${aws_lambda_alias.%s.function_name}' % resource_name,
802-
'--function-version ${aws_lambda_alias.%s.function_version}' % resource_name,
803-
# Retain a previous version to guard against the
804-
# race condition caused by eventual consistency
805-
# of Lambda alias updates.
806-
'--buffer 1'
802+
'--function-version ${aws_lambda_alias.%s.function_version}' % resource_name
807803
])
808804
}
809805
}

0 commit comments

Comments
 (0)