🌐
Amazon Web Services
docs.aws.amazon.com β€Ί aws lambda β€Ί developer guide β€Ί building lambda functions with python β€Ί working with layers for python lambda functions
Working with layers for Python Lambda functions - AWS Lambda
Create the layer in Lambda. Add the layer to your functions. ... To create a layer, bundle your packages into a .zip file archive that meets the following requirements: Build the layer using the same Python version that you plan to use for the Lambda function.
🌐
Medium
medium.com β€Ί brlink β€Ί how-to-create-a-python-layer-in-aws-lambda-287235215b79
How to Create a Python Layer in Aws Lambda | by Rafael Campana | BRLink | Medium
December 2, 2021 - I will create a sample using pandas, once this library has other dependencies and could be a good sample. ... Navigate to Lambda > Layers and create a layer.
Discussions

Best way to build a python lambda layer?
I would do it in the AWS lambdaci/lambda docker image. While very similar to the Amazon Linux distros, the Lambda runtimes are somewhat different, and the docker images are the Lambda runtimes. I ran into trouble once building a crypto library as you suggest...wouldn't work when I loaded it into the Lambda layer, but once I did the docker image, no issue at all. https://hub.docker.com/r/lambci/lambda/ More on reddit.com
🌐 r/aws
3
1
January 4, 2021
python - How to create a layer in lambda function - Stack Overflow
Steps followed to create Create a virtual environment and activate it pip install elasticsearch Zip the folder inside site-packages.zip 4.Create Layer in AWS (say name is elastic) Add the code b... More on stackoverflow.com
🌐 stackoverflow.com
amazon web services - How to use AWS Lambda layer using Python? - Stack Overflow
I'm not saying it's relevant in ... huge Python packages like Pandas, then I would avoid trying to use them in Lambda. ... I've seen that a few libraries like numpy and pandas don't work in Lambda when installed using pip. I have had success using the .whl package files for these libraries to create the Lambda layer... More on stackoverflow.com
🌐 stackoverflow.com
Help with Python Lambda Layers needed: Imports within a module fail unless also imported in the Lambda code.
OKay so the solution was the final paragraph of section 6.4.1 of the Modules tutorial: https://docs.python.org/3/tutorial/modules.html#packages Remember, there is nothing wrong with using from package import specific_submodule! In fact, this is the recommended notation unless the importing module needs to use submodules with the same name from different packages. Replacing from mymodule import subfolder with from mymodule.subfolder import ( mysubmodule ... ) solved my problem. Still not sure why the other way passed unit testing and local testing, but it is what it is. Now I'm using best practices so it's an overall improvement regardless. More on reddit.com
🌐 r/aws
3
3
March 3, 2023
🌐
DEV Community
dev.to β€Ί fadygrab β€Ί how-to-make-an-aws-lambda-custom-layer-for-python-3h6g
How to make an AWS Lambda custom layer for python - DEV Community
March 25, 2023 - ... This will produce the lambda-layer.zip file that I'll upload. 6- Now for the AWS stuff, in order to upload your custom layer, we will do the following: 1- Navigate to Layers form the left navigation bar then press create layer
🌐
Capital One
capitalone.com β€Ί tech β€Ί cloud β€Ί creating-lambda-layers
Creating Lambda Layers for Python Functions | Capital One
June 1, 2023 - Uploading a layer using the console is easy. Just go to the Lambda service, choose Layers, click on Create Layer, then follow the prompts. I prefer using the CLI. You can use the following command, replacing my-layername with your own layer name.
🌐
Medium
aws.plainenglish.io β€Ί easiest-way-to-create-lambda-layers-with-the-required-python-version-d205f59d51f6
Easiest Way to Create Lambda Layers with the Required Python Version | by Amit Duwal | AWS in Plain English
December 23, 2024 - Its important you create a directory named python. This is mandatory when working with lambda layers. Now that you are in python directory, install the required libraries. ... We will need to zip the python folder where the library is but before we do that, let’s save space and delete objects with β€œ.dis-info” extension from the folder.
🌐
DEV Community
dev.to β€Ί rishabdugar β€Ί creating-aws-lambda-layers-for-python-runtime-a-complete-guide-3gi0
The Best Way to Create AWS Lambda λ Layers for Python 🐍 (Fast & Easy) : A Complete Guide - DEV Community
January 17, 2025 - Go to Lambda > Layers and click Create Layer. Upload the python_layer.zip file, select the compatible runtime (e.g., Python 3.12), and save the layer.
🌐
Wahl Network
wahlnetwork.com β€Ί home β€Ί how to create aws lambda layers for python
How to Create AWS Lambda Layers for Python - Wahl Network
November 23, 2022 - In this post, I review how to setup Python for Windows and describe my use case scenario for needing Lambda layers for Python. I then deep dive into installing a Python package to a custom location, creating a zip file with all necessary packages and libraries, publishing the zipped code to an AWS Lambda layer, and associating the layer to an AWS Lambda function.
🌐
Pybites
pybit.es β€Ί articles β€Ί guest-create-aws-lambda-layers
How to Create an AWS Lambda Layer For Any Python Dependency – Pybites
Once the process has finished, you can see your zip file in the S3 bucket. Time to create our Lambda layer! $ aws lambda publish-layer-version \ --layer-name scikit-learn \ --description "Scikit-learn for Python 3.8" \ --compatible-runtimes python3.7 python3.8 \ --content S3Bucket=<name-of-your-bucket>
Find elsewhere
🌐
Reddit
reddit.com β€Ί r/aws β€Ί best way to build a python lambda layer?
r/aws on Reddit: Best way to build a python lambda layer?
January 4, 2021 -

