A build tool for creating optimized Python AWS Lambda layers
Creates an AWS Lambda Layers structure that is optimized for: Lambda Layer directory structure, compiled library compatibility, and minimal file size.
This repo was created to address these issues:
Note: This script requires Docker and uses a container to mimic the Lambda environment.
.pyc
files and unnecessary librariesThis function can be cloned for standalone use, into a parent repo or added as a submodule.
Clone for standalone use or within a repo:
# If installing into an exisiting repo, navigate to repo dir
git clone --depth 1 https://github.com/tobilg/python-lambda-layer-builder _build_layer
Alternatively, add as a submodule:
cd {repo root}
git submodule add https://github.com/tobilg/python-lambda-layer-builder _build_layer
# Update submodule
git submodule update --init --recursive --remote
$ ./build.sh -h
AWS Lambda Layer Builder for Python Libraries
Usage: build.sh [-p PYTHON_VER] [-n NAME] [-f] [-r] [-h] [-v]
-p PYTHON_VER : Python version to use: 2.7, 3.6, 3.7, 3.8 (default 3.7)
-n NAME : Name of the layer
-f REQ_PATH : Path to requirements file
-r : Raw mode, don't zip layer contents
-d : Don't install Python dependencies
-s : Don't strip .so files
-h : Help
-v : Display build.sh version
./build.sh
_build_layer/build.sh
if installed in sub-dir-f
switch-p PYTHON_VER
- specifies the Python version: 2.7, 3.6, 3.7, 3.8 (default 3.7)You can edit the _clean.sh
file if you want to add custom cleaning logic for the build of the Lambda layer. The above part of the file must stay intact:
#!/usr/bin/env bash
# Change to working directory
cd $1
# ----- DON'T CHANGE THE ABOVE -----
# Cleaning statements
# ----- CHANGE HERE -----
rm test.xt
The _make.sh
script will then execute the commands after the Python packages have been installed.
If installed as submodule and need to remove
# Remove the submodule entry from .git/config
git submodule deinit -f $submodulepath
# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/$submodulepath
# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f $submodulepath
# remove entry in submodules file
git config -f .git/config --remove-section submodule.$submodulepath