Search code examples
aws-lambdaserverlessaws-serverlessaws-lambda-edge

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


I have created an S3 website and have wired up some Cloufront events using the '@silvermine/serverless-plugin-cloudfront-lambda-edge' plugin which both work as expected:

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'

I now want to introduce regular lambda@edge function which is just used for the processing of a contact form on the website.

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

What happens now is that I get the following error when deploying:

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

I am thinking that this is because my mail function has some dependencies which I want deploying with the mail function but not to my viewer-request and origin-request functions however these (I think!) are being deployed along with them - resulting in the large file size. The cloudfront event functions have no dependencies. How can I seperate the dependencies so that only the required ones are bundled and deployed?

For reference (if useful), my package.json looks like this:

  "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"
  }

Solution

  • You can specify functions to be packaged indiviudally and set includes/exclude on a per function basis with:

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

    This results in:

    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