Reduce file size for python-based AWS lambda functions
30 Jan 2024When deploying lambda functions based on python that require sizeable dependencies like pandas or numpy, one can easily hit the max uncompressed filesize for lambda functions.
This is indicated by the error Unzipped size must be smaller than 262144000 bytes
.
Most articles suggest using docker or AWS layers. I dislike Docker and AWS seems to calculate the total size of the layers together with the deployment package, so the sum total is still the same.
What made a big difference in the end was adding some instructions to my buildspec.yml
that strip all libraries of debug symbols and remove some caches that are not required after the compilation. I also added some CFLAGs that promise to produce a more compact output. This is the resulting file:
phases
install:
commands:
- ...
- CFLAGS="-Os -g0 -Wl --strip-all" pip install --no-cache-dir --prefer-binary --compile -r requirements.txt -t /tmp/package
- find /tmp/package/ -name '*.so' -type f -exec strip "{}" \;
- find /tmp/package -wholename '*/tests/*' -type f -delete
- find /tmp/package/ -regex '^.*\(__pycache__\|\.py[co]\)$' -delete
build:
...
This reduced the total package of my lambda package from 256MB to 178MB uncompressed and from 65MB to 57MB compressed.