Skip to content

How to retain Lambda version when update

0

I'm using SAM to deploy lambda function with version. When redeploy function, I want to publish a new version for it. Please refer to SAM template file.

Function is deployed successfully and new version is created, but previous version (created by SAM) is deleted. I already set DeletionPolicy to Retain.

Seems DeletionPolicy does not work here? How to publish a new version but still keep previous version?

Resources:
  MyFunction:
    DeletionPolicy: Retain
    Type: AWS::Serverless::Function

  MyVersion:
    DeletionPolicy: Retain
    Type: AWS::Lambda::Version
    Properties:
      FunctionName: !Ref MyFunction

asked 2 years ago885 views

2 Answers
0

To retain previous versions of your Lambda function when updating with AWS SAM, you need to make a few adjustments to your approach. The issue you're experiencing is related to how SAM and Lambda versioning work together.

First, it's important to understand that Lambda versions are immutable. When you update a function, a new version is created, but it doesn't automatically retain old versions. The DeletionPolicy you've set on the function and version resources doesn't prevent this behavior.

To achieve what you want, you should use the AutoPublishAlias property in your AWS::Serverless::Function resource. This will automatically publish a new version and update an alias when your function code changes. Here's how you can modify your SAM template:

Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
# Your other function properties here
AutoPublishAlias: live

MyFunctionVersion:
Type: AWS::Lambda::Version
Properties:
FunctionName: !Ref MyFunction

With this setup, every time you deploy changes to your function, SAM will:

  1. Create a new version of your function
  2. Update the 'live' alias to point to the new version
  3. Retain the previous versions

The AutoPublishAlias property triggers the creation of new versions automatically when you update your function. Each version is immutable and will be retained.

If you want even more control over your versions, you can use the AWS::Lambda::Version resource to explicitly create versions. However, the AutoPublishAlias method is generally simpler and more automatic for most use cases.

Remember, while this approach will retain your function versions, it's important to manage these versions over time to avoid unnecessary costs. You may want to implement a cleanup strategy for very old versions that are no longer needed.
Sources
Step 3: Deploy the updated Lambda function - AWS CodeDeploy
Lambda version control - Serverless Applications Lens

answered 2 years ago

  • Better should have other option than AutoPublishAlias, I have nothing to do with Alias.

0

Could anyone help to answer this question?

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.