A Python library for simplifying AWS Lambda deployment
Project description
layerpack
A Python library for simplifying AWS Lambda deployment by automatically managing dependencies and creating deployment packages. Uses UV for reliable dependency resolution.
Features
- Automatically detects and downloads required Python packages
- Creates Lambda-compatible ZIP files with all dependencies
- Supports custom package versions and requirements.txt
- Handles platform-specific dependencies for AWS Lambda
- Optimizes package size through selective inclusion
- Supports layer creation with multiple runtimes
- Uses UV for reliable dependency resolution
Installation
You can install layerpack using pip:
pip install layerpack
Or using Poetry:
poetry add layerpack
Usage
Basic Usage
from layerpack import LambdaPackager
# Initialize packager
packager = LambdaPackager(
runtime="python3.9",
output_dir="./dist"
)
# Create a layer from requirements.txt
packager.create_layer_from_requirements("requirements.txt", "my-layer")
# Or specify packages directly
packager.create_layer_from_packages(
packages=["pandas", "numpy"],
layer_name="data-science-layer"
)
Command Line Interface
The library provides a convenient CLI with verbose logging support:
# Analyze dependencies in requirements.txt
lambda-bundler -v analyze -r requirements.txt
# Create layer from requirements.txt
lambda-bundler create-layer -r requirements.txt -n my-layer
# Create layer from package list
lambda-bundler create-layer -p pandas,numpy -n data-science-layer
Example requirements.txt
# Core dependencies
requests>=2.31.0
urllib3<2.0.0 # Compatible with requests
# Data processing
pandas==2.2.0
numpy==1.26.4
# AWS
boto3==1.34.34
botocore==1.34.34
# Utilities
pyyaml==6.0.1
python-dotenv==1.0.1
Advanced Configuration
packager = LambdaPackager(
runtime="python3.9",
output_dir="./dist",
config={
"exclude_packages": ["pytest", "mock"],
"include_source": ["custom_module/"],
"optimization_level": 2,
"max_size_mb": 250,
"compatible_runtimes": ["python3.8", "python3.9"]
}
)
Configuration Options
exclude_packages: List of packages to exclude from the layerinclude_source: List of local source directories to includeoptimization_level: Python optimization level (0-2)max_size_mb: Maximum size limit for the layercompatible_runtimes: List of compatible Python runtimesstrip_test_files: Remove test files to reduce sizeinclude_dependencies: Include transitive dependencies
Error Handling
The library provides specific exceptions for different error cases:
PackageNotFoundError: Package not found in PyPIIncompatibleRuntimeError: Package incompatible with Lambda runtimeLayerSizeLimitError: Layer exceeds size limitDependencyConflictError: Conflicting package dependencies
Development
Prerequisites
- Python 3.9 or higher
- UV for dependency management
Setting Up Development Environment
- Clone the repository:
git clone https://github.com/richardissailing/layerpack.git
cd layerpack
- Install dependencies:
poetry install
- Run tests:
poetry run pytest
- Run linting:
poetry run ruff check .
poetry run ruff format .
Building and Publishing
- Build the package:
poetry build
- Publish to PyPI:
poetry publish
Best Practices
- Always specify exact package versions in requirements.txt
- Use the
exclude_packagesoption to remove unnecessary dependencies - Set appropriate
compatible_runtimesfor cross-runtime compatibility - Enable
strip_test_filesto reduce layer size - Use
optimization_level=2for production deployments - Use the verbose flag (-v) when troubleshooting dependency resolution
Contributing
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch
- Write your changes
- Run the tests (
poetry run pytest) - Run the linter (
poetry run ruff check .) - Submit a Pull Request
Please make sure your PR includes:
- A clear description of the changes
- Updates to documentation if needed
- Additional tests for new features
- All tests passing and code linted
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file layerpack-0.1.2.tar.gz.
File metadata
- Download URL: layerpack-0.1.2.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.8 Linux/6.8.0-1017-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dca978e410641269009b4884e72d45aac3ae5f1a2a9922efba16c8662e572b1b
|
|
| MD5 |
1f79839ae9015aa9a2129d0a2660790b
|
|
| BLAKE2b-256 |
260ed3e336aa1e781fdf8067dd9afa0007d9e51240e7368d0282e6dc368ded93
|
File details
Details for the file layerpack-0.1.2-py3-none-any.whl.
File metadata
- Download URL: layerpack-0.1.2-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.12.8 Linux/6.8.0-1017-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
531b0d8e50748e79b6fea5e53241230c900e1c655e8ebe995580a1ba2f17218e
|
|
| MD5 |
2660998804f39339db7340ef24c14b23
|
|
| BLAKE2b-256 |
f4be0c312c20c1fab6a39f5377aaf7dd5bdd9b22e5777e841abb9fec3eb02ae3
|