Logo AppDev24 Login / Sign Up
Sign Up
Have Login?
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Login
New Account?
Recovery
Go to Login
By continuing you indicate that you agree to Terms of Service and Privacy Policy of the site.
Cloud

How to create AWS Lambda Layer

Updated on Aug 21, 2024

As the demand for serverless computing continues to grow, AWS Lambda has become a popular choice for developers looking to build scalable and efficient applications. One of the key features of AWS Lambda is its support for layers, which allow you to package and reuse code across multiple functions. In this article, we'll walk through the process of creating an AWS Lambda layer using Python on MacOS.

Install Python 3.11

To get started, we need to install Python 3.11 using Homebrew. Open your terminal and run the following command:

brew install python@3.11

Once installed, verify that you're running the correct version of Python by typing:

python3 -V

Install pip & Virtual Environment

Next, we need to install pip, the package installer for Python, and create a virtual environment using venv. Run the following commands:

python3.11 -m pip install --upgrade pip
python3.11 -m pip install virtualenv

Set up Lambda Layer Directory

Create a new directory to hold your lambda layer files, and navigate into it. Create a new virtual environment using venv.

mkdir lambda_layers
cd lambda_layers

python3.11 -m venv env
source env/bin/activate
pip list
pip install --upgrade pip

Install Dependencies & Create Lambda Layer

Install your dependencies, as per your need, and create the lambda layer. An example as below:

mkdir python
cd python

pip install snowflake-connector-python \
    "urllib3<2" \
    certifi==2023.11.17 \
    packaging \
    --no-cache-dir \
    --python-version 3.11 \
    --platform manylinux2014_x86_64 \
    --target=package \
    --implementation cp \
    --no-user \
    --only-binary=:all: \
    --upgrade \
    -t .

Remove Cache and DistInfo

To reduce the size of your lambda layer, remove any cache and distinfo files:

repo=$(pwd)
root=`basename ${repo}`
dir=$(pwd)

for d in $(find ${repo} -maxdepth 10 -type d)
do
    dname=`basename ${d}`
    pdname=`basename $(dirname ${d})`
    if [[ ( "${dname}" == "__pycache__" ) || ( "${dname}" == *".dist-info" ) || ( "${dname}" == *"tests"* ) ]]
    then
        echo "${d}"
        rm -rf "${d}"
    else
        continue
    fi
done

cd ${dir}

Create the Zip File

Finally, create a zip file containing your lambda layer:

cd ..
zip -r custom-lambda-layer.zip python


deactivate

Once you've created your lambda layer, deactivate your virtual environment

And that's it! You now have a Python-based AWS Lambda layer ready for use. With this guide, you should be able to create your own custom layers and reuse code across multiple functions in AWS Lambda.

PrimeChess

PrimeChess.org

PrimeChess.org makes elite chess training accessible and affordable for everyone. For the past 6 years, we have offered free chess camps for kids in Singapore and India, and during that time, we also observed many average-rated coaches charging far too much for their services.

To change that, we assembled a team of top-rated coaches including International Masters (IM) or coaches with multiple IM or GM norms, to provide online classes starting from $50 per month (8 classes each month + 4 tournaments)

This affordability is only possible if we get more students. This is why it will be very helpful if you could please pass-on this message to others.

Exclucively For Indian Residents: 
Basic - ₹1500
Intermediate- ₹2000
Advanced - ₹2500

Top 10 Articles