Skip to main content

A Python library for simplifying AWS Lambda deployment

Project description

layerpack

CI PyPI version Python Version License: MIT

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 layer
  • include_source: List of local source directories to include
  • optimization_level: Python optimization level (0-2)
  • max_size_mb: Maximum size limit for the layer
  • compatible_runtimes: List of compatible Python runtimes
  • strip_test_files: Remove test files to reduce size
  • include_dependencies: Include transitive dependencies

Error Handling

The library provides specific exceptions for different error cases:

  • PackageNotFoundError: Package not found in PyPI
  • IncompatibleRuntimeError: Package incompatible with Lambda runtime
  • LayerSizeLimitError: Layer exceeds size limit
  • DependencyConflictError: Conflicting package dependencies

Development

Prerequisites

  • Python 3.9 or higher
  • UV for dependency management

Setting Up Development Environment

  1. Clone the repository:
git clone https://github.com/richardissailing/layerpack.git
cd layerpack
  1. Install dependencies:
poetry install
  1. Run tests:
poetry run pytest
  1. Run linting:
poetry run ruff check .
poetry run ruff format .

Building and Publishing

  1. Build the package:
poetry build
  1. Publish to PyPI:
poetry publish

Best Practices

  1. Always specify exact package versions in requirements.txt
  2. Use the exclude_packages option to remove unnecessary dependencies
  3. Set appropriate compatible_runtimes for cross-runtime compatibility
  4. Enable strip_test_files to reduce layer size
  5. Use optimization_level=2 for production deployments
  6. Use the verbose flag (-v) when troubleshooting dependency resolution

Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch
  3. Write your changes
  4. Run the tests (poetry run pytest)
  5. Run the linter (poetry run ruff check .)
  6. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

layerpack-0.1.2.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

layerpack-0.1.2-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

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

Hashes for layerpack-0.1.2.tar.gz
Algorithm Hash digest
SHA256 dca978e410641269009b4884e72d45aac3ae5f1a2a9922efba16c8662e572b1b
MD5 1f79839ae9015aa9a2129d0a2660790b
BLAKE2b-256 260ed3e336aa1e781fdf8067dd9afa0007d9e51240e7368d0282e6dc368ded93

See more details on using hashes here.

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

Hashes for layerpack-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 531b0d8e50748e79b6fea5e53241230c900e1c655e8ebe995580a1ba2f17218e
MD5 2660998804f39339db7340ef24c14b23
BLAKE2b-256 f4be0c312c20c1fab6a39f5377aaf7dd5bdd9b22e5777e841abb9fec3eb02ae3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page