amazon-web-services - 如何在本地使用 SAM 以及在部署时重用 AWS 上的层?
问题描述
到目前为止,我有这个template.yml
:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
My Lambda for doing something
Resources:
FirstLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: FirstLayer
Description: First layer of dependencies
ContentUri: layers/first-layer/
CompatibleRuntimes:
- nodejs14.x
Metadata:
BuildMethod: nodejs14.x
SecondLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: SecondLayer
Description: Second layer of dependencies
ContentUri: layers/second-layer/
CompatibleRuntimes:
- nodejs14.x
Metadata:
BuildMethod: nodejs14.x
MyLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: "MyLambda"
Policies:
- AmazonS3FullAccess
CodeUri: src/
Handler: lambda.handler
Timeout: 30
MemorySize: 2048 # Chrome will require higher memory
Runtime: nodejs14.x
Layers:
- !Ref FirstLayer
- !Ref SecondLayer
使用此模板,我可以在MyLambda
本地启动和调用并将其部署到 AWS。我遇到的问题是我也想在其他 Lambda 上重用这些相同的层,因此我可以简单地将这些层提取到另一个yml
文件中,单独部署它们,然后将层 ARN 包含在Layers
我的 Lambda 的属性中,但是,我怎样才能在本地运行它sam
呢?我不想template.yml
为我的 Lambda 提供 2 个文件,一个包括在Resources
本地运行的层(就像我已经拥有的一样),另一个包含对实际层 ARN 的引用以在 AWS 上部署,但这是唯一的我现在看到的解决方案。
解决方案
您需要问的第一个问题是这些 lambda 是否属于同一个应用程序。如果不是这种情况,您应该使用不同的模板来部署不同的堆栈,从而拥有隔离的环境。
但是,如果要共享资源,则必须使用非常相似的选项:
- 在父模板中配置层,并将 ARN 作为参数传递。
模板.yml
Resources:
SharedLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: shared_layer
Description: Some code to share with the other lambda functions
ContentUri: ./layer
CompatibleRuntimes:
- nodejs14.x
RetentionPolicy: Delete
Application:
Type: "AWS::Serverless::Application"
Properties:
Location: "./app.template.yml"
Parameters:
SharedLayer: !Ref SharedLayer
app.template.yml
Parameters:
SharedLayer:
Type: String
Description: ARN of the SharedLayer
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./
Handler: index.handler
Layers:
- !Ref SharedLayer
- 在嵌套模板中配置图层,将 ARN 设置为输出,然后将其输出作为参数传递给其他模板。
层.模板.yml
Resources:
SharedLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: shared_layer
Description: Some code to share with the other lambda functions
ContentUri: ./layer
CompatibleRuntimes:
- nodejs14.x
RetentionPolicy: Delete
Outputs:
SharedLayerARN:
Description: ARN of the Shared Layer
Value: !Ref SharedLayer
模板.yml
Layer:
Type: "AWS::Serverless::Application"
Properties:
Location: "./layers.template.yml"
Application:
Type: "AWS::Serverless::Application"
Properties:
Location: "./app.template.yml"
Parameters:
SharedLayer: !GetAtt Layer.Outputs.SharedLayerARN
AWS SAM 支持这两种方案。
推荐阅读
- sql - Laravel Query Builder 中的第一个字母如何计算?
- c++ - std::strlen 如何在内部工作?
- haskell - 在 Haskell 中制作二叉树的问题
- c# - C# SIMD 向量
未正确初始化 - python - 如何获取在 django admin 中注册的模型列表?
- python - python中带有组名的正则表达式
- azure - Azure Key Vault 从 VMSS 实例获取机密
- regex - 同一行中的相似正则表达式
- postgresql - IN 关键字所需的数据类型是什么
- apache-poi - 将 apache-poi 从 3.17 迁移到 4.1.1 的问题