|
| 1 | +# python-lambda-layer-builder |
| 2 | + |
| 3 | +Creates an AWS Lambda Layers structure that is **optimized** for: [Lambda Layer directory structure](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path), compiled library compatibility, and minimal file size. |
| 4 | + |
| 5 | +This repo was created to address these issues: |
| 6 | + |
| 7 | +- Many methods of creating Lambda zip files for Python functions don't work for Lambda Layers |
| 8 | +- This is due to the fact Lambda Layers require specific library paths within the zip, where regular Lambda zips don't |
| 9 | +- Compiled dependencies must be created in an environment that matches the Lambda runtime |
| 10 | +- Reduce size of the layer by removing unnecessary libraries and files |
| 11 | + |
| 12 | +**Note: This script requires Docker and uses a container to mimic the Lambda environment.** |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +- Builds either a zip file or a raw directory strucutre (e.g. if you want to use frameworks like Serverless for packaging) containing Python dependencies and places the libraries into the proper directory structure for lambda layers |
| 17 | +- Ensures compiled libraries are compatible with Lambda environment by using the [lambci/lambda](https://hub.docker.com/r/lambci/lambda) Docker container that mimics the lambda runtime environment |
| 18 | +- Optimized the zip size by removing `.pyc` files and unnecessary libraries |
| 19 | +- allows specifying lambda supported python versions: 2.7, 3.6 and 3.7 |
| 20 | +- Automatically searches for requirements.txt file in several locations: |
| 21 | +- same directory as script |
| 22 | +- parent directory or script (useful when used as submodule) |
| 23 | +- function sub-directory of the parent directory |
| 24 | + |
| 25 | +## Installation |
| 26 | + |
| 27 | +This function can be **cloned** for standalone use, into a parent repo or added as a **submodule**. |
| 28 | + |
| 29 | +Clone for standalone use or within a repo: |
| 30 | + |
| 31 | +``` bash |
| 32 | +# If installing into an exisiting repo, navigate to repo dir |
| 33 | +git clone --depth 1 https://.com/tobilg/python-lambda-layer-builder _build_layer |
| 34 | +``` |
| 35 | + |
| 36 | +Alternatively, add as a submodule: |
| 37 | + |
| 38 | +``` bash |
| 39 | +cd {repo root} |
| 40 | +git submodule add https://.com/tobilg/python-lambda-layer-builder _build_layer |
| 41 | +# Update submodule |
| 42 | +git submodule update --init --recursive --remote |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +```text |
| 48 | +$ ./build.sh -h |
| 49 | +AWS Lambda Layer Builder for Python Libraries |
| 50 | +
|
| 51 | +Usage: build.sh [-p PYTHON_VER] [-n NAME] [-r] [-h] [-v] |
| 52 | +-p PYTHON_VER : Python version to use: 2.7, 3.6, 3.7 (default 3.7) |
| 53 | +-n NAME : Name of the layer |
| 54 | +-r : Raw mode, don't zip layer contents |
| 55 | +-h : Help |
| 56 | +-v : Display build.sh version |
| 57 | +``` |
| 58 | + |
| 59 | +- Run the builder with the command `./build.sh` |
| 60 | +- or `_build_layer/build.sh` if installed in sub-dir |
| 61 | +- It uses the first requirements.txt file found in these locations (in order): |
| 62 | +- Same directory as script |
| 63 | +- Parent directory of script (useful when used as submodule) |
| 64 | +- Function sub-directory of the parent directory (useful when used as submodule) |
| 65 | +- Optionally specify Python Version |
| 66 | +- `-p PYTHON_VER` - specifies the Python version: 2.7, 3.6, 3.7 (default 3.6) |
| 67 | + |
| 68 | +### Custom cleaning logic |
| 69 | + |
| 70 | +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: |
| 71 | + |
| 72 | +```bash |
| 73 | +#!/usr/bin/env bash |
| 74 | +# Change to working directory |
| 75 | +cd $1 |
| 76 | +# ----- DON'T CHANGE THE ABOVE ----- |
| 77 | + |
| 78 | +# Cleaning statements |
| 79 | +# ----- CHANGE HERE ----- |
| 80 | +rm test.xt |
| 81 | +``` |
| 82 | + |
| 83 | +The `_make.sh` script will then execute the commands after the Python packages have been installed. |
| 84 | + |
| 85 | +## Deinstallation |
| 86 | + |
| 87 | +If installed as submodule and need to remove |
| 88 | + |
| 89 | +```bash |
| 90 | +# Remove the submodule entry from .git/config |
| 91 | +git submodule deinit -f $submodulepath |
| 92 | +# Remove the submodule directory from the superproject's .git/modules directory |
| 93 | +rm -rf .git/modules/$submodulepath |
| 94 | +# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule |
| 95 | +git rm -f $submodulepath |
| 96 | +# remove entry in submodules file |
| 97 | +git config -f .git/config --remove-section submodule.$submodulepath |
| 98 | +``` |
0 commit comments