Automate AWS Lambda Layer Creation with Python and Shell Scripts
In AWS Lambda, a layer is used to provide additional code and dependencies required by lambda functions to run successfully. A layer is actually a .zip archive file that helps abstracts away the dependent libraries needed to run a function from the business logic implemented in the function. The idea of a layer in AWS Lambda promotes code sharing and a best practice of separating business logic code design from dependencies required to execute the logic.
For various Python runtimes and versions, AWS Lambda has provided a bunch of libraries to support lambda functions execution. These libraries include all the Python builtin modules as well as other third-party libraries. A list of predefined Python modules available in AWS Lambda can be found here.
For the needed libraries that are not available in the predefined module list, users are expected to package and upload such libraries for the use of their lambda functions. The process of creating and packaging a lambda layer is pretty manual and can be tedious where more than one modules and of different versions are required to be packaged as a layer for a given lambda function. This post aims to automate the process by designing a python and a shell scripts that can be easily run with desired modules as arguments in order to create a .zip archive layer to be used with lambda functions.
Although layers make life easier when deploying lambda functions, there are important quotas that users need to be aware of. AWS currently allows a total of 5 layers per lambda function and the maximum size for an uploadable .zip archive deployment package (including layers and custom runtimes) is 50MB. The unzipped deployment package maximum size must not go above 250MB. It's therefore a good practice to ensure that too many libraries are not added to the layer to avoid ballooning the size of the package above the limit. It is also advisable to first go through the predefined module list to ensure that only modules that are not already included in AWS Lambda are being packaged as layers for lambda functions.
Solution Design Steps
The solution steps highlighted below assume that you have Anaconda or its smaller footprint version, Miniconda, installed.
Create a new conda environment with a required python version
Activate the new conda environment
Create a project directory and navigate to the folder
Create a package path for libraries to be included in the layer
Create a shell array and add the required libraries to it
Install each library in the array to the defined path
Consider removing unnecessary files and folders such as pycache, LICENSES, etc. to reduce the layer size.
Change to build directory and zip the installed libraries
Deactivate and remove the conda environment
Move the zip layer package into the project directory
If the zipped layer file is less than 10MB, you can upload it directly on the Lambda console when you're creating the layer. For a larger file size, you can upload it via Amazon S3.
Final Code Design
Putting the above shell commands together, you can run the code as either a standalone script in a shell or as a command within the subprocess python module as described below.
Example Script Usage
The example below creates a layer consisting of polars and s3fs modules. When the script execution is completed, a .zip archive file polars-s3fs.zip will be created under the project folder named projectLambdaLayer in your current working directory.
Additionally, you can run the command below to see help tips on how to use the script and the required arguments.
See the full code including the shell script in my Github repository. Thanks for reading.