Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions scripts/delete_older_function_versions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Delete all versions of a Lambda function prior to the specified one.
Delete Lambda function versions older than a specified version
"""
import argparse
import logging
Expand Down Expand Up @@ -36,13 +36,18 @@ def main(argv: list[str]):
parser.add_argument('--function-version', '-v',
type=int,
required=True,
help='The Lambda function version to keep. Must be an '
'integer.')
help='A numeric version of the Lambda function. The '
'specified version will be kept, as will '
'the preceding one. All versions preceding those '
'two versions will be deleted.')
args = parser.parse_args(argv)
log.info('Deleting function %r versions older than %r',
args.function_name, args.function_version)
functions = LambdaFunctions()
functions.delete_older_versions(args.function_name, args.function_version)
functions.delete_older_versions(args.function_name,
args.function_version,
# Keep a previous version to guard against a
# race condition due to eventual consistency
# of alias updates.
num_older_versions_to_keep=1)


if __name__ == '__main__':
Expand Down
19 changes: 16 additions & 3 deletions src/azul/lambdas.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,28 @@ def list_functions(self) -> list[LambdaFunction]:
for function in response['Functions']
]

def delete_older_versions(self, function_name: str, keep_version: int) -> None:
def delete_older_versions(self,
function_name: str,
function_version: int,
*,
num_older_versions_to_keep: int = 0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decide if you want to use retain or keep for your variable and argument names, and documentation, but be consistent.

) -> None:
"""
Delete all versions of a Lambda function prior to the specified one.
Delete Lambda function versions older than the specified version.

:param function_name: The fully qualified name of the function
e.g. 'azul-service-dev'

:param keep_version: The version of the function to not delete.
:param function_version: The version of the function to keep.

:param num_older_versions_to_keep: The number of versions older than
`function_version` to also keep.
"""
assert num_older_versions_to_keep >= 0, R(
'num_older_versions_to_keep must be non-negative', num_older_versions_to_keep)
keep_version = function_version - num_older_versions_to_keep
log.info('Deleting function %r versions older than %r',
function_name, keep_version)
paginator = self._lambda.get_paginator('list_versions_by_function')
versions = [
function['Version']
Expand Down
Loading