Skip to main content

A tool to package Python AWS Lambda functions with zips, Docker containers, and layers.

Project description

lambda-packer

A streamlined tool for managing and packaging Python AWS Lambda functions

Overview

lambda-packer is a command-line tool designed to simplify the process of packaging Python AWS Lambda functions. It provides an opinionated approach to develop Lambdas using a monorepo, allowing packaging as either zip files or Docker containers, with shared dependencies packaged as Lambda layers.

Key Features

  • Package Lambdas as zip files or Docker containers
  • Support for multiple Lambda layers shared across functions
  • Simple YAML configuration to manage Lambdas and layers
  • Layer packaging with automatic dependency handling

Installation

To install the tool, clone this repository and run:

git clone https://github.com/calvernaz/lambda-packer.git
cd lambda-packer
pip install -e .

For development:

pip install -e .[dev]

Usage

1. Initialize a New Monorepo

The init command creates a basic monorepo structure for your Lambda functions, including a common folder for shared dependencies, an example Lambda function, and a package_config.yaml file.

lambda-packer init <parent_directory> --lambda-name <lambda_name>

Example:

lambda-packer init my_project --lambda-name my_lambda

This command creates:

my_project/
├── common/
├── my_lambda/
│   ├── lambda_handler.py
│   └── requirements.txt
├── dist/
└── package_config.yaml

2. Configuration

The package_config.yaml file is where you define how to package your Lambdas. You can specify the type of packaging (zip or docker), the Python runtime, and any layers associated with the Lambda.

Example package_config.yaml

lambdas:
my_lambda:
type: zip
runtime: "3.12"  # Specify the Python runtime version
layers:
- common

3. Package Lambda as a Zip

To package a Lambda function (for a zip type Lambda), use the following command:

lambda-packer package my_lambda

This will package the Lambda function and any referenced layers (e.g., common) into a zip file in the dist directory.

4. Package Lambda as a Docker Container

To package a Lambda as a Docker container (for a docker type Lambda), modify the package_config.yaml and set type: docker.

lambdas:
my_lambda:
type: docker
runtime: "3.9"
layers:
- common

Then run:

lambda-packer package my_lambda

The tool will build a Docker image using the specified Python runtime and package the Lambda function.

5. Packaging Lambda Layers

If you need to package shared dependencies (like the common folder) as Lambda layers, you can use the package-layer command:

lambda-packer package-layer common

This command packages the common directory as a Lambda layer and zips it to the dist/ folder.


Available Commands

  • init <parent_directory> --lambda-name <lambda_name>: Initialize a new monorepo with a common folder, a lambda, and package_config.yaml.
  • package <lambda_name>: Package the specified Lambda function (either as zip or Docker container).
  • package-layer <layer_name>: Package a specific layer (e.g., common) into a zip file.
  • clean: Clean the dist/ directory by removing all contents.

Example Workflow

  1. Initialize the project:
lambda-packer init my_project --lambda-name my_lambda
  1. Edit package_config.yaml to configure the Lambda:
lambdas:
my_lambda:
type: zip
runtime: "3.9"
layers:
- common
  1. Install dependencies for my_lambda by editing my_lambda/requirements.txt.

  2. Package the Lambda:

lambda-packer package my_lambda
  1. Package the common layer (if needed):
lambda-packer package-layer common

6. Adding a New Lambda to an Existing Monorepo

You can add a new Lambda to an existing monorepo using the lambda command. You can also specify layers to be added to the new Lambda.

lambda-packer lambda <lambda_name> --runtime <runtime_version> --type <zip|docker> --layers <layer1> --layers <layer2>

Example:

lambda-packer lambda my_new_lambda --runtime 3.9 --type docker --layers common --layers shared

This will create a new Lambda directory and update the package_config.yaml like so:

lambdas:
my_new_lambda:
runtime: "3.9"
type: docker
layers:
- common
- shared

If no layers are specified, the layers key will not be added.

Example without layers:

lambda-packer lambda my_new_lambda --runtime 3.9 --type docker

This will update the package_config.yaml like this:

lambdas:
my_new_lambda:
runtime: "3.9"
type: docker

Contributing

Contributions are welcome! If you'd like to contribute to this project, please open a pull request or issue on GitHub.

Development Setup

pip install -e .[dev]

Running Tests

pytest tests/

License

This project is licensed under the MIT License.


Contact

For any questions or feedback, feel free to open an issue on GitHub.

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

lambda_packer-0.1.7.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

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

lambda_packer-0.1.7-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file lambda_packer-0.1.7.tar.gz.

File metadata

  • Download URL: lambda_packer-0.1.7.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for lambda_packer-0.1.7.tar.gz
Algorithm Hash digest
SHA256 66ca291ee41cbae4fd6047843520f988c763686ef06a4e0b0e38b10db8738460
MD5 d584c3ac867b5657c090e0ad3bfb5fdf
BLAKE2b-256 9ea0675f242b91d2d25e1cac9b49341177f3d7497506b8bd0f21669e728dfacb

See more details on using hashes here.

File details

Details for the file lambda_packer-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: lambda_packer-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for lambda_packer-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 ae27ce2d5a6dedaacade88076baf075e0ac660a4f71d17739ed900463cee6a72
MD5 c537da6b263e8962165f6b89e6628b2f
BLAKE2b-256 a5069a7a073b8aba73859aad1afcd6a4eeed8faccef775d4ece4f5eb3960d475

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