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.1.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.1-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file layerpack-0.1.1.tar.gz.

File metadata

  • Download URL: layerpack-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 8863f895112e9863abe15bcd95be37217d0bb8d6a6ff4b2d8ad8c6c6ba0e0c22
MD5 50b5a0be647a2d4c7a39fa3355c3f7b2
BLAKE2b-256 2a5ecdf90777cbce27d83b9795e892de8ecbc508a418479de89dddc150d4fcd6

See more details on using hashes here.

File details

Details for the file layerpack-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: layerpack-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.3 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1b10513cc19a78f99ba4e83f24cef6d259440c99abd260ec7f08e44d1091ca99
MD5 232fccbedd7e85e8a046b7bbf48339d1
BLAKE2b-256 86b9e7ab0babceadc619be1b3e423ffae7fb18a42270d1f1dae780ebe8cb89cd

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