How does AWS Lambda handle dependencies?
Instruction: Explain the process of including external libraries or dependencies in an AWS Lambda function.
Updated
Official answer
Package application dependencies for the selected Lambda runtime, operating-system environment and CPU architecture. Native extensions built for macOS or Windows will not automatically work on Lambda's Linux environment. Pin versions, build reproducibly, scan dependencies and test imports using compatible runtime artifacts.
For a ZIP deployment, include the handler and libraries at the runtime's required paths. For example, a Python handler module must be discoverable at the package root, and Python layer dependencies use the documented layer directory layout. Package the SDK dependencies you rely on when practical rather than assuming an arbitrary preinstalled version.
The ZIP limits distinguish transfer size from expanded size: direct API/SDK upload has a 50 MB zipped limit, while the combined unzipped function, layers and custom runtime must fit within 250 MB. Uploading through S3 or moving dependencies to layers does not remove the unzipped limit.
A container-image function is an alternative packaging model with its own requirements and size limit. Include dependencies in the image; Lambda ZIP layers are not attached to image-based functions. An image must implement a compatible Lambda runtime interface rather than merely be any application container.
Retain the exact artifact and dependency lock information, verify handler loading and representative inputs, and redeploy explicitly when updating dependencies. A smaller upload is not proof of lower cold-start latency or a smaller total runtime footprint.
Python packaging and native libraries, package quotas, layers and packaging modes