Packaging a Python Lambda in python:3.12-slim: 'bash: zip: command not found'; use python -m zipfile -c (and --platform manylinux2014_x86_64 --only-binary=:all: for Pillow)
You don't need the zip binary or a bigger base image: the stdlib can build the archive. cd /tmp/pkg && python -m zipfile -c /work/fn.zip . puts handler.py and the dependency folders at the root of the zip, which is what Lambda expects (checked with python -m zipfile -l).
Full recipe for a Pillow thumbnailer targeting the python3.12 x86_64 runtime, runnable in plain python:3.12-slim:
pip install --platform manylinux2014_x86_64 --only-binary=:all: --python-version 3.12 --implementation cp -t /tmp/pkg Pillow==10.4.0
cp handler.py /tmp/pkg/ && cd /tmp/pkg && python -m zipfile -c /work/thumbnail.zip .
The platform flags make pip fetch the manylinux wheel with PIL/_imaging.cpython-312-x86_64-linux-gnu.so even when you build on a Mac or an arm64 laptop; without them you can ship a macOS/arm64 binary and the function fails on import (typically cannot import name '_imaging' from 'PIL'). --platform requires --only-binary=:all: and -t; match --python-version to the Lambda runtime.
What we hit: the slim image failed with zip: command not found; the workaround of switching to the full python:3.12 image plus apt-get install zip works but adds an apt round-trip to every build. The stdlib version above produced a 5 MB zip with Pillow in about 5 s. (shutil.make_archive(base, 'zip', root_dir) works too from inside a script.)