AWS Lambda: Layers and Dependencies
You'll learn how to package and share reusable code libraries across Lambda functions, manage external dependencies, and keep your deployment packages lean.
The Core Problem
Every Lambda function needs code to run—your handler logic plus any external libraries (like requests in Python or axios in Node.js). Without organization, you'd bundle these dependencies into every single function, creating bloated deployment packages and duplicating identical libraries across dozens of functions.
What Are Lambda Layers?
Think of Lambda Layers as shared bookshelves. Instead of every function carrying its own copy of the same textbook (dependency), you place common books on a shelf that all functions can access. A layer is a ZIP archive containing libraries, custom runtimes, or other dependencies that your function code can reference.
Why Use Layers?
- Reduce deployment package size: Your function code stays small; dependencies live separately
- Share code across functions: One layer can serve multiple functions
- Faster deployments: Update function logic without re-uploading heavy libraries
- Separation of concerns: Business logic separate from dependencies
How Dependencies Work
When Lambda executes your function, it unpacks any attached layers into the /opt directory. Your function runtime automatically includes /opt/python/lib/python3.x/site-packages (for Python) or /opt/nodejs/node_modules (for Node.js) in its search path.
Packaging Best Practices
- Keep it focused: One layer per concern (e.g., one for data processing libs, another for authentication)
- Watch the size limits: Total unzipped size (function + all layers) must stay under 250 MB
- Version your layers: Treat layers like immutable releases—create new versions rather than overwriting
Key Takeaway: Lambda Layers let you package dependencies separately from your function code, enabling reusability, faster deployments, and smaller function packages—critical for efficient serverless architecture.