如何将 AWS Lambda 层大小减少到 250 MB 解压限制以下?

问题描述 投票:0回答:1

我正在使用 AWS Lambda,并遇到了一个问题:我的 Lambda 层超出了 250 MB 的解压缩大小限制。尽管将初始大小从 407 MB 减少到 300 MB,但仍然超出了限制。以下是我迄今为止为最小化图层大小所做的操作:

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 {} +

我已经使用以下命令安装了必要的 Python 包:

pip install -r requirements.txt -t python --no-cache-dir

我使用的软件包如下:

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

尽管做出了这些努力,我仍然无法达到 250 MB 的解压缩大小限制。这是我的问题:

  • 是否有任何其他策略或工具可以帮助进一步减小 Lambda 层的大小?
  • 某些包裹是否会不必要地增加体积?如何更有效地识别和清除它们?
  • 有没有办法优化Python环境本身变得更苗条?

任何有关如何解决此问题或优化我的设置的指导将不胜感激!

python aws-lambda cloud serverless
1个回答
0
投票

您可以手动梳理您使用的软件包,并将它们精简为 lambda 工作所严格需要的内容。 IE。如果您只使用庞大库中的一个模块,请准确找出该模块具有哪些依赖项,并从库中删除其他所有内容。我建议在本地进行此操作,因为会涉及很多尝试和错误。

或者考虑使用不同的环境来运行代码,例如容器映像。它配备了更多的设置,但从长远来看是值得的。

© www.soinside.com 2019 - 2024. All rights reserved.