Sub topic 11
Packaging code: zip archives, layers and container images
This chapter assumes you can write a handler, set a function's memory and timeout, and run aws CLI commands. It covers the three ways to get code into Lambda and when to choose each.
The three package types
Lambda accepts your code in one of two package types. A zip archive can also be extended with layers.
| Option | Size limit | Best for |
|---|---|---|
| Zip archive | 50 MB zipped upload, 250 MB unzipped including layers | Small functions with few dependencies |
| Zip archive plus layers | Same 250 MB unzipped total | Dependencies shared across functions |
| Container image | 10 GB image | Large dependencies, custom tooling, existing container workflows |
The package type is fixed when you create the function. You cannot convert a zip function to an image function in place. You must create a new function.
Zip archives
A zip archive holds your handler file and every library it imports. Lambda's managed runtime includes the AWS SDK (boto3 for Python), but its version is not pinned. Bundle your own copy if you need a specific version.
This sequence builds and uploads a Python function with a third-party dependency:
mkdir -p build
pip install requests --target build/
cp app.py build/
cd build && zip -r ../function.zip . && cd ..
aws lambda update-function-code \
--function-name orders-function \
--zip-file fileb://function.zipThe handler file must sit at the root of the zip, not inside a subfolder. Otherwise Lambda reports Unable to import module.
Packages with compiled code, such as numpy or cryptography, must be built for Lambda's Linux environment and CPU architecture (x86_64 or arm64). Installing them on a Mac and zipping the result is a common cause of import errors. sam build --use-container builds inside a Lambda-like image to avoid this.
Layers
A layer is a zip archive that Lambda extracts into /opt in the execution environment. A function can use up to five layers. Layers let you share libraries or tools across functions and keep your own deployment package small.
For Python, put libraries under a python/ folder inside the layer zip so they land on the import path:
mkdir -p layer/python
pip install requests --target layer/python/
cd layer && zip -r ../layer.zip . && cd ..
aws lambda publish-layer-version \
--layer-name shared-libs \
--zip-file fileb://layer.zip \
--compatible-runtimes python3.12The command returns a version ARN. Each publish creates a new immutable version, and your function refers to one specific version. Updating a layer therefore does not change existing functions until you point them at the new version.
Container images
For container images, you build a Docker image from an AWS-provided base image and push it to Amazon ECR. Lambda pulls from ECR and caches the image.
FROM public.ecr.aws/lambda/python:3.12
COPY requirements.txt ${LAMBDA_TASK_ROOT}
RUN pip install -r requirements.txt
COPY app.py ${LAMBDA_TASK_ROOT}
CMD ["app.handler"]The CMD line plays the role of the handler setting. If you build on a non-AWS base image instead, add the Lambda Runtime Interface Client to the image so it can talk to the Lambda service.
Images work well when dependencies exceed the zip limits, for example machine learning libraries. They also let a team reuse its image scanning and build pipeline. The cost is extra steps: an ECR repository, image pushes, and an execution role that Lambda can use with the repository.
Choosing a package type
Start with a plain zip. Move to layers when several functions repeat the same dependencies. Move to an image when size or tooling requires it.
Common errors
Unable to import module: the handler is nested in a folder, or a compiled library was built for the wrong platform.Code storage limit exceededor a size error: the unzipped package plus layers is over 250 MB.- Layer changes not appearing: the function still points at the old layer version.
- Architecture mismatch on an image: the image was built for x86_64 but the function is set to arm64, or the reverse.
Exercise
A function imports a library with compiled C extensions. You pip install it on your laptop (a different CPU architecture than your Lambda function) and upload the zip. The function fails with an import error. What is the cause and how do you fix it?
Show answer
The compiled binaries target your laptop's platform, not Lambda's Linux environment and architecture. Rebuild the dependencies for Lambda, for example with sam build --use-container, or pip install with --platform and --only-binary=:all: flags matching the function's architecture, then repackage and upload.
Exercise
You publish a new version of a layer. Your function still uses the old code. Why, and what do you do?
Show answer
Layer versions are immutable and a function references one specific version. Update the function configuration to use the new layer version ARN.