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.
Qinjie Zhang
Amazon Employee
Published Feb 3, 2025
Last Modified Feb 4, 2025
AWS Lambda layers allow you to bundle your code and dependencies into a single file, which can then be reused across multiple functions. However, deployment of lambda function involves several manual steps, which can be time-consuming and error-prone.
This blog post shares a convenient script in GitHub repo qinjie/aws-create-python-lambda-layer to streamline the process. It is capable of creating a layer by handling both custom source folders and requirements files.
Here are the steps involved in creating a lambda layer. They are manual steps which are error-prone and time-consuming. It is not suitable for automation and CI/CD workflows either.
- 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.
The script can be found in GitHub repo
qinjie/aws-create-python-lambda-layer
.It is a Python-based tool designed to generate zip files that can be used as AWS Lambda layers. It offers 2 methods for creating a 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.
The script supports customization through environment variables, allowing you to tailor the behavior to suit your specific needs.
- 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
If LAYER_NAME and/or LAYER_DESCRIPTION are provided, the script will attempt to deploy the layer using AWS CLI.
Ensure that you have the following installed on your system:
- 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.
Install both
virtualenv
and optional awscli
, and setup AWS CLI credentials by following this guide. For Ubuntu,
For Mac,
For Mac, use
brew
to install other Python interpreter locally. For example, following commands install both Python3.12 and Python3.13 on Mac.Clone the GitHub repository or downloading the script directly. The repository contains the script along with example layers in the useful-layers directory.
You can customize the behavior of our script using the following environment variables:
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")
If you have custom files that you want to include in your Lambda layer, place them in a folder and run the script with the folder name as an argument. The script will create a zip file named
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.
If you're working with Python dependencies, provide a
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).
Upload the generated zip file through the AWS Management Console.
This blog post demonstrates how developers can utilize a script to streamline their AWS Lambda layer workflow. Whether working with Python libraries or custom files, the tool helps save time and maintain consistency in your projects.
Reference:
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.