DEV Community

Cover image for Handling AWS Lambda Python Dependencies
We're Serverless! for Serverless Inc.

Posted on • Originally published at serverless.com

Handling AWS Lambda Python Dependencies

Originally posted at Serverless

Developing AWS Lambda functions with **Python** 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**.

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”**

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**.

OK folks, let’s get down to code 💻

Let’s begin creating our sample project called “test-dependencies”

sls create --template aws-python3 --name test-dependencies --path test-dependenciescd test-dependencies
view raw .sh hosted with ❤ by GitHub

‍Now clean up the “serverless.yml” file to looks like the following
service: test-dependenciesprovider:
name: aws
runtime: python3.7functions:
hello:
handler: src/handler.hello
view raw .yml hosted with ❤ by GitHub

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

import json
import sysdef hello(event, context):
body = {
"message": "Python Function executed successfully!"
}
response = {
"statusCode": 200,
"body": json.dumps(body)
}
return response
view raw .py hosted with ❤ by GitHub

Time to deploy 🚀
sls deploy
view raw .sh hosted with ❤ by GitHub

and test our function ⚡️
sls invoke -f hello
view raw .sh hosted with ❤ by GitHub

This will be the output

It’s time to start breaking 💣 things and add some libraries to our python code. In the “src/handler.py” lets import **“jsonpath_rw”**

import json
import sys
sys.path.insert(0, 'src/vendor')
from jsonpath_rw import jsonpath, parse
def hello(event, context):
body = {
"message": "Python Function executed successfully!"
}
jsonpath.auto_id_field = 'id'
data = [match.value for match in parse('foo[*].id').find({'foo': [{'id': 'bizzle'}, {'baz': 3}]})]
response = {
"statusCode": 200,
"body": json.dumps(body),
"data": json.dumps(data)
}
return response
view raw .py hosted with ❤ by GitHub

And deploy again 🚀
sls deploy
view raw .sh hosted with ❤ by GitHub

and test the function again ⚡️
sls invoke -f hello
view raw .sh hosted with ❤ by GitHub

Now we got an error (expected behavior ) indicating that the library could not be imported 😱

Don’t worry, here comes the fun. We will use an interesting feature of the “**pip”** 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:

pip install -t src/vendor -r aws_requirements.txt
view raw .sh hosted with ❤ by GitHub

This will create a beautiful “vendor” folder inside “src” with dependencies within it.

And deploy again 🚀

sls deploy
view raw .sh hosted with ❤ by GitHub

and test the function again ⚡️
sls invoke -f hello
view raw .sh hosted with ❤ by GitHub

This would be the output

🎉 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

import sys
sys.path.insert(0, 'src/vendor')
from jsonpath_rw import jsonpath, parse
view raw .py hosted with ❤ by GitHub

This methodology is easy to understand and pretty straightforward to implement.‍

Originally published at https://www.serverless.com.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay