Bug: Lambda function (with alias) doesn't create a new version after its layer is updated, even if AutoPublishAliasAllProperties
is set to true
#3612
-
Description:If we use alias for a lambda function (for provisioned concurrency, etc), it won't create a new version after its layer is updated, even if Steps to reproduce:
AWSTemplateFormatVersion: "2010-09-09"
Transform:
- AWS::Serverless-2016-10-31
Globals:
Api:
Cors:
AllowMethods: "'*'"
AllowHeaders: "'*'"
AllowOrigin: "'*'"
Function:
Runtime: nodejs20.x
Architectures:
- arm64
MemorySize: 128
Timeout: 8
Resources:
Layer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub ${AWS::StackName}-Layer
CompatibleArchitectures:
- arm64
CompatibleRuntimes:
- nodejs20.x
ContentUri: layer
RetentionPolicy: Delete
Metadata:
BuildMethod: nodejs20.x
BuildArchitecture: arm64
AppFunction:
Type: AWS::Serverless::Function
Properties:
AutoPublishAlias: Main
AutoPublishAliasAllProperties: true
CodeUri: lambda
Handler: app.handler
Layers:
- !Ref Layer
Events:
Api:
Type: Api
Properties:
Path: /app
Method: GET
Outputs:
URL:
Description: "Test URL"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/app"
import layer from "/opt/nodejs/layer.mjs";
export const handler = async (event, context) => {
const response = {
statusCode: 200,
body: JSON.stringify({fnVersion: process.env.AWS_LAMBDA_FUNCTION_VERSION, layer: layer})
};
return response;
};
export default "old layer";
version = 0.1
[default]
[default.global.parameters]
stack_name = "TestApp"
[default.build.parameters]
cached = true
parallel = true
[default.validate.parameters]
lint = true
[default.deploy.parameters]
capabilities = "CAPABILITY_NAMED_IAM"
confirm_changeset = true
resolve_s3 = true
s3_prefix = "TestApp"
image_repositories = []
[default.package.parameters]
resolve_s3 = true
[default.sync.parameters]
watch = true
[default.local_start_api.parameters]
warm_containers = "EAGER"
[default.local_start_lambda.parameters]
warm_containers = "EAGER" Observed result:In step 1, the result is: In step 2, the result remains the same. Expected result:I expect the result in step 2 to be Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
|
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
Hi @zhanzhenzhen, thanks for reporting the issue. After chatting with the SAM team, this seems like a known issue. I am transferring the ticket to their repo for the SAM team to provide more context and any workaround. |
Beta Was this translation helpful? Give feedback.
-
Hi @zhanzhenzhen, Context + Root causeThe reason the AutoPublishAliasAllProperties property is not picking up the changes to the Layers property is because of the specific ordering that we have when transforming the resources listed in a SAM template. The current implementation will translate all Because SAM doesn't resolve intrinsics, we wouldn't necessarily consider this a bug, but we can definitely understand that it is not a great experience for our customers. To address this issue with a fix, we would have to change the order in which resources are transformed which could result in unintended consequences. We have broken existing templates in the past so we try to tread carefully when making changes like this where it is difficult to estimate the full impact it would have on other customers. WorkaroundAs a workaround, you can pass a combined
The combined shasum can be passed in as a global paramater:
and this can be set with the Please give this a try and let me know if you have any other questions! |
Beta Was this translation helpful? Give feedback.
-
@paulhcsun Thank you for providing the workaround. Could you explain this in more detail:
I usually only use |
Beta Was this translation helpful? Give feedback.
-
But why it updates the layer version when the function is not using alias? |
Beta Was this translation helpful? Give feedback.
-
Sorry yes, If you are just manually deploying and want a simpler workaround, you could just add a
I'm not quite sure I follow your question here, could you elaborate what you mean? |
Beta Was this translation helpful? Give feedback.
-
It seems that if a lambda function isn't deployed with |
Beta Was this translation helpful? Give feedback.
-
Oh that's interesting, I'll try to investigate why that happens but at the moment I'm also not sure why it would properly sync with new layer versions if those properties are not specified. I'll move this into Discussion to collect more feedback from anyone facing this issue in the meantime. |
Beta Was this translation helpful? Give feedback.
-
Facing the same issue. Also when implementing the "Description workaround", it seems that SAM creates new Lambda version only if the hardcoded property value changes. For example, a new Lambda version is created if you add or change
But it won't work if you parameterize the
It's cumbersome to manually change the paulhcsun, can you confirm that SAM doesn't evaluate referenced values, but looks at the hardcoded property values to determine if a new Lambda version should be created? |
Beta Was this translation helpful? Give feedback.
Hi @zhanzhenzhen,
As @hnnasit mentioned we are currently aware of this issue. Here is some context and a workaround:
Context + Root cause
The reason the AutoPublishAliasAllProperties property is not picking up the changes to the Layers property is because of the specific ordering that we have when transforming the resources listed in a SAM template. The current implementation will translate all
AWS::Serverless::Functions
before translating theAWS::Serverless::LayerVersion
. Because theServerless::Function
is translated first, the!Ref
to theServerless::LayerVersion
will not have changed yet - that changes after we transform theLayerVersion
but by then it won't go back to transform theS…