3.1.1 Dependencies & Packaging
Lambda Deployment Packaging
Lambda Deployment Package Types
Lambda supports 2 types of deployment packages:
- Container images - Up to 10GB
- ZIP file archives - Up to 250MB unzipped
Packaging Methods
| Method | Size Limit | Use Case |
|---|
| ZIP (direct upload) | 50MB zipped, 250MB unzipped | Small functions |
| ZIP (via S3) | 50MB zipped, 250MB unzipped | Larger packages |
| Container Image | 10GB | Complex dependencies, Docker workflows |
| Layers | 250MB total (max 5 layers) | Shared libraries across functions |
Lambda Layers
Question thi: Function > 100MB exceeds deployment package size limits. Cần refactor để pull additional code và dependencies từ different source. Dùng gì?
Answer: Lambda Layers
Lambda Layers là distribution mechanism cho:
- Libraries
- Custom runtimes
- Other function dependencies
Lợi ích:
- ✅ Manage function code independently from unchanging code/resources
- ✅ Configure function to use layers you create, AWS provides, hoặc from other customers
- ✅ Include layers to use libraries without including them in deployment package
- ✅ Reduce deployment package size
Layer structure:
python/
└── lib/
└── python3.12/
└── site-packages/
├── requests/
└── pandas/
Properties:
- Share dependencies across multiple functions
- Versioned, immutable
- Max 5 layers per function
- Total unzipped size (function + layers) ≤ 250MB
Layers vs Container Images
Question: Dùng layers với container images?
Answer: NO
Với container images, bạn package preferred runtime, libraries, và dependencies into container image khi build image.
ZIP Package Structure
my-function.zip
├── lambda_function.py # Handler
├── requirements/ # Dependencies
│ ├── boto3/
│ └── requests/
└── utils/ # Custom modules
└── helpers.py
# Package Python Lambda
pip install -r requirements.txt -t ./package
cp lambda_function.py ./package/
cd package && zip -r ../function.zip .
Managing Dependencies
Lambda Execution Environment
- Lambda includes libraries in execution environment
- Có thể use built-in libraries
Update Lambda with Additional Dependencies
Node.js example:
- Nếu function depends on libraries khác AWS SDK for JavaScript
- Use NPM to include them trong deployment package
- Nếu libraries use native code → use Amazon Linux environment to create package
Add newer SDK version:
- Có thể add SDK to deployment package nếu cần newer version
- Hoặc ensure version doesn’t change trong future
Native Libraries
Nếu deployment package contains native libraries:
# Build with SAM
sam build --use-container
--use-container builds package inside Docker image- Docker image compatible với Lambda execution environment
CDK Dependencies
Dependencies cho AWS CDK application/library managed bằng:
- pip (Python)
- npm (Node.js)
- yarn (Node.js)
- Maven (Java)
- NuGet (.NET)
Container Image
FROM public.ecr.aws/lambda/python:3.12
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY app.py .
CMD ["app.handler"]
- Push to ECR → Lambda references ECR image
- Dùng AWS base images hoặc custom (implement Runtime API)
- Package runtime, libraries, dependencies into image
Deployment Package Size Scenarios
Scenario 1: Function > 100MB
- Solution: Use Lambda Layers
- Extract libraries to layers
- Reduce deployment package size
Scenario 2: Function with native libraries
- Solution:
sam build --use-container - Build trong Docker compatible với Lambda environment
Scenario 3: Complex dependencies, Docker workflow
- Solution: Container image (up to 10GB)
- Package everything into image
Exam Tip:
- Lambda supports 2 types: Container images (10GB) và ZIP (250MB unzipped)
- Layers = distribution mechanism cho libraries, reduce package size
- NO layers with container images - package everything into image
- ZIP max 250MB unzipped, 50MB direct upload
- Native libraries →
sam build --use-container - CDK dependencies → pip, npm, yarn, Maven, NuGet