There are lots of different articles online describing ways to build dependency layers for python lambda functions. Is the definitive way to build a lambda layer with packages that will work to do the following:

  1. spin up a (linux) EC2

  2. build a virtualenv and source it

  3. pip install the packages you want

  4. download the /pythonx.x/ directory and zip it

  5. upload it as a lambda layer

This seems to be the most common recommendation, but I wanted to know what the actual best practice is for doing this. Thanks,

🌐
AWS re:Post
repost.aws β€Ί knowledge-center β€Ί lambda-python-function-layer
How can I create a layer for my Lambda Python function?
February 7, 2023 - From the python-lambda-layer-creation Applications settings, select I acknowledge that this app creates custom IAM roles, and then choose Deploy. You can create a layer to invoke your Lambda function and pass a list of dependencies included ...
🌐
Plain English
plainenglish.io β€Ί blog β€Ί create-your-own-python-layer-on-aws-lambda-environment-2e5160b66f17
Create your own Python Layer in AWS Lambda Environment
April 9, 2021 - Import your custom Layer from any lambda function. I'll use the Rice n' Beans from the pythonic world as the example libraries, pandas and NumPy. This step should be very simple once you can execute anywhere you'd like. In the following lines of code, we are installing all packages placed on a .txt file which is on the same directory where the command will be executed (the good old requirements.txt). $ python3 -m pip install -r requirements.txt -t ./python
🌐
DEV Community
dev.to β€Ί aws-builders β€Ί how-to-create-a-python-lamda-layer-509j
How to create a Python Lambda Layer? - DEV Community
November 16, 2022 - We will create the layer using an EC2 Instance that will be provisioned by IaC (CloudFormation). I know what you are thinking. Why do we need an EC2 Instance to simply create a Lambda Layer? Why not just do it locally? The architecture for a Python Lambda Layer requires a Linux OS.
Top answer
1 of 4
6

If I may, I would like to recommend an alternative technique which has never failed me. The technique includes docker tool described in the recent AWS blog:

  • How do I create a Lambda layer using a simulated Lambda environment with Docker?

