首页 > 解决方案 > 如何在本地使用 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 上部署,但这是唯一的我现在看到的解决方案。

标签: amazon-web-servicesaws-lambdayamlamazon-cloudformationaws-sam

解决方案


您需要问的第一个问题是这些 lambda 是否属于同一个应用程序。如果不是这种情况,您应该使用不同的模板来部署不同的堆栈,从而拥有隔离的环境。

但是,如果要共享资源,则必须使用非常相似的选项:

  1. 在父模板中配置层,并将 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
  1. 在嵌套模板中配置图层,将 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 支持这两种方案。


推荐阅读