Share common code and dependencies across Lambda functions to reduce deployment size.
Code Snippet
# serverless.yml
service: my-service
provider:
name: aws
runtime: python3.11
layers:
commonDeps:
path: layers/common
description: Shared dependencies
compatibleRuntimes:
- python3.11
functions:
userHandler:
handler: handlers/user.handler
layers:
- !Ref CommonDepsLambdaLayer
orderHandler:
handler: handlers/order.handler
layers:
- !Ref CommonDepsLambdaLayer
# layers/common/python/requirements.txt
# boto3, requests, etc.
Why This Helps
- Reduces deployment package size by 80%+
- Faster cold starts
- Centralized dependency management
How to Test
- Deploy and verify functions work
- Check deployment package sizes
When to Use
Multiple Lambda functions sharing common libraries or utilities.
Performance/Security Notes
Layer updates require redeploying functions. Keep layers under 50MB unzipped.
References
Try this tip in your next project and share your results in the comments!
Discover more from Byte Architect
Subscribe to get the latest posts sent to your email.