'Unzipped size must be smaller than 262144000 bytes - AWS Lambda Error

I have tried to upload my application using servless/lambda function AWS, but i got this issue:

An error occurred: AppLambdaFunction - Unzipped size must be smaller than 262144000 bytes (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException; Request ID: 8ea0d887-5743-4db1-96cd-6c5efa57b081).

What is the best way to resolve it?

Look my dependencies:

  "dependencies": {
    "ethereumjs-tx": "^1.3.7",
    "aws-sdk": "^2.4.52",
    "body-parser": "^1.18.3",
    "compression": "^1.7.4",
    "consign": "^0.1.6",
    "cors": "^2.8.5",
    "express": "^4.16.4",
    "helmet": "^3.16.0",
    "moment": "^2.24.0",
    "openzeppelin-solidity": "^2.3.0",
    "serverless": "^1.48.2",
    "serverless-http": "^1.9.1",
    "serverless-offline": "^4.9.4",
    "truffle": "^5.1.9",
    "truffle-hdwallet-provider": "^1.0.17",
    "web3": "^1.2.5-rc.0"
  },

Serverless.yml:

provider:
  name: aws
  runtime: nodejs8.10
  stage: v1
  region: us-east-1
  timeout: 30
  memorySize: 512
  package:
    excludeDevDependencies: true
    exclude:
      - .git/**
      - .vscode/**        
      - venv/**

functions:
  app:  
    handler: handler.run
    events:
      - http:
          path: /
          method: ANY
          cors: true
      - http:
          path: /{proxy+}
          method: ANY
          cors: true

plugins:
  - serverless-offline  


Solution 1:[1]

Use the directive exclude at your serverless.yml file. In case of Python, I've been used it as follows:

package:
  exclude:
    - node_modules/**
    - venv/**

The build process will exclude them from the build before sending to AWS.

Tip I got in this issue at Github. The documentation for this directive is detailed here.

Solution 2:[2]

You can use module bundlers to package the code.

Using module bundlers such as webpack

You can consider using plugins like serverless-webpack. The serverless-webpack plugin is using webpack to build the project and it will only include the bare minimum files required to run your application. It will not include the entire node_modules directory. so that your deployment package will be smaller.

a note about using of Lambda layers

Like others mentioned, you can use the layers and move some of the libraries and code to the layer. Layers are mainly used to share code between functions. The unzipped deployed package including layers cannot exceed 250MB.

hope this helps.

References:

https://github.com/serverless-heaven/serverless-webpack

https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path

Solution 3:[3]

You can load larger packages into AWS Lambda indirectly using s3:

  • Load your package into a bucket/key on S3
  • In the Lambda console choose Function Code -> Code Entry Type -> Upload a file from S3

Solution 4:[4]

I've had success resolving this error message using the serverless-esbuild plugin, and configuring it as follows in serverless.yml:


service: service_name
frameworkVersion: '3'
provider:
  name: aws
  runtime: nodejs12.x

plugins:
  - serverless-esbuild

custom:
  esbuild:
    bundle: true
    minify: false
    sourcemap: true
    exclude: 'aws-sdk'
    target: node14
    define:
      'require.resolve': undefined
    platform: node
    concurrency: 10

Solution 5:[5]

See my answer here

You can deploy a Lambda function using a Docker image and it bypasses this problem, allowing a function with its dependencies to be as large as 10 gb.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Arun Kamalanathan
Solution 3 debug
Solution 4 user3264341
Solution 5 Wesley Cheek