I am developing one lambda function, which use the ResumeParser library made in the python 2.7. But when I deploy this function including the library on the AWS it's throwing me following error:
Unzipped size must be smaller than 262144000 bytes
Perhaps you did not exclude development packages which made your file to grow that big.
I my case, (for NodeJS) I had missing the following in my serverless.yml:
package: exclude: - node_modules/** - venv/** See if there are similar for Python or your case.
aws-sdk are installed as dev dependencies will mean that you get code completion without the bloat in your package.aws-sdk as dev dependency but not as actual dependency?The best solution to this problem is to deploy your Lambda function using a Docker container that you've built and pushed to AWS ECR. Lambda container images have a limit of 10 gb.
Here's an example using Python flavored AWS CDK
from aws_cdk import aws_lambda as _lambda self.lambda_from_image = _lambda.DockerImageFunction( scope=self, id="LambdaImageExample", function_name="LambdaImageExample", code=_lambda.DockerImageCode.from_image_asset( directory="lambda_funcs/LambdaImageExample" ), ) An example Dockerfile contained in the directory lambda_funcs/LambdaImageExample alongside my lambda_func.py and requirements.txt:
FROM amazon/aws-lambda-python:latest LABEL maintainer="Wesley Cheek" RUN yum update -y && \ yum install -y python3 python3-dev python3-pip gcc && \ rm -Rf /var/cache/yum COPY requirements.txt ./ RUN pip install -r requirements.txt COPY lambda_func.py ./ CMD ["lambda_func.handler"] Run cdk deploy and the Lambda function will be automagically bundled into an image along with its dependencies specified in requirements.txt, pushed to an AWS ECR repository, and deployed.
This Medium post was my main inspiration
Edit:
(More details about this solution can be found in my Dev.to post here)
pandas and scipy for example, max image size shows as 1.5GB. I was able to deploy to aws lambda using the DockerImageFunction approach as outlined here.This is a hard limit which cannot be changed:
AWS Lambda Limit Errors
Functions that exceed any of the limits listed in the previous limits tables will fail with an exceeded limits exception. These limits are fixed and cannot be changed at this time. For example, if you receive the exception CodeStorageExceededException or an error message similar to "Code storage limit exceeded" from AWS Lambda, you need to reduce the size of your code storage.
You need to reduce the size of your package. If you have large binaries place them in s3 and download on bootstrap. Likewise for dependencies, you can pip install or easy_install them from an s3 location which will be faster than pulling from pip repos.
Unzipped size must be smaller than 130091036 bytes :(As stated by Greg Wozniak, you may just have imported useless directories like venv and node_modules.
package.exclude is now deprecated and removed in serverless 4, you should now use package.patterns instead:
package: patterns: - '!node_modules/**' - '!venv/**' package: exclude: and adding - "**/venv/**" totally worked, huzzahNote that boto3 is included in a Lambda by AWS so you shouldn't include it explicitly in the requirements file. It is a common reason of causing a Lambda to exceed its max limit.
A workaround that worked for me: Install pyminifier:
pip install pyminifier Go to the library folder that you want to zip. In my case I wanted to zip the site-packages folder in my virtual env. So I created a site-packages-min folder at the same level where site-packages was. Run the following shell script to minify the python files and create identical structure in the site-packages-min folder. Zip and upload these files to S3.
#/bin/bash for f in $(find site-packages -name '*.py') do ori=$f res=${f/site-packages/site-packages-min} filename=$(echo $res| awk -F"/" '{print $NF}') echo "$filename" path=${res%$filename} mkdir -p $path touch $res pyminifier --destdir=$path $ori >> $res || cp $ori $res done HTH
Just if someone is still having this issue.
I was receiving this error when deploying a single function using Serverless 3.37.0 and the esbuild plugin.
I tried everything found related to node-modules, but nothing worked. What worked for me was to delete the .zip files of the lambda functions on my project located at the .Serverless and .esbuild/.Serverless folders.