Skip to main content

Python script to package a Python function for deploying to AWS Lambda

Project description

package-python-function

Python command-line (CLI) tool to package a Python function for deploying to AWS Lambda, and possibly other cloud platforms.

This tool builds a ZIP file from a virtual environment with all dependencies installed that are to be included in the final deployment asset. If the content is larger than AWS Lambda's maximum unzipped package size of 250 MiB, This tool will then employ the ZIP-inside-ZIP (nested-ZIP) workaround. This allows deploying Lambdas with large dependency packages, especially those with native code compiled extensions like Pandas, PyArrow, etc. The ZIP files are generated reproducibly, ensuring that the same source will always generate a ZIP file with the same hash.

This technique was originally pioneered by serverless-python-requirements, which is a NodeJS (JavaScript) plugin for the Serverless Framework. The technique has been improved here to not require any special imports in your entrypoint source file. That is, no changes are needed to your source code to leverage the nested ZIP deployment.

The motivation for this Python tool is to achieve the same results as serverless-python-requirements but with a purely Python tool. This can simplify and speed up developer and CI/CD workflows.

One important thing that this tool does not do is build the target virtual environment and install all of the dependencies. You must first generate that with a tool like Poetry and the poetry-plugin-bundle.

Example command sequence

poetry bundle venv .build/.venv --without dev
package-python-function .build/.venv --output-dir .build/lambda

The output will be a .zip file with the same name as your project from your pyproject.toml file (with dashes replaced with underscores).

Installation

Use pipx to install:

pipx install package-python-function

Usage / Arguments

package-python-function venv_dir [--project PROJECT] [--output-dir OUTPUT_DIR] [--output OUTPUT]
  • venv_dir [Required]: The path to the virtual environment to package.
  • --project [Optional]: Path to the pyproject.toml file. Omit to use the pyproject.toml file in the current working directory.

One of the following must be specified:

  • --output: The full output path of the final zip file.
  • --output-dir: The output directory for the final zip file. The name of the zip file will be based on the project's name in the pyproject.toml file (with dashes replaced with underscores).

Notes on Reproducibility

Timestamps

The ZIP files generated adhere with reproducible builds. This means that file permissions and timestamps are modified inside the ZIP, such that the ZIP will have a deterministic hash. By default, the date is set to 1980-01-01.

Additionally, the tool respects the standardized $SOURCE_DATE_EPOCH environment variable, which will allow you to set that date as needed.

One important caveat is that ZIP files do not support files with timestamps earlier than 1980-01-01 inside them, due to MS-DOS compatibility. Therefore, the tool will throw a SourceDateEpochError is $SOURCE_DATE_EPOCH is below 315532800.

Files with embedded full paths

In testing, we found that several file types can leak information from the machine that generated the virtual environment.

To get around this, the tool removes the following files:

**/__pycache/
**/*.dist-info/direct_url.json
**/*.dist-info/RECORD
**/*.pyc
**/*.pyo

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

package_python_function-0.0.12.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

package_python_function-0.0.12-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file package_python_function-0.0.12.tar.gz.

File metadata

  • Download URL: package_python_function-0.0.12.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for package_python_function-0.0.12.tar.gz
Algorithm Hash digest
SHA256 ee302e7a727e82d00f48b9ad96448dd7c0fda1a045eb3f32204f377a5a617a8e
MD5 37526c186f54452619f21a5e839f03f0
BLAKE2b-256 19e6a708a62e97815aa456702b2b85ecaf90bd29aa2910f9e6c36c46c9b28ee4

See more details on using hashes here.

Provenance

The following attestation bundles were made for package_python_function-0.0.12.tar.gz:

Publisher: release.yml on BrandonLWhite/package-python-function

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file package_python_function-0.0.12-py3-none-any.whl.

File metadata

File hashes

Hashes for package_python_function-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 ea045c68ae0515f6c396d1cefd71d01034b6e16495cc72b88f23068e3a4858b6
MD5 30424ebcc8f24c331d0cbf2f50df312c
BLAKE2b-256 f9cc5e474b751cdc081c7f016c75e8725cbf3a8d31bb55180920ef2ec0a4437f

See more details on using hashes here.

Provenance

The following attestation bundles were made for package_python_function-0.0.12-py3-none-any.whl:

Publisher: release.yml on BrandonLWhite/package-python-function

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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