Handling AWS Lambda Python Dependencies
Tags: aws, lambda, python, serverless
Published: 2022-04-21 16:51:08

Originally posted at [Serverless](https://www.serverless.com/blog/handling-aws-lambda-python-dependencies)

![](https://cdn-images-1.medium.com/max/3200/0*v5Un0s41vTMgqn4b.png)

Developing AWS Lambda functions with [**Python](https://www.python.org/)** is awesome because this is a powerful programming language, but the tears start coming when we start dealing with dependencies.

In this article you will learn how to handle and win the battle against the Python dependencies when you deploy your Lambda functions to AWS using the [**Serverless Framework](https://serverless.com/)**.

When we refer to “dependencies” we are talking about those libraries that are not available in the AWS Python Lambda runtime, for example [**“jsonpath_rw”](https://pypi.org/project/jsonpath-rw/)**

First of all we need to clarify that the methodology described in this article is not a “silver bullet” to manage dependencies (but an easy one). We recommend reading the following official dependencies management that can be useful.

Remember that all the source code we use in this article will be available in [**github](https://github.com/dorian599/serverless-medium/tree/master/test-dependencies)**.

OK folks, let’s get down to code 💻

Let’s begin creating our sample project called “test-dependencies”
{% gist https://gist.github.com/richardgrantserverless/76537e165b502923b05bc9ba7c1920cc.js %}
‍Now clean up the **“serverless.yml”** file to looks like the following
{% gist https://gist.github.com/richardgrantserverless/1c3ab7a974b092f8e8f94ceabe76071d.js %}
As shown above we will work with our simple test function called “hello” located inside the folder “src” where our python code will be grouped.

And the code of the **“src/handler.py”** function will be the following
{% gist https://gist.github.com/richardgrantserverless/567ddeb9700d8cd479f8786d8ac862c2.js %}
Time to deploy 🚀
{% gist https://gist.github.com/richardgrantserverless/821221a96a005a20a27daa01eb7f0d3e.js %}
and test our function ⚡️
{% gist https://gist.github.com/richardgrantserverless/67c30e65cbae5064e7be66fff4397836.js %}
This will be the output

![](https://cdn-images-1.medium.com/max/2000/0*8CP52hTOtJAVu8l2.png)

It’s time to start breaking 💣 things and add some libraries to our python code. In the **“src/handler.py”** lets import [**“jsonpath_rw”](https://pypi.org/project/jsonpath-rw/)**
{% gist https://gist.github.com/richardgrantserverless/cb17852e9bb1ea59fb007b9581fbf89a.js %}
And deploy again 🚀
{% gist https://gist.github.com/richardgrantserverless/26488a9855b495687c9ac8ee4fbf84ad.js %}
and test the function again ⚡️
{% gist https://gist.github.com/richardgrantserverless/2bf7c81adb3eafb645bf0cde42961d6f.js %}
Now we got an error (expected behavior ) indicating that the library could not be imported 😱

![](https://cdn-images-1.medium.com/max/2000/0*D4iVAWuLAZZZOn3a.png)

Don’t worry, here comes the fun. We will use an interesting feature of the “[**pip](https://en.wikipedia.org/wiki/Pip_(package_manager))”** package manager for Python.

Create a file called **“aws_requirements.txt”** with the following content
>  *jsonpath_rw==1.4.0*

**NOTE:** use the version of the library you need for your project.

and Execute this magic pip command:
{% gist https://gist.github.com/richardgrantserverless/d94bd4d28ca09b4fdb129d03a27ced5a.js %}
This will create a beautiful **“vendor”** folder inside **“src”** with dependencies within it.

![](https://cdn-images-1.medium.com/max/2000/0*N7khw-XOnkb10DHK.png)

And deploy again 🚀
{% gist https://gist.github.com/richardgrantserverless/68a195ea72f769f653bfb4042c4ec374.js %}
and test the function again ⚡️
{% gist https://gist.github.com/richardgrantserverless/1c777cc395346efca3247278584276ab.js %}
This would be the output

![](https://cdn-images-1.medium.com/max/2000/0*JWbVCIQB6h-DAuv_.png)

🎉 Amazing 🎉 we did it, we executed a Lambda function in AWS with dependencies nice and easy.

Using the **pip** command with the **-t** flag allowed us to download dependencies (indicated in the file “aws_requirements.txt”) and encapsulated them into the “src/vendor” folder.

Inside our Python code we need to expand the execution context of our function to allow us to import the library “jsonpath_rw” with
{% gist https://gist.github.com/richardgrantserverless/ff765c5c17e9a8773c3f9dddc70a8c03.js %}
This methodology is easy to understand and pretty straightforward to implement.‍

*Originally published at [https://www.serverless.com](https://www.serverless.com/blog/handling-aws-lambda-python-dependencies).*