与常规 lambda 函数结合使用时,Cloudfront 事件函数的大小太大

Size of Cloudfront event function too big when combined with regular lambda function

我创建了一个 S3 网站并使用“@silvermine/serverless-plugin-cloudfront-lambda-edge”插件连接了一些 Cloofront 事件,这两个事件都按预期工作:

functions:
  origin_request:
    handler: handler.origin_request
    memorySize: 128
    timeout: 1
    lambdaAtEdge:
      distribution: 'StaticSiteCloudfront'
      eventType: 'origin-request'
  viewer_request:
    handler: handler.viewer_request
    memorySize: 128
    timeout: 1
    lambdaAtEdge:
      distribution: 'StaticSiteCloudfront'
      eventType: 'viewer-request'

我现在想介绍一下常规的 lambda@edge 函数,它仅用于处理网站上的联系表单。

functions:
  ...
  hello:
    handler: mail.send_mail
    timeout: 10
    memorySize: 256
    events:
      - http:
          path: mail
          method: post

现在发生的是我在部署时收到以下错误:

An error occurred: StaticSiteCloudfront - The function code size is larger than the maximum allowed size for functions that are triggered by a CloudFront event: 2908922 Max allowed: 1048576 Function: arn:aws:lambda:us-east-1:12345678001:function:myywebsite-dev-viewer_request:15

我认为这是因为我的邮件功能有一些依赖项,我想将其与邮件功能一起部署,而不是我的查看器请求和原始请求功能,但是这些(我认为!)正在与他们 - 导致大文件大小。云端事件函数没有依赖关系。我怎样才能分离依赖项,以便只捆绑和部署所需的依赖项?

供参考(如果有用),我的 package.json 看起来像这样:

  "devDependencies": {
    "@silvermine/serverless-plugin-cloudfront-lambda-edge": "^2.1.1",
    "serverless-s3-sync": "^1.15.0"
  },
  **** only the following deps are needed for the "mail" function ****
  "dependencies": {
    "mailgun-js": "^0.22.0",
    "validator": "^13.1.17"
  }

您可以指定要单独打包的函数,并在每个函数的基础上设置 includes/exclude:

package:
  individually: true
  exclude:
    - node_modules/**

这导致:

functions:
  origin_request:
    handler: handler.origin_request
    memorySize: 128
    timeout: 1
    # specify exclusions
    package:
      individually: true
      exclude:
        - node_modules/**
    lambdaAtEdge:
      distribution: 'StaticSiteCloudfront'
      eventType: 'origin-request'
  viewer_request:
    handler: handler.viewer_request
    memorySize: 128
    timeout: 1
    # specify exclusions
    package:
      individually: true
      exclude:
        - node_modules/**
    lambdaAtEdge:
      distribution: 'StaticSiteCloudfront'
      eventType: 'viewer-request'
  mail:
    handler: mail.send_mail
    timeout: 10
    memorySize: 256
    events:
      - http:
          path: mail
          method: post