Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support to delete old versions when successful deployment #7414

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

driverpt
Copy link

@driverpt driverpt commented Aug 27, 2024

Which issue(s) does this change fix?

#7398

Why is this change necessary?

Because there's a limit on how many versions a Lambda has.

How does it address the issue?

Automatically deletes old version. Very useful when on SnapStart

What side effects does this change have?

None, it's disabled by default

Mandatory Checklist

PRs will only be reviewed after checklist is complete

  • Add input/output type hints to new functions/methods
  • Write design document if needed (Do I need to write a design document?)
  • Write/update unit tests
  • Write/update integration tests
  • Write/update functional tests if needed
  • make pr passes
  • make update-reproducible-reqs if dependencies were changed
  • Write documentation

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@driverpt driverpt requested a review from a team as a code owner August 27, 2024 09:17
@github-actions github-actions bot added pr/external stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Aug 27, 2024
@jysheng123
Copy link
Contributor

Hi, Thanks for bringing up this PR. The code looks good to me so Ill run the approval workflow to get the testing process started, but can you also create an integration tests testing the happy path from beginning to end?

@jysheng123
Copy link
Contributor

Can you also run make black to fix the formatting changes?

Copy link
Contributor

@lucashuy lucashuy left a comment

Choose a reason for hiding this comment

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

Thanks for opening this PR, left some comments. Additionally, we might want to consider adding this to the sam deploy command. This might need some more thought though if this makes sense to you and the team.

@@ -109,11 +109,13 @@ def gather_dependencies(self) -> List[SyncFlow]:
raise FunctionNotFound(f"Unable to find function {self._function_identifier}")

auto_publish_alias_name = function_resource.get("Properties", dict()).get("AutoPublishAlias", None)
auto_delete_old_alias = function_resource.get("Properties", dict()).get("AutoDeleteOldAlias", False)
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like a new property for the AWS::Serverless::Function. This will require a SAM spec change if we wanted to use it like this.

Since deleting old Lambda versions is something that is being implemented as SAM CLI specific behaviour, placing it inside of the Metadata part of the function inside the template might be a better location.

I can also bring this up with the team about the best way to go about triggering the delete behaviour.

version = self._lambda_client.publish_version(FunctionName=function_physical_id).get("Version")
self._local_sha = str_checksum(str(version), hashlib.sha256())
LOG.debug("%sCreated new function version: %s", self.log_prefix, version)
if version:
self._lambda_client.update_alias(
FunctionName=function_physical_id, Name=self._alias_name, FunctionVersion=version
)
if self._delete_old_alias and current_alias_version:
function_name_w_version = "{}:{}".format(function_physical_id, current_alias_version)
self._lambda_client.delete_function(FunctionName=function_name_w_version)
Copy link
Contributor

Choose a reason for hiding this comment

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

Will it ever be possible for this to be my_function:$LATEST? Just wanted to ask what happens if the Lambda alias resolved to the latest version of the function, and if deleting my_function:$LATEST just deletes the function.

version = self._lambda_client.publish_version(FunctionName=function_physical_id).get("Version")
self._local_sha = str_checksum(str(version), hashlib.sha256())
LOG.debug("%sCreated new function version: %s", self.log_prefix, version)
if version:
self._lambda_client.update_alias(
FunctionName=function_physical_id, Name=self._alias_name, FunctionVersion=version
)
if self._delete_old_alias and current_alias_version:
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a log message here? This is deleting something, so we'll want to let people know that an old version is now being deleted.

version = self._lambda_client.publish_version(FunctionName=function_physical_id).get("Version")
self._local_sha = str_checksum(str(version), hashlib.sha256())
LOG.debug("%sCreated new function version: %s", self.log_prefix, version)
if version:
self._lambda_client.update_alias(
FunctionName=function_physical_id, Name=self._alias_name, FunctionVersion=version
)
if self._delete_old_alias and current_alias_version:
function_name_w_version = "{}:{}".format(function_physical_id, current_alias_version)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: changing _w_ to _with_, or renaming to something like formatted_function_name_version for future contributors that are not familiar with the "with" shorthand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/external stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants