So I've recently ran into this issue, and I believe I found a cleaner way to import your layers.

First for the structure of the zip file which you upload:

  • You do not need an __init__.py file
  • Put all the scripts which you want to import into a folder name python
  • Zip up that python folder (choose any name you want) and upload it to your layer
  • Once uploaded, and the layer has been configured in your lambda function, you can simply use it with import {filename}

So if your script in the python folder was called something like custom_helper.py, import it in your lambda with import custom_helper.

I am not sure if this is the clean way to do it, but it seems simple enough to start.

Answer from Cecaro on Stack Overflow
๐ŸŒ
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 - If you want a deep discussion about ... this library has other dependencies and could be a good sample. ... Navigate to Lambda > Layers and create a layer....
Discussions

How do I import a Python lambda layer? - Stack Overflow
I have a file with this as the contents. def print_hello_world(): print ('Hello World') It's zipped up in a folder with a __init__.py file. I add this as the layer and set the correct runtime... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
amazon web services - How to use AWS Lambda layer using Python? - Stack Overflow
I have a simple Lambda function which is using the numpy library, I have set up a virtual environment in my local, and my code is able to fetch and use the library locally. I tried to use AWS Lamb... More on stackoverflow.com
๐ŸŒ stackoverflow.com
CDK PyPi Python Lambda Layer
I tend to not use layers unless it's for a very specific reason and prefer just packaging within the Lambda itself. I use this to package a python based Lambda with requirements: https://subaud.io/blog/deploying-python-lambda-with-requirements-using-cdk It uses similar methods of using a Docker container to do the build locally. More on reddit.com
๐ŸŒ r/aws
10
2
February 27, 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 - AWS recommends the use of Cloud9 to do that as your custom layer must be compatible with Amazon Linux to function correctly on your lambda. But I find WSL satisfactory in MOST cases) For the demo, I'll just be following the example listed in the docs as it's very clear but I'll remove the clutter and share my own experience ๐Ÿ˜‰. 1- I'll make one directory to group everything I need for this task as follows: my-lambda |_ venv-python3.9 |_ python |_ lambda-layer.zip
๐ŸŒ
GitHub
github.com โ€บ keithrozario โ€บ Klayers
GitHub - keithrozario/Klayers: Python Packages as AWS Lambda Layers ยท GitHub
Using CDK, you can use the cdk-klayers package to help you pull in the latest layers for your Stack or App. from cdk_klayers import Klayers class MockStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) runtime = aws_lambda.Runtime.PYTHON_3_12 # Initialize Klayers Class klayers = Klayers( self, python_version = runtime, region = "ap-southeast-1" ) # get the latest layer version for the requests package requests_layer = klayers.layer_version(self, "requests") lambda_function = aws_lambda.Function( self, 'HelloHandler', runtime=runtime, layers=[requests_layer], code=aws_lambda.Code.from_asset('lambda'), handler='hello.handler' )
Starred by 2.5K users
Forked by 353 users
Languages ย  Python 82.8% | HCL 15.2% | Dockerfile 1.9% | Shell 0.1%
๐ŸŒ
Capital One
capitalone.com โ€บ tech โ€บ cloud โ€บ creating-lambda-layers
Creating Lambda Layers for Python Functions | Capital One
June 1, 2023 - In conclusion, creating a Lambda layer for Python functions can significantly simplify the process of deploying code and managing dependencies, while also overcoming the limitations of Lambda itself. By creating a layer or two containing common libraries, developers can keep their functions lean and easily update their dependencies as needed.
๐ŸŒ
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 - Seamless Integration: CloudShell integrates seamlessly with other AWS services, simplifying the process of uploading files to S3 and creating Lambda Layers. Below are the steps to create a Layer ZIP file in CloudShell: ... Log in to the AWS Management Console. Click on CloudShell icon left of the notification icon ... Wait for the the terminal to load. The default version of cloudshell is python 3.9.
Find elsewhere
๐ŸŒ
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 - AWS Lambda layers are .zip archives that let you share libraries, custom runtimes, or other dependencies across functions. They simplify updates, promote modularity, and reduce deployment sizes.
๐ŸŒ
AWS re:Post
repost.aws โ€บ knowledge-center โ€บ lambda-python-function-layer
Create a layer for a Lambda Python function | AWS re:Post
February 7, 2023 - You can create a layer to invoke your Lambda function and pass a list of dependencies included with the layer metadata. The following example creates Python Lambda layers containing requests (latest version), numpy (version 1.20.1), and keyring (version >= 4.1.1) libraries.
๐ŸŒ
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 - A Lambda Layer is an isolated zip file that contains libraries, packages and/or application code that is shareable between your Lambda functions. A common use case for a Lambda Layer is, for example your company has two separate Python Web Scrapers.
๐ŸŒ
Medium
medium.com โ€บ the-cloud-architect โ€บ getting-started-with-aws-lambda-layers-for-python-6e10b1f9a5d
Getting started with AWS Lambda Layers for Python | by Adrian Hornsby | The Cloud Engineer | Medium
June 1, 2023 - Let me explain โ€” A Layer is a ZIP archive that contains libraries and other dependencies that you can import at runtime for your lambda functions to use. It is especially useful if you have several AWS Lambda functions that use the same set ...
๐ŸŒ
GitHub
github.com โ€บ mthenw โ€บ awesome-layers
GitHub - mthenw/awesome-layers: ฮป A curated list of awesome AWS Lambda Layers. Sponsored by https://cloudash.dev ยท GitHub
ARN: arn:aws:lambda:us-east-1:251566558623:layer:python37-layer-pandas-gbq:1 Link: vbalasu/pandas-gbq-layer Includes pandas, a versatile data exploration tool that builds on numpy.
Starred by 2.3K users
Forked by 184 users
๐ŸŒ
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 scenario, I construct a Python script to scrape the GitLab GraphQL API and upload the formatted results to InfluxDB using their Python client library. The objective is to monitor continuous integration workflows and environments in a live dashboard. The script must satisfy these requirements: from datetime import datetime from influxdb_client import InfluxDBClient, Point, WritePrecision from influxdb_client.client.write_api import SYNCHRONOUS ยท The influxdb_client package and all dependencies must be stored inside a Lambda layer before the script can be run as a Lambda function.
๐ŸŒ
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,

๐ŸŒ
Medium
aws.plainenglish.io โ€บ lambda-layer-how-to-create-them-python-version-bc1e027c5fea
Lambda Layer : how to create them? โ€” python version. | AWS in Plain English
August 21, 2024 - A Lambda layer is an archive containing additional code, such as libraries, dependencies, or even custom runtimes. When you include a layer in a function, the contents are extracted to the /opt directory in the execution environment.
๐ŸŒ
Amazon Web Services
docs.aws.amazon.com โ€บ aws lambda โ€บ developer guide โ€บ building lambda functions with python โ€บ working with .zip file archives for python lambda functions
Working with .zip file archives for Python Lambda functions - AWS Lambda
For Lambda functions that use the Python runtime, a dependency can be any Python package or module. When you deploy your function using a .zip archive, you can either add these dependencies to your .zip file with your function code or use a Lambda layer. A layer is a separate .zip file that can contain additional code and other content.
๐ŸŒ
Nicolasneudeck
nicolasneudeck.com โ€บ blog โ€บ aws-python-lambda-layer
Creating and Deploying Custom Lambda Layers for Python Functions | Nicolas Neudeck
If your code requires additional packages, such as sqlalchemy, the function will fail unless those dependencies are included. A Lambda layer allows you to include additional packages that your function requires.
๐ŸŒ
Keras
keras.io โ€บ api โ€บ layers โ€บ core_layers โ€บ lambda
Keras documentation: Lambda layer
The main reason to subclass Layer instead of using a Lambda layer is saving and inspecting a model. Lambda layers are saved by serializing the Python bytecode, which is fundamentally non-portable and potentially unsafe. They should only be loaded in the same environment where they were saved.
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.