I’m working with AWS Lambda and have encountered an issue where my Lambda layer exceeds the unzipped size limit of 250 MB. Despite reducing the initial size of 407 MB to 300 MB, I’m still above the limit. Here’s what I’ve done so far to minimize the layer’s size:
find python/ -name "*.txt" -type f -delete
find python/ -name "*.md" -type f -delete
find python/ -name "tests" -type d | xargs rm -rf
find python/ -name "*.dist-info" -type d | xargs rm -rf
find python/ -name "docs" -type d | xargs rm -rf
find python/ -name "examples" -type d | xargs rm -rf
find python/ -name "__pycache__" -type d | xargs rm -rf
find python/ -name "*.pyc" -type f -delete
find python/ -name "datasets" -type d -exec rm -rf {} +
I’ve installed the necessary Python packages using the following command:
pip install -r requirements.txt -t python --no-cache-dir
The packages I am using are as follows:
Successfully installed Pillow-10.3.0 cmdstanpy-1.2.2 colorama-0.4.6 contourpy-1.2.1 cycler-0.12.1 defusedxml-0.7.1 fonttools-4.51.0 fpdf2-2.7.8 holidays-0.47 importlib-resources-6.4.0 joblib-1.4.0 kiwisolver-1.4.5 matplotlib-3.8.3 numpy-1.26.4 packaging-24.0 pandas-2.2.1 prophet-1.1.5 pyparsing-3.1.2 python-dateutil-2.9.0.post0 pytz-2024.1 scikit-learn-1.4.1.post1 scipy-1.13.0 seaborn-0.13.2 six-1.16.0 stanio-0.5.0 threadpoolctl-3.4.0 tqdm-4.66.2 tzdata-2024.1
Despite these efforts, I’m still not able to get under the 250 MB unzipped size limit. Here are my questions:
- Are there any additional strategies or tools that could help further reduce the size of my Lambda layer?
- Could some packages be unnecessarily adding bulk? How can I identify and remove them more effectively?
- Is there a way to optimize the Python environment itself to be slimmer?
Any guidance on how to address this issue or optimize my setup would be greatly appreciated!