Skip to content

Pure python utility function for packaging AWS Lambda functions and layers

Notifications You must be signed in to change notification settings

nuage-studio/lambda-package

Repository files navigation

Nuage Lambda Packaging

This repository provides both a library function and command-line tool to package up Lambda functions. Given a directory, it will package all the files in the directory into a zip file, but ignoring any files covered exclude patterns. If exclude patterns are not given in the configuation, then these patterns will be read from the .gitignore file.

Command line useage

To use on the command line, you should install the package using pip, and then use the following command:

python -m lambda_package path [--output OUTPUT]

where path is the path to the source directory which you wish to package, and the --output (or -o) option specifies the zip file output. If no -o option is given, the tool will display a preview of the file tree which it would package.

Library usage

The package also exposes a function named package, which performs the same function as the command line. For example:

from lambda_package import package

package(root_path="src", Configuration(output="app.zip"))

Configuration

Further configuration can be specified in either the .lambda-packagerc or setup.cfg files, with the former taking priority. The package function will look for these files by default if no Configuration object is passed in.

An example .lambda-packagerc might be:

[lambda-package]
output = "app.zip"
exclude = ["node_modules", "*.bak"]

Note that the exclude option overrides any patterns in the .gitignore file.

Name Default Description
output None The path of the zip file to be generated
exclude None A list of exclude pattern strings. If not given, .gitignore is used instead.
requirements None The path to the requirements.txt file if Python dependencies are to be packaged.
layer_output None Path to a folder where requirement outputs should be stored rather than the package.
use_docker true Whether or not the Lambda layer dependencies should be built using a Docker image.
python_version Runtime version The Python version used to build the pip requirements. Must be in the format [major].[minor], patch version will be ignored.

Pip Dependencies

The tool also has the option to automatically bundle any pip dependencies into the package by setting the requirements parameter to be the path to a requirements.txt file. If use_docker is True, these requirements will be built using a Docker image which mimics the Lambda environment. The layer_output option can also be set in order to package the dependencies into a separate zip file, for the creation of a Lambda layer.

Development

Getting started

You need Python 3 (preferably 3.8) installed to start working on this project.

In order to install your virtualenv, just go to the root of the project and:

make install

IDE

Nuage recommends Visual Studio Code to work on this project, and some default settings have been configured in the .vscode/settings.json.

These settings merely enforce the code-quality guidelines defined below, but if you use another IDE it's probably worth taking a quick look at it to ensure compliance with the standard.

By default, we recommend:

  1. Putting your virtualenv in a venv folder at the project root
  2. Using a .env file to define your environment variables (cf. python-dotenv)

Code quality

This project has opinionated code-quality requirements:

All of these tools are enforced at the commit-level via pre-commit

You can run the following command to apply code-quality to your project at any point:

make quality

Code quality configuration files:

Unit tests

  • To run the unit tests (you may also want to instantiate a virtual environment in the root directory):
python setup.py test

The test file names are in the format {source file name}_tests.py, or {source file name}_{function name}_tests.py for functions which have a large number of tests.

About

Pure python utility function for packaging AWS Lambda functions and layers

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages