Deploy any AWS Lambda Layer within a Minute
Creating AWS Lambda layers can be a tedious, manual process prone to errors. Do it within a minute with this bash script.
- Identify the libraries and the files to be included in the layer. Create a
requirements.txt
file which contains all libraries. - Create a folder structure which matches the runtime requirements, e.g. `
python/lib/python3.12/site-packages/`
. - Install the libraries to the appropriate folder.
- Compress the root folder
python
as a zip file. - Create a lambda layer using the zip file.
qinjie/aws-create-python-lambda-layer
.- Using a Source Folder: If you have custom code or files to be included in the layer, you can place them together with a
requirements.txt
file in a single folder. The script will include this folder in the zip file. - Using a Requirement File: You specify the dependencies in a
requirements.txt
file. When run, the script will install these dependencies into a resulting zip file.
- PYTHON_BASE_VERSION: base version of Python
- LAYER_NAME: name of the layer to be deployed
- LAYER_DESCRIPTION: description of the layer to be deployed
- Python: The bash script uses Python, specifically
virtualenv
andpip
modules, to create a virtual environment and install python libraries. - Other Python Interpreters: To create a Lambda of a particular Python version, we need to have that Python interpreter installed locally too. This is because `virtualenv` use it to replicate a Python virtual environment. For example, to create a layer of python3.13, we need to have python3.13 installed locally.
- AWS CLI: To publish layers directly to AWS, you'll need the AWS Command Line Interface installed and configured.
virtualenv
and optional awscli
, and setup AWS CLI credentials by following this guide.1
sudo apt install python3-virtualenv awscli
1
2
pip3 install virtualenv
brew install awscli
brew
to install other Python interpreter locally. For example, following commands install both Python3.12 and Python3.13 on Mac.1
2
brew install python@3.12
brew install python@3.13
1
2
git clone https://github.com/qinjie/aws-create-python-lambda-layer
cd aws-create-python-lambda-layer
PYTHON_VERSION
: Specifies the version of Python to use. Defaults to3.13
.LAYER_NAME
: Sets a custom name for your Lambda layer. If it is not provided, the script will not run AWS CLI to deploy the layer.LAYER_DESCRIPTION:
Adds a description for your layer (default is "A sample Lambda layer")
1
2
3
export PYTHON_VERSION=3.11
export LAYER_NAME="my-sample-lambda-layer"
export LAYER_DESCRIPTION="Sample description for lambda layer"
myfolder.zip
in the same directory.- Create a folder (e.g.,
myfolder
) containing all the files you wish to include. - Navigate to the directory where our script is located.
- Run the script with the name of your folder as an argument.
1
./create_lambda_layer.sh myfolder
requirements.txt
file listing the libraries. The script will install these dependencies and include them in requirements.zip
.- Create a
requirements.txt
file listing all the dependencies your layer needs. - Navigate to the directory where our script is located.
- Run the script without any arguments (since it will read from
requirements.txt
by default).
1
./create_lambda_layer.sh
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.