How to Handle your Python packaging in Lambda with Serverless plugins

Amit Joshi
1 min readMay 11, 2019

Python is my go-to language, but handling Python packages in Lambda can be tricky. So in this blog i will tell you how you can Handel your python package in Lambda with serverless plugins.

Here few steps using you can handle your python package in Lambda function

Step 1- You need to install serverless-python-requirements

npm install serverless-python-requirements

Step 2- Then add the following to your serverless.yml

plugins:
- serverless-python-requirements

Step 3- Make sure you have your python virtual environment active in CLI

source venv/bin/activate
or
workon <env name>

Step 4- Install any dependencies with pip — note that in CLI you can tell if venv is active by the venv to the left of the terminal text

(venv) $ pip install <NAME>
(venv) $ pip freeze > requirements.txt

Step 5- Now, deploy your function

serverless deploy

What will happen is that serverless-python-requirements will build you python packages in docker using a lambda environment, and then zip them up ready to be uploaded with the rest of your code.

--

--