How to add dependencies to a Lambda Function in AWS
Photo by Guenter Guni

How to add dependencies to a Lambda Function in AWS


The Lambda Python runtimes includes the AWS SDK for Python (Boto3) and its dependencies, but this does not include a lot of frequently used libraries. Quite recently, I had to work with pandas in AWS Lambda, and quickly realized that it's not as easy as simply saying import pandas as pd or saying pip install pandas. AWS has their own way of adding dependencies, and that's through something called "Layers".

A Lambda layer is generally a .zip folder that contains supplementary resources, which can be for library dependencies, a custom runtime, or configuration files.

My aim with this post is to provide an easy and straightforward way to add Layers to a Lambda function, that 100% works, because when I first tried, I wasn't able to find adequate guidance. I'll show you how to add pandas, but this approach can be applied for any dependency. In fact, you'll technically learn how to add three different dependencies at once with this article!

Article content
Error thrown when Lambda function is run without adding Layer for pandas dependency

1. Download the files for the dependencies you need

Article content
This is the version I used for the purpose of this tutorial

  • Once you select the required version, under "Download files" under the navigation pane, select the "Build Distribution" that you need. Selecting the build distribution is key. It depends on the "Runtime" and "Architecture" that you select for your Lambda function. These are attributes that you select during the time of function creation.

Article content
This is what the AWS console looks like for creating a Lambda function. This is where you'll select the Architecture and Runtime of your Lambda Function

  • Under each build distribution, you'll see a label of the format "cp123". These refer to the runtimes. So in the below image, cp312 refers to Python 3.12 while cp311 is Python 3.11. Since my Lambda function's runtime is Python 3.10, I'll need to choose cp310.

Article content
List of Build Distributions

  • However, you'll notice that there are numerous files that are of type cp310. For a Lambda function we need to choose the one for Linux, since Lambda functions run in a Linux environment. Don't download windows/mac since your machine is windows/mac. You'll see two files for Linux environment, and here you'll select the one that pertains to your function's architecture. Mine is x86_64 so I'll choose the first file.

Article content
Multiple types for Linux environment based on function architecture

2. Unzip the wheel file

Use the command wheel unpack <filename>

Article content
Unzip the wheel file

3. Make a new folder named "python"

Make a new folder named "python" and move the contents from inside of the unzipped folder from the previous step, into this new folder.

4. Repeat steps 1-3 to download numpy and pytz since pandas is dependent on numpy. *** Don't remake "python" folder

Make sure to add the files from the unzipped folders to the python folder. At this point, your python folder should have all the required filed for pandas, numpy and pytz. ***pytz wouldn't have "cp123" type tags but rather has "py2" and "py3" which indicates that it is suitable for any Python 2 or 3 versions.


Article content
"python" folder with all required dependencies

5. Zip up python file.

6. Create a Layer to upload dependencies

While creating a layer to, select the same runtime and architecture as your lambda function. You can choose up to 15 runtimes. This is where you'll upload the file you just zipped in the previous step.

7. Add the layer to the lambda function

When you add the layer, select "Custom layers" and select the Layer you just created.

8. Run your Lambda Function

Now, if you run your Lambda function, it should execute without any errors!

Article content
Successful compilation of Lambda function


I wanted to express my gratitude for your insightful post. It provided much-needed clarity for me as I tried to create a Lambda’s custom layer for the first time. I'd like to contribute a bit more to the discussion based on my own experience. While attempting the suggested approach of downloading packages from pypi.org, I encountered some challenges. Specifically, for my lambda function, I needed to install several packages, each with its own set of dependencies. Keeping track of these dependencies became somewhat complex. Instead, I opted to install the Windows Subsystem for Linux (WSL) (https://www.ssl.com/how-to/enable-linux-subsystem-install-ubuntu-windows-10) and followed these steps: 1. Installed the necessary Python version using WSL (sudo apt install python3.9). 2. Installed all required packages to a Python folder using pip (pip install --target python_folder requirements.txt). 3. Zipped this folder and proceeded with the steps outlined in your guide. This alternative approach proved to be highly effective in managing dependencies and simplifying the process. Thank you once again for your valuable insights.

To view or add a comment, sign in

More articles by Om Kudalkar

  • Deploying a Server using IaC

    In my previous article, I gave a quick run-down of what IaC is, why it's lucrative, and why adopting it is beneficial…

  • An Introduction to Infrastructure as Code

    "IaC" or Infrastructure as Code, is simply the ability to provision, manage and support your computing infrastructure…

    3 Comments
  • Unpaid Internships - yea or nay? Nay.

    Today, I came across this post by Ishan Arora and I was initially reluctant to say anything especially on a…

  • How Good Design Implementation Prevents Errors

    Over this semester I’m taking Object Oriented Programming and Design and till now, I’ve learned a lot. I now have a…

  • How java stores array length safely.

    Quite recently, in my Object Oriented Programming class, we learnt what constitutes good software design and what…

  • Useful Intraday Trading Indicators

    Top Indicators for day trading - 5 and 10 EMA (Exponential Moving Average) Bollinger Band (BB) RSI Volume A. 5 and 10…

    2 Comments
  • How you can gain control of your free time.

    This was a particularly interesting talk and a very powerful one. It opened my eyes to some of the mistakes I was doing…

    2 Comments
  • Yes, Binary Search is O(log(n)), but how?

    We know that the Binary Search algorithm is of O(log(n)) complexity, but how do we arrive at this conclusion? Is there…

    2 Comments
  • HOW YOU CAN SELL ANYTHING

    Derek Thompson shares the 4-letter code to sell anything. Quite recently, I watched Derek Thompson's Ted talk which…

Others also viewed

Explore content categories