Thus for this question, I verified it using elasticsearch as follows:

  1. Create empty folder, e.g. mylayer.

  2. Go to the folder and create requirements.txt file with the content of

elasticsearch
  1. Run the following docker command (may adjust python version to your needs):
docker run -v "$PWD":/var/task "lambci/lambda:build-python3.8" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.8/site-packages/; exit"
  1. Create layer as zip:
zip -r elastic.zip python > /dev/null
  1. Create lambda layer based on elastic.zip in the AWS Console. Don't forget to specify Compatible runtimes to python3.8.

  2. Test the layer in lambda using the following lambda function:

import json

from elasticsearch import Elasticsearch, RequestsHttpConnection

def lambda_handler(event, context):
    # TODO implement
    
    print(dir(Elasticsearch))
    
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

The function executes correctly:

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'bulk', 'clear_scroll', 'close', 'count', 'create', 'delete', 'delete_by_query', 'delete_by_query_rethrottle', 'delete_script', 'exists', 'exists_source', 'explain', 'field_caps', 'get', 'get_script', 'get_script_context', 'get_script_languages', 'get_source', 'index', 'info', 'mget', 'msearch', 'msearch_template', 'mtermvectors', 'ping', 'put_script', 'rank_eval', 'reindex', 'reindex_rethrottle', 'render_search_template', 'scripts_painless_execute', 'scroll', 'search', 'search_shards', 'search_template', 'termvectors', 'update', 'update_by_query', 'update_by_query_rethrottle']
2 of 4
0

As one option is already mentioned by @Marcin which is required Docker to be installed in the target machine. If you want to skip docker then you can use below script to create and publish layer to AWS.

All you need

./creater_layer.sh <package_name> <layer_name>

./creater_layer.sh elasticsearch my-layer

script creater_layer.sh

path="app"
package="${1}"
layername="${2}"
mkdir -p $path
pip3 install "${package}" --target "${path}/python/lib/python3.8/site-packages/"
cd $path && zip -r ../lambdalayer.zip .
aws lambda publish-layer-version --layer-name "${layername}" --description "My layer" --license-info "MIT" --zip-file "fileb://../lambdalayer.zip" --compatible-runtimes python3.8
🌐
Nicolasneudeck
nicolasneudeck.com β€Ί blog β€Ί aws-python-lambda-layer
Creating and Deploying Custom Lambda Layers for Python Functions | Nicolas Neudeck
A step-by-step guide on creating and deploying custom AWS Lambda layers to include additional Python dependencies, featuring a bash script for building layers locally.
🌐
LinkedIn
linkedin.com β€Ί pulse β€Ί quick-tutorial-creating-aws-lambda-layer-pak-hun-chan
Quick Tutorial - Creating an AWS Lambda Layer
July 28, 2022 - Here is my quick guide on how to create a Lambda layer (with the PyMySQL library as an example): Step 1: Download the library onto your local environment Β· For my project, I needed the pymysql library, so I entered the following command: ... This places the pymysql library into a folder named "python...
🌐
Medium
medium.com β€Ί @shivkaundal β€Ί how-to-create-and-use-aws-lambda-layers-a-step-by-step-tutorial-424c762bbdbf
How to Create and Use AWS Lambda Layers: A Step-by-Step Tutorial | by Shiv Pal Singh Kaundal | Medium
March 31, 2025 - Follow these steps to create a deployment package: First, create a folder to organize the function and its dependencies: ... Inside this folder, create a Python file (e.g., lambda_function.py) and paste the code provided before.
🌐
Medium
jainsaket-1994.medium.com β€Ί creating-a-lambda-function-layer-in-python-without-using-ec2-using-cloudshell-399614a4e3ce
Create AWS Lambda Layers for Python Packages | by π’πšπ€πžπ­ π‰πšπ’π§ | Medium
October 9, 2023 - Create a ZIP archive of the python folder using the following command (replace my-layer.zip with your desired layer name): ... On the right side, locate the β€œActions” button. Under β€œActions,” click on β€œDownload file.” ... Use the absolute path. You can copy the file path from the command-line and paste it below. ... Use the AWS Command Line Interface (CLI) to publish your Lambda Layer using the AWS CloudShell terminal.
🌐
DEV Community
dev.to β€Ί razcodes β€Ί how-to-create-a-lambda-layer-in-aws-106m
How to create a lambda layer in AWS - DEV Community
May 10, 2020 - That folder structure is important because that is where Python expects to find the modules. Also as you can see, in this example I am using Python 3.8. Now we can go into the lambda-layer folder and create a zip file for the layer that will be uploaded using the console.
Top answer
1 of 3
1

