Chapter 93: AWS Lambda and Serverless for DevOps
93.9 Dependencies, ZIP packages and layers
Story (fictional teaching scenario). At a fictional Patna shop, Nina from Berlin installs a library on her laptop but forgets to package it. The cloud runtime cannot import it.
Why
Your laptop’s packages do not automatically travel with the function.
What
- Dependency is a library your code imports.
- ZIP package contains the handler and dependencies; the handler file must be at its expected root path.
- Layer is a separately versioned ZIP of reusable libraries/content mounted into the environment. A Python layer uses a
python/folder. Layers do not apply to a function packaged as a container image. - Native dependency contains compiled code; it must match the Linux runtime and CPU architecture.
- Container image package is another packaging option, not an always-running ECS service.
How
-
Start with standard-library-only code in this local lab.
-
Pin and test dependencies for the target runtime.
-
Keep ZIP/layer paths correct.
-
Rebuild image-based functions when dependencies or base images need updates.
Example 1: Make a local ZIP without any AWS call
- Read the input and the code.
python3 -m zipfile -c price.zip price.py
python3 -m zipfile -l price.zip
- Read the result.
Output (illustrative; not executed on AWS)
File Name ...
price.py ...
- What each part means: The first command creates an archive of just the handler. The second lists its entries. The root contains price.py, not an extra parent folder. No upload occurs. Exact dates and sizes vary.
Example 2: Layer layout
- Read the input and the code.
layer.zip
python/
shared_rules.py
- Read the result.
Output (illustrative; not executed on AWS)
Import path in the function: import shared_rules
- What each part means: The Python folder is the required package layout. A layer version is immutable; changing a shared library means publishing a new layer version and deliberately updating consumers. This is a layout illustration, not a layer creation.
Ravindra Bagale's Tip
Small packages make startup and security review easier.
छोटं package असेल तर startup आणि security review सोपा होतो.
छोटे package का startup और security review आसान होता है.
Did you understand? Let us try it once more.