- Newest
- Most votes
- Most comments
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:
- Create a new version of your function
- Update the 'live' alias to point to the new version
- 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
Relevant content
asked a year ago

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