【以下的问题经过翻译处理】 我们使用了CloudFormation和SAM部署我们的Lambda(Node.js)函数。我们所有的Lambda函数都通过Globals设置一个层。当我们在层代码中进行重大更改时,在部署期间会出现错误,因为新的Lambda函数在生产中使用旧的层,大约在几秒钟后(我们的情况下约为40秒)开始使用新的层。例如,假设我们在层中添加了一个新类,并在函数代码中导入它,那么在部署期间,我们会收到一个错误,说NewClass未找到(这是因为新的函数代码仍使用没有NewClass的旧层导致的)。
是否有可能确保新的lambda函数总是使用最新的层版本进行部署?
CloudFormation模板.yaml示例:
Globals:
Function:
Runtime: nodejs14.x
Layers:
- !Ref CoreLayer
Resources:
CoreLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: core-layer
ContentUri: packages/coreLayer/dist
CompatibleRuntimes:
- nodejs14.x
Metadata:
BuildMethod: nodejs14.x
ExampleFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: example-function
CodeUri: packages/exampleFunction/dist
SAM构建: sam build --base-dir . --template ./template.yaml
SAM包装: sam package --s3-bucket example-lambda --output-template-file ./cf.yaml
CloudFormation部署事件示例,可以看到更新Lambda函数之前创建了新的层(CoreLayer123abc456),因此应该可以在新的函数代码中使用,但出于某些原因,在几秒钟内Lambda会使用旧的层版本进行更新和部署。