I've seen that a few libraries like numpy and pandas don't work in Lambda when installed using pip. I have had success using the .whl package files for these libraries to create the Lambda layer. Refer to the steps below:

NOTE: These steps set up the libraries specific to the Python 3.7 runtime. If using any other version, you would need to download the .whl files corresponding to that Python version.

  1. Create an EC2 instance using Amazon Linux AMI and SSH into this instance. We should create our layer in Amazon Linux AMI as the Lambda Python 3.7 runtime runs on this operating system (doc).

  2. Make sure this instance has Python3 and "pip" tool installed.

  3. Download the numpy .whl file for the cp37 Python version and the manylinux1_x86_64 OS by executing the below command:

$ wget https://files.pythonhosted.org/packages/d6/c6/58e517e8b1fb192725cfa23c01c2e60e4e6699314ee9684a1c5f5c9b27e1/numpy-1.18.5-cp37-cp37m-manylinux1_x86_64.whl
  1. Skip to the next step if you're not using pandas. Download the pandas .whl file for the cp37 Python version and the manylinux1_x86_64 OS by executing the below command:
$ wget https://files.pythonhosted.org/packages/a4/5f/1b6e0efab4bfb738478919d40b0e3e1a06e3d9996da45eb62a77e9a090d9/pandas-1.0.4-cp37-cp37m-manylinux1_x86_64.whl
  1. Next, we will create a directory named "python" and unzip these files into that directory:
        $ mkdir python
        $ unzip pandas-1.0.4-cp37-cp37m-manylinux1_x86_64.whl -d python/
        $ unzip numpy-1.18.5-cp37-cp37m-manylinux1_x86_64.whl -d python/
  1. We also need to download "pytz" library to successfully import numpy and pandas libraries:
        $ pip3 install -t python/ pytz
  1. Next, we would remove the β€œ*.dist-info” files from our package directory to reduce the size of the resulting layer.
        $ cd python
        $ sudo rm -rf *.dist-info
  1. This will install all the required libraries that we need to run pandas and numpy.

  2. Zip the current "python" directory and upload it to your S3 bucket. Ensure that the libraries are present in the hierarchy as given here.

        $ cd ..
        $ zip -r lambda-layer.zip python/
        $ aws s3 cp lambda-layer.zip s3://YOURBUCKETNAME
  1. The "lambda-layer.zip" file can then be used to create a new layer from the Lambda console.
2 of 3
0

Base on aws lamda layer doc, https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html your zip package for the layer must have this structure.

my_layer.zip
  | python/numpy
  | python/numpy-***.dist-info

So what you have to do is create a folder python, and put the content of site-packages inside it, then zip up that python folder. I tried this out with a simple package and it seem to work fine.

Also keep in mind, some package require c/c++ compilation, and for that to work you must install and package on a machine with similar architecture to lambda. Usually you would need to do this on an EC2 where you install and package where it have similar architecture to the lambda.

🌐
Amazon Web Services
docs.aws.amazon.com β€Ί aws lambda β€Ί developer guide β€Ί managing lambda dependencies with layers
Managing Lambda dependencies with layers - AWS Lambda
For more information, see Working with Lambda layers and extensions in container images ... To create a layer, package your dependencies into a .zip file, similar to how you create a normal deployment package.