The lambda layer allows you to add additional code such as libraries, dependencies for the deployed runtime within lambda functions. While working with python runtime we have used temporary on-demand EC2 instances to create & configure the lambda layer which was a tedious task. You can find the relevant blog on the same here.
However, to minimize the efforts & overhead of spinning temporary instance and setup, we started using this shell script, which helped to expedite the process of building, creating & publishing the lambda layer and all will happen just with a single command. To use this shell script you need to install & configure AWS CLI & Docker.
Prerequisites
- AWS CLI (Install & configure)
- Docker (Install)
Installation
Post configuration of AWS CLI & Docker, one-time installation of the shell script is required. Run below command for installation.
SCRIPT_INSTALL_LOC=/usr/local/bin/create-layer; curl https://raw.githubusercontent.com/srcecde/aws-lambda-layer-creator/main/create-layer.sh > $SCRIPT_INSTALL_LOC; chmod +x $SCRIPT_INSTALL_LOC
The above command will allow you to execute the script from any location and please note that this is a one-time configuration.
Lambda layer creation | Usage
To create the layer using the above script the command should be in the specified format.
create-layer layer-name python/nodejs-runtime package1 [package2,…]
- create-layer - Command to invoke the script to create lambda layer
- layer-name - Name of the lambda layer (Please make sure to follow the lambda layer naming convention)
- python/nodejs-runtime - Mention the respective python/nodejs runtime
- package - Name of the package, library that you want to install
Python runtime lambda layer example
For example, I want to create the lambda layer package for python runtime for pandas & NumPy
create-layer pandas-numpy-layer python3.9 numpy pandas
The above command will create the lambda layer package for pandas & NumPy and it will upload & create the lambda layer within your AWS account.
NodeJS runtime lambda layer example
For example, I want to create the lambda layer package for nodejs runtime for request & moment
create-layer request-moment-layer nodejs14.x request moment
The above command will create the lambda layer package for request & moment libraries and it will upload & create the lambda layer within your AWS account.
The time taken for configuration & creation of lambda layers is reduced drastically with this. You can go through the video as well.
I hope this will help to reduce the development time!