'Maximum size of the function in lambda@edge

I have two serverless functions that are executed in lambda@edge given two cloudfront events: viewer-request and origin-response, I want to implement a nodejs sharp library to scale these images on the fly given some parameters received in the querystring.

The problem is that when I install the mentioned dependency, the deployment package is greater than 9MB, and according to the edge documentation, the functions must be <= 1MB.

Can I do something about it to fix this?

This is my serverless.yml file.

service: scale-otf

provider:
  name: aws
  timeout: 5
  memorySize: 128
  runtime: nodejs14.x
  lambdaHashingVersion: 20201221
  iam:
    role: LambdaEdgeRole

plugins:
  - serverless-lambda-edge-pre-existing-cloudfront

functions:
  request:
    handler: dist/request.handler
    events:
      - preExistingCloudFront:
          distributionId: <id-of-my-existing-distribution>
          eventType: viewer-request
          pathPattern: '*'
          includeBody: false

  response:
    handler: dist/response.handler
    events:
      - preExistingCloudFront:
          distributionId: <id-of-my-existing-distribution>
          eventType: origin-response
          pathPattern: '*'
          includeBody: false

package:
  patterns:
    - '!**/**'
    - 'dist/**'

# Create a Lambda@Edge function via the wizard in Lambda Console
# and then copied the role and pasted it here
resources:
  Resources:
    LambdaEdgeRole:
      Type: AWS::IAM::Role
      Properties:
        AssumeRolePolicyDocument:
          Statement:
          - Effect: Allow
            Principal:
              Service:
                - edgelambda.amazonaws.com
                - lambda.amazonaws.com
            Action:
            - sts:AssumeRole
        Policies:
          - PolicyName: LambdaEdgeExecutionRole
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                - Effect: Allow
                  Action:
                    - logs:CreateLogGroup
                    - logs:CreateLogStream
                    - logs:PutLogEvents
                  Resource: "arn:aws:logs:*:*:*"



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source